关于多表搜索解决! 重新构建一个搜索php(index1.php和result/index1.php) 把需要多表搜索的action指向index1.php其它指向默认index.php 我这个里只能按标题,作者,时间,简介这几字段并且是没有info,movie,shop,photo这个表内容,如需要请自己加入
请大家支持我的网站一下,www.xcpet.com index1.php见如下内容: <?php require("../class/connect.php"); include("../class/db_sql.php"); include("../class/config.php"); include("../class/class.php"); include("../class/q_functions.php"); $link=db_connect(); $empire=new mysqlquery(); $ip=egetip(); $searchtime=time(); $getvar=$_POST['getvar']; if(empty($getvar)) { $getfrom="history.go(-1)"; } else { $getfrom="../../search"; $dogetvar="&getvar=1"; } //搜索用户组 if($public_r['searchgroupid']) { $psearchgroupid=$public_r['searchgroupid']; @include("../class/MemberLevel.php"); $searchgroupid=(int)getcvar('mlgroupid'); if($level_r[$searchgroupid][level]<$level_r[$psearchgroupid][level]) { printerror("NotLevelToSearch",$getfrom,1); } } $add=""; $addprice=""; $keyboard=trim($_POST['keyboard']); $s_tbname=RepPostVar($_POST['tbname']); $s_tempid=(int)$_POST['tempid']; $classid=(int)$_POST['classid']; $searchclass=$_POST['show']; $myorder=(int)$_POST['myorder']; $orderby=(int)$_POST['orderby']; $starttime=RepPostVar($_POST['starttime']); if(empty($starttime)) {$starttime="0000-00-00";} $endtime=RepPostVar($_POST['endtime']); if(empty($endtime)) {$endtime="0000-00-00";} $startprice=(int)$_POST['startprice']; $endprice=(int)$_POST['endprice']; //搜索间隔 $lastsearchtime=getcvar('lastsearchtime'); if($lastsearchtime) { if($searchtime-$lastsearchtime<$public_r[searchtime]) {printerror("SearchOutTime",$getfrom,1);} } $len=strlen($keyboard); if(empty($keyboard)) {printerror("EmptyKeyboard",$getfrom,1);} //关键字限制 if($len<$public_r[min_keyboard]||$len>$public_r[max_keyboard]) {printerror("MinKeyboard",$getfrom,1);} $keyboard=addslashes(stripSlashes($keyboard)); //时间范围 if($endtime!="0000-00-00") { $starttimea=$starttime." 00:00:00"; $endtimea=$endtime." 11:59:59"; $add=" and newstime>='$starttimea' and newstime<='$endtimea'"; } //价格 if($endprice) { $addprice=" and price>=".$startprice." and price<=".$endprice; } if($classid) { //终极栏目 if($class_r[$classid][islast]) { $add.=" and classid='$classid'"; } else { $where=ReturnClass($class_r[$classid][sonclass]); $add.=" and (".$where.")"; } $tbname=$class_r[$classid][tbname]; $modid=$class_r[$classid][modid]; } elseif($s_tbname) {
$tbnamenum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewstable where tbname='$s_tbname' limit 1"); if(!$tbnamenum) { Header("Location:result/index1.php?searchid=0".$dogetvar); exit(); } //修改此处的表 $tbname=" (select classid,newspath,filename,title,newstime,checked,smalltext,writer from xcpetecms_news union select classid,newspath,filename,title,newstime,checked,softsay as smalltext,softwriter as writer from xcpetecms_download union select classid,newspath,filename,title,newstime,checked,flashsay as smalltext,flashwriter as writer from xcpetecms_flash union select classid,newspath,filename,title,newstime,checked,smalltext,writer from xcpetecms_article) a "; //$tbname=$s_tbname; //模型id $thestemp_r=$empire->fetch1("select modid from ".GetTemptb("enewssearchtemp")." where tempid='$s_tempid'"); if(empty($thestemp_r['modid'])) { Header("Location:result/index1.php?searchid=0".$dogetvar); exit(); } $modid=$thestemp_r['modid']; } else { //修改此处的表 $tbname=" (select classid,newspath,filename,title,newstime,checked,smalltext,writer from xcpetecms_news union select classid,newspath,filename,title,newstime,checked,softsay as smalltext,softwriter as writer from xcpetecms_download union select classid,newspath,filename,title,newstime,checked,flashsay as smalltext,flashwriter as writer from xcpetecms_flash union select classid,newspath,filename,title,newstime,checked,smalltext,writer from xcpetecms_article) a ";
//$tbname=$public_r['tbname']; } //表不存在 if(empty($tbname)) { Header("Location:result/index1.php?searchid=0".$dogetvar); exit(); } //搜索字段 if(empty($searchclass)||strstr($searchclass," ")) { Header("Location:result/index1.php?searchid=0".$dogetvar); exit(); } //取得字段 if(empty($class_r[$classid][searchtempid])) { if(empty($modid)) { $tempr=$empire->fetch1("select modid from ".GetTemptb("enewssearchtemp")." where isdefault=1 limit 1"); } else { $tempr[modid]=$modid; } } else { $tempr[modid]=$modid; //$tempr=$empire->fetch1("select modid from ".GetTemptb("enewssearchtemp")." where tempid='".$class_r[$classid][searchtempid]."' limit 1"); } //完全 $asame=""; $sfsql=" like '%[!--key--]%'"; if($_POST['allsame']==1) { $asame=1; $sfsql="='[!--key--]'"; } $mr=$empire->fetch1("select searchvar,tbname from {$dbtbpre}enewsmod where mid='$tempr[modid]'"); $fr=explode(",",$searchclass); $sf=""; for($j=0;$j<count($fr);$j++) { //验证参数是否正确 if(!strstr($mr[searchvar],",".$fr[$j].",")) { continue; } if(empty($newsearchclass)) { $or=""; $dh=""; } else { $or=" or "; $dh=","; } $newsearchclass.=$dh.$fr[$j]; $sf.=$or.$fr[$j].$sfsql; } $searchclass=$newsearchclass; //是否包含价格 if(!strstr($mr[searchvar],",price,")) { $addprice=""; $startprice=0; $endprice=0; } //参数错 if(empty($sf)) { Header("Location:result/index1.php?searchid=0".$dogetvar); exit(); } //---------------多个关键字 $kr=explode(" ",$keyboard); for($i=0;$i<count($kr);$i++) { if(empty($kr[$i])) {continue;} $add.=" and (".str_replace("[!--key--]",$kr[$i],$sf).")"; } //验证码 if($s_tempid) { $mtempid=$s_tempid; } else { $mtempid=""; } $checkpass=md5($keyboard.$searchclass.$classid.$starttime.$endtime.$startprice.$endprice.$s_tbname.$mtempid.$asame); $search_r=$empire->fetch1("select searchid,searchtime from {$dbtbpre}enewssearch where checkpass='$checkpass' limit 1"); $todaytime=date("Y-m-d H:i:s"); $query="select count(*) as total from ".$tbname." where checked=1".$add.$addprice; $searchid=$search_r[searchid]; //是否有历史记录 if($searchid) { $search_num=$empire->gettotal($query); $sql=$empire->query("update {$dbtbpre}enewssearch set searchtime=$searchtime,result_num=$search_num,onclick=onclick+1,orderby=$orderby,myorder=$myorder,tempid=$s_tempid,allsame='$asame' where searchid='$searchid'"); if(empty($search_num)) {$searchid=0;} } else { $search_num=$empire->gettotal($query); if(empty($search_num)) {$searchid=0;} else { $sql=$empire->query("insert into {$dbtbpre}enewssearch(searchtime,keyboard,searchclass,result_num,searchip,classid,onclick,orderby,myorder,starttime,endtime,startprice,endprice,checkpass,tbname,tempid,allsame) values($searchtime,'$keyboard','$searchclass',$search_num,'$ip',$classid,1,$orderby,$myorder,'$starttime','$endtime',$startprice,$endprice,'$checkpass','$s_tbname',$s_tempid,'$asame')"); $searchid=$empire->lastid(); } } //设置最后搜索时间 $set1=esetcookie("lastsearchtime",$searchtime,$searchtime+3600*24); db_close(); $empire=null; Header("Location:result/index1.php?searchid=$searchid".$dogetvar); ?>
result/index1.php内容如下:
<?php require("../../class/connect.php"); include("../../class/db_sql.php"); include("../../class/config.php"); include("../../class/class.php"); include("../../class/q_functions.php"); include "../".LoadLang("pub/fun.php"); $editor=1; $link=db_connect(); $empire=new mysqlquery(); $getvar=$_GET['getvar']; if(empty($getvar)) { $getfrom="history.go(-1)"; } else { $getfrom="../../../search"; } //搜索结果 $searchid=(int)$_GET['searchid']; if(empty($searchid)) {printerror("SearchNotRecord",$getfrom,1);} $page=(int)$_GET['page']; $start=(int)$_GET['start']; $search="&searchid=".$searchid; $add=""; $page_line=$public_r['search_pagenum'];//每页显示链接数 $line=$public_r['search_num'];//每页显示记录数 $offset=$start+$page*$line;//总偏移量 $search_r=$empire->fetch1("select searchid,keyboard,result_num,classid,searchclass,orderby,myorder,starttime,endtime,startprice,endprice,tbname,tempid,allsame from {$dbtbpre}enewssearch where searchid='$searchid'"); if(empty($search_r['searchid'])) {printerror("SearchNotRecord",$getfrom,1);} //时间范围 if($search_r[endtime]!="0000-00-00") { $starttime=$search_r[starttime]." 00:00:00"; $endtime=$search_r[endtime]." 11:59:59"; $add=" and newstime>='$starttime' and newstime<='$endtime'"; } //价格 if($search_r[endprice]) { $add.=" and price>=".$search_r[startprice]." and price<=".$search_r[endprice]; } $have_class=1; if($search_r[classid]) { //终极栏目 if($class_r[$search_r[classid]][islast]) { $add.=" and classid='$search_r[classid]'"; $have_class=0; } else { $where=ReturnClass($class_r[$search_r[classid]][sonclass]); $add.=" and (".$where.")"; } $tbname=$class_r[$search_r[classid]][tbname]; $modid=$class_r[$search_r[classid]][modid]; } elseif($search_r['tbname']) { //修改此处的表 $tbname=" (select classid,newspath,filename,title,newstime,checked,smalltext,writer from xcpetecms_news union select classid,newspath,filename,title,newstime,checked,softsay as smalltext,softwriter as writer from xcpetecms_download union select classid,newspath,filename,title,newstime,checked,flashsay as smalltext,flashwriter as writer from xcpetecms_flash union select classid,newspath,filename,title,newstime,checked,smalltext,writer from xcpetecms_article) a "; } else { //修改此处的表 $tbname=" (select classid,newspath,filename,title,newstime,checked,smalltext,writer from xcpetecms_news union select classid,newspath,filename,title,newstime,checked,softsay as smalltext,softwriter as writer from xcpetecms_download union select classid,newspath,filename,title,newstime,checked,flashsay as smalltext,flashwriter as writer from xcpetecms_flash union select classid,newspath,filename,title,newstime,checked,smalltext,writer from xcpetecms_article) a "; } //完全 $sfsql=" like '%[!--key--]%'"; if($search_r['allsame']) { $sfsql="='[!--key--]'"; } //搜索字段 $fr=explode(",",$search_r[searchclass]); $searchfield=$search_r[searchclass]; for($j=0;$j<count($fr);$j++) { if($j==0) {$or="";} else {$or=" or ";} $sf.=$or.$fr[$j].$sfsql; } //---------------多个关键字 $kr=explode(" ",$search_r[keyboard]); $keyboard=$search_r[keyboard]; for($i=0;$i<count($kr);$i++) { if(empty($kr[$i])) {continue;} $add.=" and (".str_replace("[!--key--]",$kr[$i],$sf).")"; } if($search_r[orderby]==1)//信息ID {$myorder="id";} elseif($search_r[orderby]==2)//评论数 {$myorder="plnum";} elseif($search_r[orderby]==3)//点击率 {$myorder="onclick";} elseif($search_r[orderby]==4)//下载数 {$myorder="totaldown";} else//时间 {$myorder="newstime";} if(empty($search_r[myorder])) {$myorder.=" desc";} //当前时间 $todaytime=date("Y-m-d H:i:s"); $query="select * from ".$tbname." where checked=1".$add; $num=$search_r[result_num]; $query.=" order by ".$myorder." limit $offset,$line"; $sql=$empire->query($query); $listpage=page1($num,$line,$page_line,$start,$page,$search); //取得模板 if($search_r['tempid']) { $tempr=$empire->fetch1("select temptext,subnews,listvar,rownum,showdate,modid,subtitle from ".GetTemptb("enewssearchtemp")." where tempid='".$search_r['tempid']."' limit 1"); } elseif(empty($class_r[$search_r[classid]][searchtempid])) { $tempr=$empire->fetch1("select temptext,subnews,listvar,rownum,showdate,modid,subtitle from ".GetTemptb("enewssearchtemp")." where isdefault=1 limit 1"); } else { $tempr=$empire->fetch1("select temptext,subnews,listvar,rownum,showdate,modid,subtitle from ".GetTemptb("enewssearchtemp")." where tempid='".$class_r[$search_r[classid]][searchtempid]."' limit 1"); } //替换公共模板变量 $listtemp=$tempr[temptext]; if($public_r['searchtempvar']) { $listtemp=ReplaceTempvar($listtemp); } $listtemp=str_replace("[!--show.page--]",$listpage,stripSlashes($listtemp)); $listtemp=str_replace("[!--keyboard--]",$keyboard,$listtemp); $listtemp=str_replace("[!--ecms.num--]",$num,$listtemp); $listtemp=str_replace("[!--news.url--]",$public_r[newsurl],$listtemp); $listtemp=str_replace("[!--pagetitle--]",$fun_r['adsearch'],$listtemp); $rownum=$tempr[rownum]; if(empty($rownum)) {$rownum=1;} $formatdate=$tempr[showdate]; $subnews=$tempr[subnews]; $subtitle=$tempr[subtitle]; $modid=$tempr[modid]; $listvar=stripSlashes($tempr[listvar]); //字段 $ret_r=ReturnAddF($tempr[modid],2); $field=$ret_r[0]; //取得列表模板 $list_exp="[!--empirenews.listtemp--]"; $list_r=explode($list_exp,$listtemp); $listtext=$list_r[1]; $no=$offset; $changerow=1; while($r=$empire->fetch($sql)) { //替换列表变量
$r[title]=str_replace($search_r[keyboard],"<font color='red'><B>$search_r[keyboard]</B></font>",$r[title]); $r[smalltext]=str_replace($search_r[keyboard],"<font color='red'><B>$search_r[keyboard]</B></font>",$r[smalltext]); $repvar=ReplaceListVars($no,$listvar,$subnews,$subtitle,$formatdate,$url,$have_class,$r,$field); $listtext=str_replace("<!--list.var".$changerow."-->",$repvar,$listtext); $changerow+=1; //超过行数 if($changerow>$rownum) { $changerow=1; $string.=$listtext; $listtext=$list_r[1]; } $no++; } db_close(); $empire=null; //多余数据 if($changerow<=$rownum&&$listtext<>$list_r[1]) { $string.=$listtext; } $string=$list_r[0].$string.$list_r[2]; echo $string; ?>
|