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