码迷,mamicode.com
首页 > 其他好文 > 详细

实例三搜索

时间:2016-06-16 23:05:35      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

单条件搜索(关键字不变色显示结果)

<!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="sousuo.php" method="post" >
<input  type="text" name="name"  /><input type="submit" value="查询"/>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
   <tr>
   <td>代号</td>
   <td>名称</td>
   <td>系列</td>
   <td>上市时间</td>
   <td>油耗</td>
   <td>功率</td>
   </tr>
<?php 

$db=new mysqli("localhost","root","","1");
!mysqli_connect_error () or die("连接错误");
$tj="";//判断提交值是否为空empty判断

if(!empty($_POST["name"]))//
{
	$tj="where name like ‘%{$_POST[‘name‘]}%‘";
	
}
//拼接sql语句
$sql="select * from car ".$tj;//表名后面切记加个空格
$result=$db->query($sql);
$a=$result->fetch_all();
foreach($a as $v)
{
	
	echo "  <tr>
   <td>{$v[0]}</td>
   <td>{$v[1]}</td>
   <td>{$v[2]}</td>
   <td>{$v[3]}</td>
   <td>{$v[4]}</td>
   <td>{$v[5]}</td>
   </tr>";
}

?>   
 </table>
 </form>
</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="sousuo.php" method="post" >
<input  type="text" name="name"  /><input type="submit" value="查询"/>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
   <tr>
   <td>代号</td>
   <td>名称</td>
   <td>系列</td>
   <td>上市时间</td>
   <td>油耗</td>
   <td>功率</td>
   </tr>
<?php 

$db=new mysqli("localhost","root","","1");
!mysqli_connect_error () or die("连接错误");
$tj="";//判断提交值是否为空empty判断
$name="";
if(!empty($_POST))//
{
	$tj="where name like ‘%{$_POST[‘name‘]}%‘";
	$name=$_POST["name"];
}
//拼接sql语句
$sql="select * from car ".$tj;//表名后面切记加个空格
$result=$db->query($sql);
$a=$result->fetch_all();
foreach($a 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>
 </form>
</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="sousuo.php" method="post" >
汽车名称
<input  type="text" name="name"  /><input type="submit" value="查询"/>
汽车系列号
<input  type="text" name="brand"  /><input type="submit" value="查询"/>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
   <tr>
   <td>代号</td>
   <td>名称</td>
   <td>系列</td>
   <td>上市时间</td>
   <td>油耗</td>
   <td>功率</td>
   </tr>
<?php 

$db=new mysqli("localhost","root","","1");
!mysqli_connect_error () or die("连接错误");
$tj1="1=1";//判断提交值是否为空empty判断
$tj2="1=1";
$name="";
$brand="";
if(!empty($_POST))
{    
    if($_POST["name"]!="")
	{
	$name=$_POST["name"];
	$tj1 =" name like ‘%{$_POST[‘name‘]}%‘";
	}  
	if($_POST["brand"]!="")
	{
	$brand=$_POST["brand"];
	$tj2=" brand=‘{$_POST[‘brand‘]}‘";
	}
	
}
//拼接sql语句
$cx=" where ".$tj1." and ".$tj2;
$sql=" select * from car ".$cx;//表名后面切记加个空格
$result=$db->query($sql);
$a=$result->fetch_all();
foreach($a as $v)
{
	//处理关键字
	$v[1];$name;//车名关键字变色
	$re="<span style=‘color:red‘>{$name}</span>";
	$str=str_replace($name,$re,$v[1]);
	$v[2];$brand;//型号名关键字变色
	$re1="<span style=‘color:red‘>{$brand}</span>";
	$str1=str_replace($brand,$re1,$v[2]);
	echo "  <tr>
   <td>{$v[0]}</td>
   <td>{$str}</td>
   <td>{$str1}</td>
   <td>{$v[3]}</td>
   <td>{$v[4]}</td>
   <td>{$v[5]}</td>
   </tr>";
}

?>   
 </table>
 </form>
</body>
</html>

 技术分享

实例三搜索

标签:

原文地址:http://www.cnblogs.com/zoubizhici/p/5592147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!