Chinaunix首页 | 论坛 | 博客
  • 博客访问: 198501
  • 博文数量: 264
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 2740
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-03 13:25
文章分类

全部博文(264)

文章存档

2011年(1)

2009年(263)

我的朋友

分类:

2009-06-03 15:45:22

  1. /*
  2.         Name:                 Search.class.php
  3.         Author:         Genghonghao
  4.         Revisions:        2006/12/16
  5.         功能     :   多条件关联查寻类
  6. */
  7. class Search extends Mysql_Class
  8. {
  9.         /**
  10.          * $dbname:数据库
  11.          * $searchfiled:查询的字段名例如:SELECT username,userage,usersex FROM
  12.          * $termfiled查询的条件的字段,where后面的字段名。例如:如WHERE USERID<>25 AND usersex=1
  13.          * $termvalue查询条件对应的值和条件符号,例如:=@1@,<>@25@,like '%@com@%'等,最之是条件表达式右侧的东西和$termfiled数组一一对应,
  14.          * 且下标必需为数字,通常情况下符号右边可能是变量,所以变量的前后一定要加上'@'符号
  15.          *
  16.          */
  17.         private $dbname;                  
  18.         private $searchfiled = array();  
  19.         private $termfiled = array();   
  20.         private $termvalue = array();

  21.         /**
  22.          * 设置所有属性的值,参数为两个属性和其值
  23.          */
  24.         public function __construct($dbv,$sv,$fiv,$vv)
  25.         {
  26.                 $this->SetPm($dbv,$sv,$fiv,$vv);
  27.         }
  28.         
  29.         /**
  30.          * 设置所有属性的值
  31.          *
  32.          */
  33.         public function SetPm($v1,$v2,$v3,$v4)
  34.         {
  35.                 self::__set('dbname',$v1);
  36.                 self::__set('searchfiled',$v2);
  37.                 self::__set('termfiled',$v3);
  38.                 self::__set('termvalue',$v4);
  39.         }
  40.         // * 设置属性的值的函数
  41.         public function __set($pmname,$value)
  42.         {
  43.                 if(isset($value))
  44.                 {
  45.                         $this->$pmname = $value;
  46.                 }
  47.                 else
  48.                 {
  49.                         return null;
  50.                 }
  51.         }
  52.         
  53.         /**
  54.          * 参数$id是WHERE 后边跟的第一个字段名用来进行第一次判断
  55.          * 参数$value是字段$id的默认值
  56.          * 参数$emblem是条件判断的符号,例如>,<,<>等
  57.          * 参数$last是SQL语句最部分,可以为空
  58.          * */
  59.         public function GetSearch($id,$emblem="<>",$emblemvalue=0,$last="ORDER BY subtime DESC")
  60.         {
  61.                 $termfiled = $this->termfiled;
  62.                 $termvalue = $this->termvalue;
  63.                 $filenum = count($this->termvalue);
  64.                 if(count($this->termfiled) != count($this->termvalue))
  65.                 {
  66.                         return null;
  67.                 }
  68.                 else
  69.                 {
  70.                         $filed = "";
  71.                         foreach($this->searchfiled as $value)
  72.                         {
  73.                                 $filed .= $value.",";
  74.                         }
  75.                         $fnum = strlen($filed);
  76.                         $filed = substr($filed,0,($fnum-1));
  77.                         $sql = "SELECT ";
  78.                         $sql .= $filed;
  79.                         $sql .= " FROM ";
  80.                         $sql .= $this->dbname;
  81.                         $sql .= " WHERE {$id}{$emblem}{$emblemvalue} ";
  82.                         for($i=0;$i<$filenum;$i++)
  83.                         {
  84.                                 //$termvalue[$i] = str_replace(" ","",$termvalue[$i]);
  85.                                 if(!strstr($termvalue[$i],"@@") and !strstr($termvalue[$i],"\"\"") and !strstr($termvalue[$i],"%%"))
  86.                                 {
  87.                                         $termvalue[$i] = str_replace("@","",$termvalue[$i]);
  88.                                         $sql.= "AND {$termfiled[$i]}{$termvalue[$i]} ";
  89.                                 }
  90.                         }
  91.                         if(!empty($last))
  92.                         {
  93.                                 $sql.=$last;
  94.                         }
  95.                         // * Execute为父类中的数据据库操作方法,返回的值是数组!
  96.                         return parent::Execute($sql);
  97.                 }
  98.         }
  99. }
  100. ?>
  101. 实例:
  102. include_once('include/common.inc.php');
  103. include_once('include/Search.class.php');  // 调用这个文件
  104. $table = "tax_codex";  //查寻的数据库表名
  105. $filedd = array('Title','Text'); //要查寻的字段
  106. $term  = array('Click','City','Title'); // 要查寻的条件字段
  107. $termv = array('>@0@','="@上海@"',' LIKE "%@中国@%"'); // 要查寻的条件
  108. $objsql = new Mysql_Class();  
  109. $objsearch = new Search($table,$filedd,$term,$termv);  // 实例化这个类
  110. $print_arr = $objsearch->GetSearch('CodexId',"<>","0","ORDER BY SubTime DESC"); // 调用类中方法行到结果数组
  111. print_r($print_arr); // 打印出数组中的值.
  112. ?>
阅读(185) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~