search
HomeBackend DevelopmentPHP ProblemHow to implement conditional query in php

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SecLists

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

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

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

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools