简体   繁体   中英

How to filter search in PHP

Please help, the code is working to search name but i want to filter aside from FirstName and LastName i want to limit the search for High School only (Habay Elementary School, Dasma National High School, Alab High School).

<?php
$dbhost = "localhost";$dbname = "do";$dbuser = "root";$dbpass = "usbw";         
//Connection
global $tutorial_db;
$tutorial_db = new mysqli();
$tutorial_db->connect($dbhost, $dbuser, $dbpass, $dbname);
$tutorial_db->set_charset("utf8");
//  Check Connection
if ($tutorial_db->connect_errno) {
    printf("Connect failed: %s\n", $tutorial_db->connect_error);
exit();
}
// Define Output HTML Formating
$html = '';
$html .= '<li class="result">';
$html .= '<a href="urlString">';
$html .= '<h3>nameString</h3>';
$html .= '<h4>functionString</h4>';
$html .= '</a>';
$html .= '</li>';
// Get Search
$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search_string = $tutorial_db->real_escape_string($search_string);
// Check Length More Than One Character
if (strlen($search_string) >= 1 && $search_string !== ' ') {
// Build Query
 $query = 'SELECT * FROM personneldo WHERE FirstName LIKE  "%'.$search_string.'%" OR LastName LIKE "%'.$search_string.'%"';
// Do Search
$result = $tutorial_db->query($query);
while($results = $result->fetch_array()) {
$result_array[] = $results;
}
// Check If We Have Results
if (isset($result_array)) {
foreach ($result_array as $result) {

if ($type='bacoores'){
// Format Output Strings And Hightlight Matches $display_function = preg_replace("/".$search_string."/i", "<b  class='highlight'>".$search_string." </b>", $result['FirstName']);
$display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['LastName']);
$display_url = '/bacoor/login/JVSearchOutput.php?id='.urlencode($result['studid']);
// Insert Name
$output = str_replace('nameString', $display_name, $html);
// Insert Function
$output = str_replace('functionString', $display_function, $output);
// Insert URL
$output = str_replace('urlString', $display_url, $output);
// Output
echo($output);
 }   }
}else{
// Format No Results Output
$output = str_replace('urlString', 'javascript:void(0);', $html);
$output = str_replace('nameString', '<b>No Results Found.</b>', $output);
$output = str_replace('functionString', 'Sorry :(', $output);
// Output
echo($output);
}
}?>

Add School parameter in your Where clause.

$query = "SELECT * FROM personneldo 
                        WHERE (FirstName LIKE  '%$search_string%' 
                        OR LastName LIKE '%$search_string%')
                        And (School = 'High School')";

Explanation:

1) Your SQL searches for those records who have search string anywhere (in the beginning/middle/end) in the field value.

2) You were already searching for FirstName and LastName , School was missing.

$query = "SELECT * FROM personneldo 
          WHERE FirstName LIKE '%$search_string%' 
             OR LastName  LIKE '%$search_string%' 
             OR School    LIKE '%$search_string%'";

Assuming the field name is School

One solution would be to create another column in your table that contained the type of the school such as High School or Elementary School and then a simple query like this would work.

SELECT * FROM people WHERE type='High School'

However to answer your question you could use the LIKE SQL Operator such as below

SELECT * FROM people WHERE school LIKE '%High School%'

As it says on W3schools

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

More information here

http://www.w3schools.com/sql/sql_like.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM