简体   繁体   English

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

[英]A search form for multiple db fields

Actually, I'm trying to to create a search form for a personal website that have single search form and multiple DB fields. 实际上,我正在尝试为具有单一搜索表单和多个数据库字段的个人网站创建搜索表单。 if data match with input value then that field or similar all fields will echo . 如果数据与输入值匹配,则该字段或类似的所有字段将回显。 examples: 例子:

guess i have 5 fields in my db: 我的数据库中有5个字段:

$f1, $f2, $f3, $f5

and all of them containing some string like 并且所有这些都包含一些字符串

$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";

Now guess $search is a input field there we will search all data stored on those variable. 现在猜猜$search是一个输入字段,我们将搜索存储在那些变量上的所有数据。 If we type look at the outside this string only on our input html field then $f1 will echo the whole data of $f1 but if we search with Hello there I'm this strings then it will echo our all of variables because in all of them this strings available. 如果我们键入look at the outside这个字符串只有在我们输入HTML字段,然后$f1会响应的整个数据$f1 ,但如果我们用搜索Hello there I'm这个字符串,然后它会echo我们所有的变量,因为在所有的他们这个字符串可用。 I think that examples will help you to understand properly what my requirement actually. 我认为这些例子可以帮助您正确理解我的要求。

problems facing on working: 工作面临的问题:

searching data if match any data then all fields are showing as i used echo keyword of php on foreach loop for all fields. 搜索数据是否匹配任何数据然后显示所有字段,因为我在所有字段的foreach循环上使用了php的echo关键字。

Snippets here: 片段在这里:

<?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>";}
?>

You can use this before displaying each field or whatever you need to display (below code for first field) 您可以在显示每个字段或需要显示的任何内容之前使用它(在第一个字段的代码下面)

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'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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