实际上,我正在尝试为具有单一搜索表单和多个数据库字段的个人网站创建搜索表单。如果数据与输入值匹配,则该字段或类似的所有字段将回显。 示例:
我猜我的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
1}}我们所有的变量,因为在所有变量中都有这个字符串可用。我认为这些例子可以帮助您正确理解我的要求。
工作面临的问题:
搜索数据是否匹配任何数据然后显示所有字段,因为我在所有字段的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>";}
?>
答案 0 :(得分:0)
您可以在显示每个字段或需要显示的任何内容之前使用此功能(在第一个字段的代码下方)
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'];
}