📄 search2.php
字号:
<?php
session_start();//自己的
function parse($option,$class)
{
$arr=explode("or",$option);
$logic="or";
if(count($arr)==1)
{
$arr=explode("and",$option);
$logic="and";
}
$num=count($arr);
for($i=0;$i<$num;$i++)
{
$arr[$i]=trim(chop($arr[$i]));
}
// if((!strcmp($class,"publishday"))
// {
// return "publishday<\"$arr[0]\" ";
// }
if(!strcmp($class,"downdate"))
{
return "publishday>\"$arr[0]\" ";
}
else if(!strcmp($class,"isbn"))
{
return "isbn=$arr[0]";
}
else if(!strcmp($class,"limit"))
{
return "limit $arr[0]";
}
else if(!strcmp($class,"title"))
{
if(count($arr)==1)
{
return "title like \"%".$arr[0]."%\" ";
}
else
{
$query="title like ";
if(!strcmp($logic,"and"))
{
$query="$query\"%";
for($i=0;$i<count($arr);$i++)
{
$query=$query."$arr[$i]%";
}
$query="$query\"";
}
else
{
$query=$query."\"%".$arr[0]."%\" ";
for($i=1;$i<count($arr);$i++)
{
$query=$query."or\"%".$arr[$i]."%\" ";
}
}
return $query;
}
}
else if(!strcmp($class,"publisher"))
{
if(count($arr)==1)
{
return "fullname like \"%".$arr[0]."%\"";
}
else
{
$query="fullname like ";
if(!strcmp($logic,"and"))
{
$query="$query.\"%";
for($i=0;$i<count($arr);$i++)
{
$query=$query."$arr[$i]%";
}
$query="$query\"";
}
else
{
$query=$query."\"%".$arr[0]."%\"";
for($i=1;$i<count($arr);$i++)
{
$query=$query."or\"%".$arr[$i]."%\"";
}
}
return $query;
}
}
else if(!strcmp($class,"author"))
{
if(count($arr)==1)
{
return "author like \"%".$arr[0]."%\" ";
}
else
{
$query="author like ";
if(!strcmp($logic,"and"))
{
$query="$query.\"%";
for($i=0;$i<count($arr);$i++)
{
$query=$query."$arr[$i]%";
}
$query="$query\"";
}
else
{
$query=$query."\"%".$arr[0]."%\" ";
for($i=1;$i<count($arr);$i++)
{
$query=$query."or\"%".$arr[$i]."%\" ";
}
}
return $query;
}
}
else if(!strcmp($class,"cata"))
{
return "class>$arr[0] and class<".($arr[0]+1000)." ";
}
}
function conndb()
{
$db=@mysql_pconnect("localhost","root","307910");
if($db==false)
{
print "Database failed!";
exit();
}
return $db;
}
function resultempty($cata,$option)
{
print "<tr><td align=center>你的查询条件为:<FONT color=#cc0066>";
print "$cata=$option</FONT></td></tr>";
print "<tr><td align=center>数据库中没有满足条件的记录!</td></tr>";
}
function result($query,$from,$total,$limit)
{
$db=conndb();
print "<tr><td colspan=2 align=center>你的查询条件为:";
print "<FONT color=#cc0066>$query[0]";
if(!empty($limit))
{
print "limit $limit";
}
print "</FONT></td></tr>";
if($total==0)
{
print "<tr><td colspan=2 align=center>数据库里没有满足条件的记录!";
print "</td></tr>";
}
else
{
if(!empty($limit))
{
$min=$limit>$total?$total:$limit;
}
else
{
$min=$total;
}
$step=($min-$from>15)?15:($min-$from);
$res=@mysql_query("set names 'gb2312'");/////////////////////////////////////////////////////////////////////
$res=@mysql_db_query("bookseller","$query[0] limit $from,$step",$db);
print "<tr><td colspan=2>当前记录 $from-.($from+$step-1)";
print "(总有记录 $total,显示 $min)";
for($i=0;$i<$step;$i++)
{
$row=@mysql_fetch_row($res);
print "<tr><td><a href=\"detail.php?isbn=$row[0]\">";
print "$row[1]</a></td>$row[2]</td></tr>\n";
}
if($step<$min)
{
print "<tr><td colspan=2><a href=\"$PHP_SELF?query=$query&";
print "from=0&total=$total&limit=$limit\"><FONT color=#cc0066>";
print "首页</FONT></a> ";
}
if($from>=2*15)
{
print "<a href=\"$PHP_SELF?query=$query&from=";
print ($from-15)."&total=$total&limit=$limit\"><FONT color=#cc0066>";
print "上一页</FONT></a> ";
}
if(($min-$from-15)>0)
{
print "<a href=\"$PHP_SELF?query=$query&from=";
print ($from+15)."&total=$total&limit=$limit\"><FONT color=#cc0066>";
print "下一页</FONT></a> ";
}
if(($from+2*15)<=$min)
{
print "<a href=\"$PHP_SELF?query=$query&from=";
print ($from+$step)."&total=$total&limit=$limit\"><FONT color=#cc0066>";
print "末页</FONT></a> ";
}
print "</td></tr>\n";
}
}
?>
<html><head><title>搜索结果</title>
<meta content="text/html;charset=gb2312" http-equiv=Content-Type></head>
<?
require("index.html.inc");
?>
<body>
<table width="70%" align=center bgcolor=#8cc9ec border=1>
<tbody>
<?
for($i=0,$j=0;$i<count($HTTP_POST_VARS);$i++)
{
$var=each($HTTP_POST_VARS);
if(empty($var[1]))
$j++;
}
if($j==count($HTTP_POST_VARS)-1)
{
print "<table align=center><tbody><tr><td align=center>";
print "<FONT size=4 color=#cc0066>查询条件不能问空!<br>";
print "请填入完整的查询条件然后再开始查询.谢谢合作!";
print "</FONT></td></tr></tbody></table>";
}
else if(isset($from))
{
result($query,$from,$total,$limit);
}
else if(isset($find1))
{
$db=conndb();
if(!empty($option))
{
if(empty($optionid))
{
$optionid=1;
}
$query="select isnd,title,author from books where ";
switch($optionid)
{
case 1:
$s=parse($option,"title");
$query=$query.$s;
$res=@mysql_query("set names 'gb2312'");////////////////////////////////////////////////////////////////
$res=@mysql_db_query("bookseller",$query,$db);
//$res=@mysql_query($query,$db);
// if($res==false)
// {
// echo "对不起,链接错误!";
// exit();
// }
// $res=@mysql_query($query,$db);//
// $num=mysql_num_rows($res);
if(@mysql_num_rows($res)==0)
{
resultempty("书名",$option);
}
else
{
$array[0]=$query;
$num=mysql_num_rows($res);
result($array,0,$num,0);
}
break;
case 2:
$s=parse($option,"author");
$query=$query.$s;
$res=@mysql_query("set names 'gb2312'");/////////////////////////////////////////////////////////////////
$res=@mysql_db_query("bookseller",$query,$db);
if(@mysql_num_rows($res)==0)
{
resultempty("作者",$option);
}
else
{
$array[0]=$query;
$num=mysql_num_rows($res);
result($array,0,$num,0);
}
break;
case 3:
$s=parse($option,"isbn");
$query=$query.$s;
$res=@mysql_query("set names 'gb2312'");////////////////////////////////////////////////////////////////
$res=@mysql_db_query("bookseller",$query,$db);
if(@mysql_num_rows($res)==0)
{
resultempty("ISBN",$option);
}
else
{
$array[0]=$query;
$num=mysql_num_rows($res);
result($array,0,$num,0);
}
break;
case 4:
$s=parse($option,"publisher");
$query="select id from publishers where $s";
$res=@mysql_query("set names 'gb2312'");////////////////////////////////////////////////////
$res=@mysql_db_query("bookseller",$query,$db);
$query="select isnd,title,author from books where publisher in (";
if(@mysql_num_rows($res)!=0)
{
$row=@mysql_fetch_row($res);
$query="$query $rows[0],";
for($i=1;$i<@mysql_num_rows($res);$i++)
{
$row=@mysql_fetch_row($res);
$query="$query,$row[0]";
}
$query="$query )";
$res=@mysql_query("set names 'gb2312'");///////////////////////////////////////////////////////////////
$res=@mysql_db_query("bookseller",$query,$db);
if($num=@mysql_num_rows($res)!=0)
{
$array[0]=$query;
result($array,0,$num,0);
}
else
{
resultempty("出版社",$option);
}
}
else
{
resultempty("出版社",$option);
}
break;
}
}
else
{
print "<table align=center><tbody><tr><td align=center>";
print "<FONT size=4 color=#cc0066>请输入你的查询条件!";
print "</td></tr></tbody></table>\n";
}
}//有问题
else
{
$db=conndb();
$query="select isdn,title,author from books where";
if(!empty($isbn))
{
$query="select title,author from books where";
$a=parse($isbn,"isbn");
if(!empty($limit))
{
$b=parse($limit,"limit");
$query="$query $a $b";
}
else
{
$query=$query.$a;
}
}
else
{
$a;
if(!empty($title))
{
$a=parse($title,"title");
}
if(!empty($author))
{
if(!empty($a))
{
$a="$a and ".parse($author,"author");
}
else
{
$a=parse($author,"author");
}
}
if(!empty($downdate))
{
if(!empty($a))
{
$a="$a and ".parse($downdate,"downdate");
}
else
{
$a=parse($downdate,"downdate");
}
}
if(!empty($update))
{
if(!empty($a))
{
$a="$a and ".parse($update,"update");
}
else
{
$a=parse($update,"update");
}
}
if(!empty($publisher))
{
$b=parse($publisher,"publisher");
$qry="select id from publishers where $b;";
$res=@mysql_query("set names 'gb2312'");////////////////////////////////////////////////////////////////
$res=@mysql_db_query("bookseller",$qry,$db);
if($res==false||@mysql_num_rows($res)==0)
{
print "<table align=center><tbody><tr><td align=center>";
print "<FONT size=4 color=#cc0066>你的查询条件为";
print "出版社 =$publisher<br>在数据库里面找不到相关记录!";
print "</td></tr></tbody></table>\n";
require("foot.html.inc");
exit();
}
$b="publisher in (";
for($i=0;$i<@mysql_num_rows($res)-1;$i++)
{
$row=@mysql_fetch_row($res);
$b="$b$row[0],";
}
$row=@mysql_fetch_row($res);
$b="$b$row[0])";
if(!empty($a))
{
$a="$a and $b";
}
else
{
$a=$b;
}
}
if(!empty($cata))
{
if(!empty($a))
{
$a="$a and ".parse($cata,"cata");
}
else
{
$a=parse($cata,"cata");
}
}
$limitted;
if(!empty($limit))
{
if(!empty($a))
{
print "<table align=center><tbody><tr><td align=center>";
print "<FONT size=4 color=#cc0066>";
print "你还要指出其他一些条件!";
print "</td></tr></tbody></table>\n";
require("foot.html.inc");
exit();
}
$limitted=parse($limit,"limit");
}
$query="$query $a";
$res=@mysql_query("set names 'gb2312'");////////////////////////////////////////////////////////////////////
$res=@mysql_db_query("bookseller",$query,$db);
$num=@mysql_num_rows($res);
$array[0]=$query;
result($array,0,$num,$limit);
}
}
?>
</tbody></table>
</body>
<?
require("foot.html.inc");
?>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -