Home >Backend Development >PHP Problem >How to implement conditional query in php

How to implement conditional query in php

藏色散人
藏色散人Original
2020-10-15 11:12:465559browse

How to implement conditional query in php: first call the previously encapsulated class; then use keyword fuzzy query; then create a form, submit the data to the current page, extract the keyword query; and finally use PHP code Traverse the elements in the table and turn the keywords into red.

How to implement conditional query in php

Recommended: "PHP Video Tutorial"

1. Single condition Query is a query with only one condition:

1. First call the previously encapsulated class, and then use keywords to fuzzy query:

<?php //单查询
require "DBDA.class.php";//调取封装类
$db=new DBDA;
$Sname="";//创建变量,为了后面可以让Sname在表单中显示
$sql="select * from t_student";

if(!empty($_POST["Sname"]))//确定是否存在数据
{
    $Sname=$_POST["Sname"];
    $sql="select * from t_student where Sname like &#39;%{$Sname}%&#39; ";//模糊查询
}
?>

2. Create a form, submit the data to the current page, and extract the keyword query:

<form action="chaxun.php" method="post"><!--因为查询数据在当前页面,所以提交到当前页面-->
<div>姓名:<input type="text" name="Sname" value="<?php echo $Sname ?>" />
<input type="submit" value="查询" /></div>
</form><br />

<table width="100% " border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>姓名</td>
        <td>性别</td>
        <td>班级</td>
    </tr>

3. Traverse the elements in the table and turn the keywords into red:

<?php
$arr=$db->query($sql);
foreach($arr as $v)
{
    $str = str_replace($Sname,"<span style=&#39;color:red&#39;>{$Sname}</span>",$v[1]);//用span标签使关键字变为红色,
    echo "<tr>
        <td>{$str}</td>
        <td>{$v[2]}</td>
        <td>{$v[4]}</td>
        </tr>";    
}
?>

Final result:

How to implement conditional query in php

## 2. Multi-condition query:

1. First make a form and create a table to display the table:

<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
    <td>代号</td>
    <td>名称</td>
    <td>系列</td>
    <td>上市时间</td>
    <td>价格</td>
</tr>
</table>

2. Call the encapsulation class, create corresponding conditions, and check whether the data is empty :

<?php
require "DBDA.class.php";
$db=new DBDA();//1.如果没有提交数据,显示所有//2.如果有提交数据,根据关键字查询显示$name="";
$tj1=" 1=1 "; //第一个条件,对应名称,要用空格隔开$tj2=" 1=1 ";//第二个条件,对应系列,要用空格隔开if(!empty($_POST["name"]))
{
    $name = $_POST["name"];
    $tj1 = " name like &#39;%{$name}%&#39; ";
}if(!empty($_POST["brand"]))
{
    $brand = $_POST["brand"];
    $tj2 = " brand = &#39;{$brand}&#39; ";
}//总条件$tj=" {$tj1}and{$tj2} ";
$sql="select * from car where".$tj;?>

3. Use PHP code to traverse the database table and turn the keywords into red (php should be embedded in the table):

<?php
$arr = $db->query($sql);foreach($arr as $v)
{
    $str = str_replace($name,"<span style=&#39;color:red&#39;>{$name}</span>",$v[1]);
    echo "<tr>
    <td>{$v[0]}</td>
    <td>{$str}</td>
    <td>{$v[2]}</td>
    <td>{$v[3]}</td>
    <td>{$v[7]}</td>
</tr>";}?>

The final result is:

How to implement conditional query in php

The above is the detailed content of How to implement conditional query in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn