ImageResultsProvider.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. class ImageResultsProvider {
  3. private $con;
  4. public function __construct($con) {
  5. $this->con = $con;
  6. }
  7. public function getNumResults($term) {
  8. $query = $this->con->prepare("SELECT COUNT(*) as total
  9. FROM images
  10. WHERE (title LIKE :term
  11. OR alt LIKE :term)
  12. AND broken=0");
  13. $searchTerm = "%". $term . "%";
  14. $query->bindParam(":term", $searchTerm);
  15. $query->execute();
  16. $row = $query->fetch(PDO::FETCH_ASSOC);
  17. return $row["total"];
  18. }
  19. public function getResultsHtml($page, $pageSize, $term) {
  20. $fromLimit = ($page - 1) * $pageSize;
  21. $query = $this->con->prepare("SELECT *
  22. FROM images
  23. WHERE (title LIKE :term
  24. OR alt LIKE :term)
  25. AND broken=0
  26. ORDER BY clicks DESC
  27. LIMIT :fromLimit, :pageSize");
  28. $searchTerm = "%". $term . "%";
  29. $query->bindParam(":term", $searchTerm);
  30. $query->bindParam(":fromLimit", $fromLimit, PDO::PARAM_INT);
  31. $query->bindParam(":pageSize", $pageSize, PDO::PARAM_INT);
  32. $query->execute();
  33. $resultsHtml = "<div class='imageResults'>";
  34. $count = 0;
  35. while($row = $query->fetch(PDO::FETCH_ASSOC)) {
  36. $count++;
  37. $id = $row["id"];
  38. $imageUrl = $row["imageUrl"];
  39. $siteUrl = $row["siteUrl"];
  40. $title = $row["title"];
  41. $alt = $row["alt"];
  42. if($title) {
  43. $displayText = $title;
  44. }
  45. else if($alt) {
  46. $displayText = $alt;
  47. }
  48. else {
  49. $displayText = $imageUrl;
  50. }
  51. $resultsHtml .= "<div class='gridItem image$count'>
  52. <a href='$imageUrl' data-fancybox data-caption='$displayText'
  53. data-siteurl='$siteUrl'>
  54. <script>
  55. $(document).ready(function() {
  56. loadImage(\"$imageUrl\", \"image$count\");
  57. });
  58. </script>
  59. <span class='details'>$displayText</span>
  60. </a>
  61. </div>";
  62. }
  63. $resultsHtml .= "</div>";
  64. return $resultsHtml;
  65. }
  66. }
  67. ?>