Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7955825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:48:29+00:00 2026-06-04T03:48:29+00:00

I have written this code for a search page in PHP and I would

  • 0

I have written this code for a search page in PHP and I would like to know a way to make the search results more accurate. Some search strings will pull up everything in the database because it contains that word. Here is my code

<?php include("header.php");?>
<h3>Rental Search Results</h3>
<div class="searchbox">
<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="usersearch">Search Rentals Now:</label>
<input type="text" name="usersearch" value="<?php echo $_GET['usersearch']; ?>" />
<input type="submit" name="submit" value="Search" />
</form>
</div>
<br />
<br />
<?php

  // This function builds a search query from the search keywords and sort setting
  function build_query($user_search, $sort) {
    $search_query = "SELECT * FROM online_rental_db";

    // Extract the search keywords into an array
    $clean_search = str_replace(',', ' ', $user_search);
    $search_words = explode(' ', $clean_search);
    $final_search_words = array();
    if (count($search_words) > 0) {
      foreach ($search_words as $word) {
        if (!empty($word)) {
          $final_search_words[] = $word;
        }
      }
    }

    // Generate a WHERE clause using all of the search keywords
    $where_list = array();
    if (count($final_search_words) > 0) {
      foreach($final_search_words as $word) {
        $where_list[] = "Description LIKE '%$word%' OR Manufacturer LIKE '%$word%' OR Model LIKE '%$word%' OR Category LIKE '%$word%'";
      }
    }
    $where_clause = implode(' OR ', $where_list);

    // Add the keyword WHERE clause to the search query
    if (!empty($where_clause)) {
      $search_query .= " WHERE $where_clause";
    }

    // Sort the search query using the sort setting
    switch ($sort) {
    // Ascending by job title
    case 1:
      $search_query .= " ORDER BY Description";
      break;
    // Descending by job title
    case 2:
      $search_query .= " ORDER BY Description DESC";
      break;
    // Ascending by state
    case 3:
      $search_query .= " ORDER BY Manufacturer";
      break;
    // Descending by state
    case 4:
      $search_query .= " ORDER BY Manufacturer DESC";
      break;
    // Ascending by date posted (oldest first)
    case 5:
      $search_query .= " ORDER BY Model";
      break;
    // Descending by date posted (newest first)
    case 6:
      $search_query .= " ORDER BY Model DESC";
      break;
    default:
      // No sort setting provided, so don't sort the query
    }

    return $search_query;
  }

  // This function builds navigational page links based on the current page and the number of pages
  function generate_page_links($user_search, $sort, $cur_page, $num_pages) {
    $page_links = '';

    // If this page is not the first page, generate the "previous" link
    if ($cur_page > 1) {

      $page_links .= '<a href="' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search . '&sort=' . $sort . '&page=' . ($cur_page - 1) . '"><-</a> ';
    }
    else {
      $page_links .= '<- ';
    }

    // Loop through the pages generating the page number links
    for ($i = 1; $i <= $num_pages; $i++) {
      if ($cur_page == $i) {
        $page_links .= ' ' . $i;
      }
      else {
        $page_links .= ' <a href="' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search . '&sort=' . $sort . '&page=' . $i . '"> ' . $i . '</a>';
      }
    }

    // If this page is not the last page, generate the "next" link
    if ($cur_page < $num_pages) {
      $page_links .= ' <a href="' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search . '&sort=' . $sort . '&page=' . ($cur_page + 1) . '">-></a>';
    }
    else {
      $page_links .= ' ->';
    }

    return $page_links;
  }

  // Grab the sort setting and search keywords from the URL using GET
  $user_search = $_GET['usersearch'];

  // Calculate pagination information
  $cur_page = isset($_GET['page']) ? $_GET['page'] : 1;
  $results_per_page = 5;  // number of results per page
  $skip = (($cur_page - 1) * $results_per_page);

  // Start generating the table of results
  echo '<table class="results">';
  echo '<td align="center">Description</td><td align="center">Manufacturer</td><td align="center">Model #</td><td align="center">Image</td>';

  // Generate the search result headings
  echo '<tr class="bottomborder">';
  echo '</tr>';

  // Connect to the database
  require_once('connectvars.php');
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  // Query to get the total results 
  $query = build_query($user_search,'');
  $result = mysqli_query($dbc, $query);
  $total = mysqli_num_rows($result);
  $num_pages = ceil($total / $results_per_page);

  // Query again to get just the subset of results
  $query =  $query . " LIMIT $skip, $results_per_page";
  $result = mysqli_query($dbc, $query);
  while ($row = mysqli_fetch_array($result)) {
      $description = $row['Description'];
      $model = $row['Model'];
      $manufacturer = $row['Manufacturer'];
      $image = $row['Image'];
      $hour = $row['Hour'];
      $day = $row['Day'];
      $week = $row['Week'];
      $month = $row['Month'];
      $file = $row['PDF'];
      $ID = $row['ID'];
      $Category = $row['Category'];
      $CTGID = $row['CTGID'];
      if ($image == "") {
          $image = "No Image";
      }else {
          $image = "<a class=\"thumbnail\" href=\"pp.php?ID=$ID\"><img class=\"rental_image\" src=\"$image\"><span><img src=\"$image\" ><br> $description</span></a>";
      }
    echo '<tr>';
    echo "<td align=\"center\" valign=\"middle\" width=\"25%\"><a href=\"pp.php?ID=$ID\" alt=\"$description\">$description</a></td>";
    echo "<td align=\"center\" valign=\"middle\" width=\"25%\"><a href=\"pp.php?ID=$ID\">$manufacturer</a></td>";
    echo "<td align=\"center\" valign=\"middle\" width=\"25%\"><a href=\"pp.php?ID=$ID\">$model</a></td>";
    echo "<td align=\"center\" valign=\"middle\" width=\"25%\">$image</td>";
    echo '</tr>';



  } 
  echo '</table>';

  // Generate navigational page links if we have more than one page
  if ($num_pages > 1) {
    echo generate_page_links($user_search, '', $cur_page, $num_pages);
  } 
  mysqli_close($dbc);
?>
<?php include("footer.php");?>
</div>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:48:30+00:00Added an answer on June 4, 2026 at 3:48 am

    I would use a fulltext index with match against.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code in javascript however I have to make it work
Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {
In our application, I have seen code written like this: User.java (User entity) public
I have always written my boolean expressions like this: if (!isValid) { // code
I have written this code to convert string in such format 0(532) 222 22
In my code I have written this but it fails to compile: In Class1.h:
I have this code written in .NET 4.0 using VS2010 Ultimate Beta 2: smtpClient.Send(mailMsg);
I have written grammar for a language (sample code below) //this is a procedure
I have this code in my ASP.NET application written in C# that is trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.