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.
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 '%{$Sname}%' ";//模糊查询 } ?>
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='color:red'>{$Sname}</span>",$v[1]);//用span标签使关键字变为红色, echo "<tr> <td>{$str}</td> <td>{$v[2]}</td> <td>{$v[4]}</td> </tr>"; } ?>
Final result:
## 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 '%{$name}%' "; }if(!empty($_POST["brand"])) { $brand = $_POST["brand"]; $tj2 = " brand = '{$brand}' "; }//总条件$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='color:red'>{$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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools
