条件循环
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <form action="Untitled-1.php" method="post"> <div> 汽车名称 <input type="text" name="name" /> <input type="submit" value="查询" /> </div> </form> <br /> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>名称</td> <td>系列</td> <td>时间</td> <td>油耗</td> <td>功率</td> </tr> <?php include("dbda.class.php"); $db=new dbda(); $tj=""; $name=""; if(!empty($_POST)) { $name=$_POST[‘name‘]; $tj="where name like ‘%{$_POST[‘name‘]}%‘"; } $sql="select * from car ".$tj; $attr=$db->Query($sql); foreach($attr as $v) { //处理关键字 $v[1];$name; $re="<span style=‘color:red‘>{$name}</span>"; $str=str_replace($name,$re,$v[1]); echo "<tr> <td>{$v[0]}</td> <td>{$str}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> </tr>"; } ?> </table> </body> </html>
多条件查询
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <form action="chaxun.php" method="post"> <div> 汽车名称 <input type="text" name="name" /> 系列代号 <input type="text" name="brand" /> <input type="submit" value="查询" /> </div> </form> <br /> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>名称</td> <td>系列</td> <td>时间</td> <td>油耗</td> <td>功率</td> </tr> <?php //一般来说做查询的时候:用户不输入任何内容就是查询所有的 include("dbda.class.php"); $db=new dbda(); //多条件查询 $tj1="1=1"; $tj2="1=1"; $name=""; if(!empty($_POST)) { if($_POST[‘name‘]!="") { $name=$_POST[‘name‘]; $tj1=" name like ‘%{$_POST[‘name‘]}%‘"; } if($_POST[‘brand‘]!="") { $tj2="brand=‘{$_POST[‘brand‘]}‘"; } } $cxtj=" where ".$tj1." and ".$tj2; $sql="select * from car ".$cxtj; echo $sql; $attr=$db->Query($sql); foreach($attr as $v) { $v[1];$name; $re="<span style=‘color:red‘>{$name}</span>"; $str=str_replace($name,$re,$v[1]); echo "<tr> <td>{$v[0]}</td> <td>{$str}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> </tr>"; } ?> </table> </body> </html>
时间: 2024-10-17 19:36:45