多个数据库字段的搜索表单


A search form for multiple db fields

实际上,我正试图为一个个人网站创建一个具有单个搜索表单和多个DB字段的搜索表单。如果数据与输入值匹配,则该字段或类似的所有字段都将回显。示例:

我想我的数据库中有5个字段:

$f1, $f2, $f3, $f5

所有这些都包含一些类似字符串的

$f1 = "Hello there I'm f1, look at the outside";
$f2 = "Hello there I'm f2, be brave on you";
$f3 = "Hello there I'm f3,Do great things";
$f4 = "Hello there I'm f4, drink somethings";
$f5 = "Hello there I'm f5, eat somethings";

现在假设$search是一个输入字段,我们将搜索存储在这些变量上的所有数据。如果我们只在输入html字段上键入look at the outside这个字符串,那么$f1将返回$f1的整个数据,但如果我们用Hello there I'm这个字符串搜索,那么它将echo我们的所有变量,因为在所有变量中,这个字符串都可用。我认为这些例子将帮助你正确理解我的要求。

工作中面临的问题:

搜索数据,如果匹配任何数据,则显示所有字段,因为我在foreach循环中为所有字段使用了php的echo关键字。

此处的代码段:

<?php
 if(isset($_GET['submit_value']) && !empty($_GET['search_input']))
 {
    include("db_config.php");    
    $db = database::getInstance();
    $searchValue = $_GET['search_input'];
    $sql = "SELECT * FROM `sitedata` WHERE c_main_title LIKE '%$searchValue%' OR c_main_body LIKE '%$searchValue%' OR cmt_dept_title LIKE '%$searchValue%' OR cmt_dept_body LIKE '%$searchValue%' OR aidt_dept_title LIKE '%$searchValue%' OR aidt_dept_body LIKE '%$searchValue%' OR food_dept_title LIKE '%$searchValue%' OR food_dept_body LIKE '%$searchValue%' OR rac_dept_title LIKE '%$searchValue%' OR rac_dept_body LIKE '%$searchValue%' OR p_message_title LIKE '%$searchValue%' OR p_message_body LIKE '%$searchValue%' OR vp_message_title LIKE '%$searchValue%' OR vp_message_body LIKE '%$searchValue%' OR about_college_title LIKE '%$searchValue%' OR about_college_body LIKE '%$searchValue%' OR mission_message LIKE '%$searchValue%' OR vision_message LIKE '%$searchValue%' ";    
// Now i want to match fields with my $searchValue var  if fields match with input data then that fields will echo. otherwise won't.
    $result = $db->dbc->query($sql);
    foreach( $result as $row)
    {
        // Now i need to use condition here to show my data which match with input field.
        // like $field1 = " hello world this is me" and user input value = "hello world"; then this will show up!
        echo "<h2 class='all-header'>".$row['c_main_title']."</h2>";
        echo $row['c_main_body'];
        echo "<h2 class='all-header'>".$row['cmt_dept_title']."</h2>";
        echo $row['cmt_dept_body'];
        echo "<h2 class='all-header'>".$row['aidt_dept_title']."</h2>";
        echo $row['aidt_dept_body'];
        echo "<h2 class='all-header'>".$row['food_dept_title']."</h2>";
        echo $row['food_dept_body'];
        echo "<h2 class='all-header'>".$row['p_message_title']."</h2>";
        echo $row['p_message_body'];
        echo "<h2 class='all-header'>".$row['vp_message_title']."</h2>";
        echo $row['vp_message_body'];
        echo "<h2 class='all-header'>".$row['about_college_title']."</h2>";
    }                                        
 }
 else { echo "<h2 class='not-found'>404 not found !</h2>";}
?>

您可以在显示每个字段或需要显示的任何内容(第一个字段的代码如下)之前使用

if(strpos($row['c_main_title'], $searchValue) !== false || strpos($row['c_main_body'], $searchValue) !== false){
    echo "<h2 class='all-header'>".$row['c_main_title']."</h2>";
    echo $row['c_main_body'];
}