Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP search page problems
    primarykey
    data
    text
    <p>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</p> <pre><code>&lt;?php include("header.php");?&gt; &lt;h3&gt;Rental Search Results&lt;/h3&gt; &lt;div class="searchbox"&gt; &lt;form method="get" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt; &lt;label for="usersearch"&gt;Search Rentals Now:&lt;/label&gt; &lt;input type="text" name="usersearch" value="&lt;?php echo $_GET['usersearch']; ?&gt;" /&gt; &lt;input type="submit" name="submit" value="Search" /&gt; &lt;/form&gt; &lt;/div&gt; &lt;br /&gt; &lt;br /&gt; &lt;?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) &gt; 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) &gt; 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 &gt; 1) { $page_links .= '&lt;a href="' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search . '&amp;sort=' . $sort . '&amp;page=' . ($cur_page - 1) . '"&gt;&lt;-&lt;/a&gt; '; } else { $page_links .= '&lt;- '; } // Loop through the pages generating the page number links for ($i = 1; $i &lt;= $num_pages; $i++) { if ($cur_page == $i) { $page_links .= ' ' . $i; } else { $page_links .= ' &lt;a href="' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search . '&amp;sort=' . $sort . '&amp;page=' . $i . '"&gt; ' . $i . '&lt;/a&gt;'; } } // If this page is not the last page, generate the "next" link if ($cur_page &lt; $num_pages) { $page_links .= ' &lt;a href="' . $_SERVER['PHP_SELF'] . '?usersearch=' . $user_search . '&amp;sort=' . $sort . '&amp;page=' . ($cur_page + 1) . '"&gt;-&gt;&lt;/a&gt;'; } else { $page_links .= ' -&gt;'; } 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 '&lt;table class="results"&gt;'; echo '&lt;td align="center"&gt;Description&lt;/td&gt;&lt;td align="center"&gt;Manufacturer&lt;/td&gt;&lt;td align="center"&gt;Model #&lt;/td&gt;&lt;td align="center"&gt;Image&lt;/td&gt;'; // Generate the search result headings echo '&lt;tr class="bottomborder"&gt;'; echo '&lt;/tr&gt;'; // 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 = "&lt;a class=\"thumbnail\" href=\"pp.php?ID=$ID\"&gt;&lt;img class=\"rental_image\" src=\"$image\"&gt;&lt;span&gt;&lt;img src=\"$image\" &gt;&lt;br&gt; $description&lt;/span&gt;&lt;/a&gt;"; } echo '&lt;tr&gt;'; echo "&lt;td align=\"center\" valign=\"middle\" width=\"25%\"&gt;&lt;a href=\"pp.php?ID=$ID\" alt=\"$description\"&gt;$description&lt;/a&gt;&lt;/td&gt;"; echo "&lt;td align=\"center\" valign=\"middle\" width=\"25%\"&gt;&lt;a href=\"pp.php?ID=$ID\"&gt;$manufacturer&lt;/a&gt;&lt;/td&gt;"; echo "&lt;td align=\"center\" valign=\"middle\" width=\"25%\"&gt;&lt;a href=\"pp.php?ID=$ID\"&gt;$model&lt;/a&gt;&lt;/td&gt;"; echo "&lt;td align=\"center\" valign=\"middle\" width=\"25%\"&gt;$image&lt;/td&gt;"; echo '&lt;/tr&gt;'; } echo '&lt;/table&gt;'; // Generate navigational page links if we have more than one page if ($num_pages &gt; 1) { echo generate_page_links($user_search, '', $cur_page, $num_pages); } mysqli_close($dbc); ?&gt; &lt;?php include("footer.php");?&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload