SiteResultsProvider.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. class SiteResultsProvider {
  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 sites WHERE title LIKE :term
  10. OR url LIKE :term
  11. OR keywords LIKE :term
  12. OR description LIKE :term");
  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 sites WHERE title LIKE :term
  23. OR url LIKE :term
  24. OR keywords LIKE :term
  25. OR description LIKE :term
  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='siteResults'>";
  34. while($row = $query->fetch(PDO::FETCH_ASSOC)) {
  35. $id = $row["id"];
  36. $url = $row["url"];
  37. $title = $row["title"];
  38. $description = $row["description"];
  39. $title = $this->trimField($title, 55);
  40. $description = $this->trimField($description, 230);
  41. $resultsHtml .= "<div class='resultContainer'>
  42. <h3 class='title'>
  43. <a class='result' href='$url' data-linkId='$id'>
  44. $title
  45. </a>
  46. </h3>
  47. <span class='url'>$url</span>
  48. <span class='description'>$description</span>
  49. </div>";
  50. }
  51. $resultsHtml .= "</div>";
  52. return $resultsHtml;
  53. }
  54. private function trimField($string, $characterLimit) {
  55. $dots = strlen($string) > $characterLimit ? "..." : "";
  56. return substr($string, 0, $characterLimit) . $dots;
  57. }
  58. }
  59. ?>