lily 2007-8-2 13:36
两行代码搞定多级关联搜索
<p><?php<BR>/*<BR>Name: Search.class.php<BR>Author: Genghonghao<BR>Revisions: 2006/12/16<BR>功能 : 多条件关联查寻类<BR>*/<BR>class Search extends Mysql_Class <BR>{<BR>/**<BR>* dbname:数据库名<BR>* searchfiled:查询的字段名例如:SELECT username,userage,usersex FROM<BR>* termfiled查询的条件的字段,where后面的字段名。例如:如WHERE USERID<>25 AND usersex=1<BR>* termvalue查询条件对应的值和条件符号,例如:=@1@,<>@25@,like '%@com@%'等,最之是条件表达式右侧的东西和termfiled数组一一对应,<BR>* 且下标必需为数字,通常情况下符号右边可能是变量,所以变量的前后一定要加上'@'符号<BR>* <BR>*/<BR>private dbname; <BR>private searchfiled = array(); <BR>private termfiled = array(); <BR>private termvalue = array();<BR><BR>/**<BR>* 设置所有属性的值,参数为两个属性和其值<BR>*/<BR>public function __construct(dbv,sv,fiv,vv)<BR>{<BR>this->SetPm(dbv,sv,fiv,vv);<BR>}<BR><BR>/**<BR>* 设置所有属性的值<BR>* <BR>*/<BR>public function SetPm(v1,v2,v3,v4)<BR>{<BR>self::__set('dbname',v1);<BR>self::__set('searchfiled',v2);<BR>self::__set('termfiled',v3);<BR>self::__set('termvalue',v4);<BR>}<BR>// * 设置属性的值的函数<BR>public function __set(pmname,value)<BR>{<BR>if(isset(value))<BR>{<BR>this->pmname = value;<BR>}<BR>else <BR>{<BR>return null;<BR>}<BR>}<BR><BR>/**<BR>* 参数id是WHERE 后边跟的第一个字段名用来进行第一次判断<BR>* 参数value是字段id的默认值<BR>* 参数emblem是条件判断的符号,例如>,<,<>等<BR>* 参数last是SQL语句最部分,可以为空<BR>* */<BR>public function GetSearch(id,emblem="<>",emblemvalue=0,last="ORDER BY subtime DESC")<BR>{<BR>termfiled = this->termfiled;<BR>termvalue = this->termvalue;<BR>filenum = count(this->termvalue);<BR>if(count(this->termfiled) != count(this->termvalue))<BR>{<BR>return null;<BR>}<BR>else <BR>{<BR>filed = "";<BR>foreach(this->searchfiled as value)<BR>{<BR>filed .= value.",";<BR>}<BR>fnum = strlen(filed);<BR>filed = substr(filed,0,(fnum-1));<BR>sql = "SELECT ";<BR>sql .= filed;<BR>sql .= " FROM ";<BR>sql .= this->dbname;<BR>sql .= " WHERE {id}{emblem}{emblemvalue} ";<BR>for(i=0;i<filenum;i++)<BR>{<BR>//termvalue[i] = str_replace(" ","",termvalue[i]);<BR>if(!strstr(termvalue[i],"@@") and !strstr(termvalue[i],"\"\"") and !strstr(termvalue[i],"%%"))<BR>{<BR>termvalue[i] = str_replace("@","",termvalue[i]);<BR>sql.= "AND {termfiled[i]}{termvalue[i]} ";<BR>}<BR>}<BR>if(!empty(last))<BR>{<BR>sql.=last;<BR>}<BR>// * Execute为父类中的数据据库操作<a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, '%B7%BD%B7%A8');" target="_self"><u><strong>方法</strong></u></a>,返回的值是数组!<BR>return parent::Execute(sql);<BR>}<BR>}<BR>}<BR>?><BR>实例:<BR><?php<BR>include_once('include/common.inc.php');<BR>include_once('include/Search.class.php'); // 调用这个文件<BR>table = "tax_codex"; //查寻的<a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, '%CA%FD%BE%DD%BF%E2');" target="_self"><u><strong>数据库</strong></u></a>表名<BR>filedd = array('Title','Text'); //要查寻的字段<BR>term = array('Click','City','Title'); // 要查寻的条件字段<BR>termv = array('>@0@','="@上海@"',' LIKE "%@中国@%"'); // 要查寻的条件<BR>objsql = new Mysql_Class(); <BR>objsearch = new Search(table,filedd,term,termv); // 实例化这个类<BR>print_arr = objsearch->GetSearch('CodexId',"<>","0","ORDER BY SubTime DESC"); // 调用类中方法行到结果数组<BR>print_r(print_arr); // 打印出数组中的值.<BR>?></p> <center><input type="image" onclick=copyToClipBoard() src="http://www.phpchina.com/images/phpcn_book_bu_tj.gif" border="0"></center>