ImageResultsProvider.php 1.9 KB

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