查询搜索列表类封装。



2 how to use

二 使用示例.Demo

 

 

First Step to create the form.And render it.

      

echo   $this->view->render("photo/demo/index2.html");

 
 
 


 form表单源代码

<form action="#"method="post">

  <input type="text"name="QueryLike[CountryCode]"  value=""/> 使用like 查询 CountryCode  配置为 QueryLike[CountryCode]<br/>

  <input type=checkbox name="QueryIn_Country[]" value="Austria"> Austria    <input type=checkbox name="QueryIn_Country[]"value="Ukraine">  Ukraine

  <input type="text"name="QueryDeep[CityName-_-Country]"  value=""/>多值段多值查询,字段用  -_-间隔,值用空格隔开 QueryDeep[CityName-_-Country] <br/>

    <input type="submit"value="submit" />

</form>   

 

Then.

创建对象,设置document 及分页的basePath.

    $me = new Gutils_Glist();

    $document = new Gp_Model_Geography_CityCodes_Document();

    $me->setModelDocument($document);

$me->setPagePath("http://mygoput/photo/demo3/");

 

 

获取数据

    list($destResult,$pager) = $me->get();

    

at last output the data or renderit.

echo $pager->display();

       echo "<br/>";

       echo "<font style ='font-weight:bold'>Country----";

        echo "CityName----";

        echo "ProvinceCode----";

        echo "CountryCode </font><br/><br/>";

       foreach($destResult as $key=>$val):

 

      

       echo $val->Country."----";

        echo $val->CityName."----";

        echo $val->ProvinceCode."----";

        echo $val->CountryCode;

        echo "<hr/>";

        endforeach;

        echo $pager->display();

 

 

 

最终的数据效果。

共12358条记录 1/1236 第一页上一页 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22下一页最后页
Country----CityName----ProvinceCode----CountryCode

Australia----Adelaide----AU-SA----AU


Australia----Albany----AU-WA----AU


Australia----Albury----AU-NSW----AU


Australia----Alice Springs----AU-NT----AU


Australia----Armidale----AU-NSW----AU


Australia----Bacchus Marsh----AU-VIC----AU


Australia----Bairnsdale----AU-VIC----AU


Australia----Ballarat----AU-VIC----AU


Australia----Ballina----AU-NSW----AU


Australia----Barwon----AU-VIC----AU


共12358条记录 1/1236 第一页上一页 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22下一页最后页



/**
 * xListClass
 * @author murain 2011/8/22
 *
 */
class JIM_xlist {
    private $_factory;
    private $_searchHead;  //the searchHeadObject.
    private $_data;
    private $_pager;
    private $_modelName;
    private $CI;
    
    
    
    private $_parseDeep = 3; //url 解析深度
    private $_pageSize = 2;
    private $_pagerPath;
    
    private $_params;
    private $_queryParams;
    private $_searchHeadParams;
    private $_pagerParams;
    

    private $_buttons = "";
    
    

    function JIM_xlist() {
        $this->CI = &get_instance();
        $this->_factory = new ciFactory();
        $this->setParseDeep(3);
    }
    
    function setModelName ($name){
        $this->_modelName = $name;
    }
    
    function getParams(){
        $parseHandle = $this->_factory->createParse($this->_parseDeep);
        $this->_params = $parseHandle->parse();
        //pr($this->_params,"what we get!");
        $queryParamsHandle = $this->_factory->createQueryConvert($this->_params);
        $this->_queryParams = $queryParamsHandle->convert();
        
        
        $pageParamsHandle = $this->_factory->createPagerParamConvert($this->_params);
        $this->_pagerParams = $pageParamsHandle->convert();
        
        $searchHeadHandel = $this->_factory->createSearchHeadConvert($this->_params);
        $this->_searchHeadParams = $searchHeadHandel->convert();
        
        
    //    pr($this->_pagerParams,"pagerParams");
        
        
    }
    
    function get(){
        $model_name = CS_get_model_name($this->_modelName);    //取得模型名称
        $this->CI->load->model($model_name);                     //加载模型.

        $offset = ($this->_pagerParams["page"] - 1) * $this->_pageSize;
        
        $data['rows']  = $this->CI->$model_name->get($this->_queryParams, $this->_pageSize, $offset);
        
        $count = $this->CI->$model_name->get_count($this->_queryParams);
        
        //pr($this->_pagerParams);
        $pageConstructParam = array(
            "recordcount" => $count,    //总页数
            "pagesize"      => $this->_pageSize             //每页显示的记录数
        );
        
         //分页处理
        $this->CI->load->library("pager", $pageConstructParam);
        $this->CI->pager->setPath($this->_pagerPath);
        
        if(isset($this->_buttons) && sizeof($this->_buttons))
        {
            foreach ($this->_buttons as $button)
            {
                $this->CI->pager->addButton($button);
            }
        }
    



        unset($this->_pagerParams['page']);    //删除参数中的分页参数
        $this->CI->pager->setParamters($this->_pagerParams);    //设置分页链接中需要带的参数
        
        $data["pager"] = $this->CI->pager->display();    //取得分页的HTML
        
        $data["config_search"] = CS_get_config_search($this->_modelName);
        
        //pr($this->_searchHeadParams);
        //$data["config_search"] = $this->mergeSearchHead($data["config_search"]);
        return $data;
    }
    
    function setPageButton($buttons)
    {
        $this->_buttons = $buttons;
    }
    
    public function mergeSearchHead($searchItems){
        return $config_search;
    }
    
    
    
    
    /**
     * 设置解析深度
     */
    public function setParseDeep($deep = 3)
    {
        $this->_parseDeep = $deep;
    }
    
    public function setPagerPath($path)
    {
        $this->_pagerPath = $path;
    }
    
    /**
     */
    public function setPageSize($pageSize = 10)
    {
        $this->_pageSize = $pageSize;
        //echo "set the pageSize here";
    }
    
    
    function deBug()
    {
        pr($this->_params,"通过iparse得到的参数信息");
        pr($this->_queryParams,"查询解析参数");
        pr($this->_pagerParams,"分页参数");
        pr($this->_searchHeadParams,"搜索参数参数");
        
    }
    
    /**
     * 测试输出
     *
     * @param string $array
     * @param string type �ṩjson��ʽ��type ��ʽ�����
     * @return no return.
     */
    function pr($data,$title = 'DEBUGINFO',$type = "array")
    {
        echo "<fieldset style=\"border: 1px solid rgb(0, 153, 0); margin: 20px 0pt; padding: 6px 10px 10px; background-color: rgb(238, 238, 238);\">
    <legend style=\"color: rgb(0, 153, 0);\">$title</legend>";
        echo "<div style = 'font-size:14px; color:#000; border:1px solid #666; background:#ccc; padding:5px;'>";
            print("<pre>");
            if($type == "jsonStr") print_r(json_decode($data));
            print_r($data);
            print("</pre>");
        echo "<div>";
        echo  "</fieldset>";
    }    
}


abstract class  xListFactory{
    abstract function createParse($params);
    abstract function createQueryConvert($params);
    abstract function createSearchHeadConvert($params );
    abstract function createPagerParamConvert($params);    
    
    public function createClass($name,$param = '')
    {
        return new $name($param);
    }
    
}



/**
 *
 * Enter description here ...
 * @author Administrator
 *@todo CILoader or ZFLoader
 */
class ciFactory extends xListFactory {
    private $_className;
        
    function __construct() {
    
    }
    
    function createParse($param){
        if( $this->isPost() ){
            return     $this->createClass("ciPostParse");
        }else {
            return     $this->createClass("ciGetParse",$param);
        }
        
    }
    function createQueryConvert($params)
    {
//        pr($params ,"from the factory!");
        return $this->createClass("ciQueryConvert",$params);
    }
    function createSearchHeadConvert($params)
    {
     //   createPagerParamConvert
         return $this->createClass("ciSearchHeadConvert",$params);
    }
    function createPagerParamConvert($params)
    {
       return $this->createClass("ciPagerParamConvert",$params);
    }        
    
    
    
    function isPost(){
            return strtolower($_SERVER['REQUEST_METHOD']) == 'post';
    }
    
    
}


//参数转换算法族。
interface iConvertHandle{
    function convert2Query($key,$val);
    function convert2Page($key,$val);
    function convert2SearchHead($key,$val);
}

/**
 *
 * 等值查询
 * @author murain
 *
 */
class search_equalConvertHandle implements iConvertHandle{
    function convert2Query($key,$val){
        $val = base_decode($val);
         $where="($key = '%".$val."%')";
         return $where;
    }
    function convert2Page($key,$val)
    {
        $pageParam = array( "search_eqaul_$key"=>"$val");
        return $pageParam;
    }
    function convert2SearchHead($key,$val)
    {
        return;
    }
}

/**
 *
 * like 查询.
 * @author Administrator
 *
 */
class search_likeConvertHandle implements iConvertHandle{
    function convert2Query($key,$val){
        $val = base_decode($val);
         $where="($key like '%".$val."%')";
         return $where;
    }
    function convert2Page($key,$val)
    {
        $pageParam = array( "search_like_$key"=>"$val");
        return $pageParam;
    }
    function convert2SearchHead($key,$val)
    {
        return;
    }
}

/**
 * in 查询接口处理.
 * */
class search_multConvertHandle implements iConvertHandle{
    function convert2Query($key,$val){
         $itemArr = explode("_xyx_",$val);
        // $itemArr =  array_map($this->addQuote,$itemArr);
        
         foreach ($itemArr  as $val_piece)
         {
             $strCond []= "$key = '".base_decode($val_piece)."'  ";  //生成查询条件数组.
         }
         $where= "(".implode("or ",$strCond ).")";  //合并生成查询条件.        
         return $where;
    }
    function convert2Page($key,$val)
    {
        $pageParam = array( "search_mult_$key"=>"$val");
        return $pageParam;
    }
    function convert2SearchHead($key,$val)
    {
        return;
    }
}

/**
 *
 * 垂直搜索查询
 * @author Administrator
 *
 */
class search_deepConvertHandle implements iConvertHandle{
    function convert2Query($key,$val){
        //echo $key;
        $keyArr = explode("-_-",$key);  //字段名按 "-_-" 来分割
        $val = trim(base_decode($val));
        $valArr = preg_split("/\s+/i", $val); //值按空格来分割
        foreach($keyArr as $keyItem):
            foreach($valArr as $valItem):
                 $strCond []= "$keyItem like '%".$valItem."%' ";  //生成查询条件数组.
            endforeach;
        endforeach;
        $where =   "(".implode("or ",$strCond ).")";  //合并生成查询条件.
        return $where;
    }
    function convert2Page($key,$val)
    {
        $pageParam = array( "search_deep_$key"=>"$val");
        return $pageParam;
    }
    function convert2SearchHead($key,$val)
    {
        return;
    }
}

/**
 *
 * 是否为空搜索
 * @author Administrator
 *
 */
class search_isnullConvertHandle implements iConvertHandle{
    function convert2Query($key,$val){
        $val = base_decode($val);
        if($val=='1')
        {
        $where = "($key is  null)";
        }
        else if($val=='2')
        {
        $where ="($key is not null)";
        }
        return $where;
    }
    
    function convert2Page($key,$val)
    {
        $pageParam = array( "search_isnull_$key"=>"$val");
        return $pageParam;
    }
    
    function convert2SearchHead($key,$val)
    {
        return;
    }
}


/**
 *
 * 大于等于查询.
 * @author Administrator
 *
 */
class search_startConvertHandle implements iConvertHandle{
    function convert2Query($key,$val){
        $val = base_decode($val);
         $where="($key >= '$val')";
        return $where;
    }
    function convert2Page($key,$val)
    {
        $pageParam = array( "search_start_$key"=>"$val");
        return $pageParam;
    }
    
    function convert2SearchHead($key,$val)
    {
        return;
    }
}

/**
 *
 * 小于等于查询
 * @author Administrator
 *
 */
class search_endConvertHandle implements iConvertHandle{
    function convert2Query($key,$val){
        $val = base_decode($val);
         $where="($key <= '$val')";
        return $where;
    }
    function convert2Page($key,$val)
    {
        $pageParam = array( "search_end_$key"=>"$val");
        return $pageParam;
    }
    
    function convert2SearchHead($key,$val)
    {
        return;
    }
}


/**
 *
 * 小于等于查询
 * @author Administrator
 *
 */
class search_end_dayConvertHandle implements iConvertHandle{
    function convert2Query($key,$val){
        $val = base_decode($val);
         $where="($key <= '$val')";
        return $where;
    }
    function convert2Page($key,$val)
    {
        $pageParam = array( "search_end_day_$key"=>"$val");
        return $pageParam;
    }
    
    function convert2SearchHead($key,$val)
    {
        return;
    }
}


/**
 *
 * abstract convert which convert the params to the dest dataFormat.
 * @author Administrator
 *
 */
abstract class Convert{
    private $_params;
    private $_iConvertHandle;
    
    function __construct($params){
        $this->setParams($params);
    //    pr($this->getParams(),"得到了什么参数?");
    }
    
    
    public function setParams($params){
        $this->_params = $params;
    }
    public function getParams()
    {
        return $this->_params;
    }
    
    public function setConvertHandle($handle)
    {
        $this->_iConvertHandle = new $handle();
    }
    public function getConvertHandle()
    {
        return $this->_iConvertHandle;
    }
    
    public function getKeyValue($key)
    {
        //pr($key,"正在解析字段名");
        preg_match_all("/(search_[^_]+)_([\w-]+)/i",$key,$match);
        
        if(!$match[0]){
            $match[1][0] = $key;
            $match[2][0] = "";
        }
        //    pr($match,"解析后的值");
        return array($match[1][0],$match[2][0]);
        
    }
    abstract function convert();
}

class ciPagerParamConvert extends Convert{
    function convert()
    {
         $params  = $this->getParams();
          $pageParams = array();  //初始化parameters
          
        foreach($params as $key=>$val):
              list($queryHandel,$queryKey) = $this->getKeyValue($key);
              if(preg_match("/search_/i",$queryHandel))
              {
                   $this->setConvertHandle($queryHandel."ConvertHandle");
                   $pageParams += $this->getConvertHandle()->convert2Page($queryKey,$val);
              }else if($queryHandel == "page"){
                  $pageParams += array("page"=>$val);
              }
        endforeach;
        return $pageParams;
    }
    
    
}

/**
 *
 * execute the querConvert
 * @author Administrator
 *
 */
class ciQueryConvert extends  Convert{
    /**
     * Enter description here ...
     */
    function convert(){
    //pr($this->getParams(),"start to convert the info.");
        $queryCond = array();
        $params = $this->getParams();
        foreach($params as $key=>$val):
              list($queryHandel,$queryKey) = $this->getKeyValue($key);
              if(preg_match("/search_/i",$queryHandel))
              {
                   $this->setConvertHandle($queryHandel."ConvertHandle");
                   $queryCond []= $this->getConvertHandle()->convert2Query($queryKey,$val);
              }
            
        endforeach;
        //pr($queryCond,"查询条件!");
        return $queryCond;
        //return "hello world!";
    }
}


class ciSearchHeadConvert extends Convert {
    function convert() {
        
        $params = $this->getParams();
        foreach($params as $key=>$val):
        
          list($queryHandel,$queryKey) = $this->getKeyValue($key);
          
            if(preg_match("/search_/i",$queryHandel))
             {
                   $this->setConvertHandle($queryHandel."ConvertHandle");
                   $searchHead []= $this->getConvertHandle()->convert2Page($queryKey,$val);
             }
        endforeach;
        
        return $searchHead;
              
    }
}


/**
 *
 * interface iParse to parse the src to the interchange parameters;
 * @author Administrator
 *
 */
interface iParse{
    function parse();
}

/**
 *
 * this class implement the iParse interface to parse the getPrase.
 * @author Administrator
 *
 */
class ciGetParse implements iParse{
    private $_params = "";
    private $_deep;
    
    function __construct($deep = 3)
    {
        $this->setDeep($deep);
    }
    
    /**
     *
     * set the urlParseDeep
     */
    public function setDeep($deep){
        $this->_deep = $deep;
    }
    
    function parse(){
        $CI = &get_instance();
           //$CI->output->enable_profiler(TRUE);
        $parameters = $CI->uri->uri_to_assoc($this->_deep);    //解析URI中的参数
       // pr($parameters);
       
        
        if(isset($parameters["page"]) && is_numeric($parameters["page"])){//兼容分页类
            $_GET["page"] = $parameters["page"];    
        }
        else{  
            $parameters["page"] = 1;
            $_GET["page"] = 1;
        }
        
        // pr($parameters,"url_to_assoc");
         return $parameters;
    }
}

/**
 * cipostParse
 * @author Administrator
 *
 */
class ciPostParse implements iParse{
    private $_params = array();
    function parse(){
         //分页参数的处理,如果同有相关参数,则设为1.
        if(isset($this->_params["page"]) && is_numeric($this->_params["page"])){
            $_GET["page"] = $this->_params["page"];    //兼容分页类
        }
        else{
            $this->_params["page"] = 1;       
            $_GET["page"] = 1;
        }
        
        $search_like = $_POST['search_like'];  //接收search_like 的参数
        if(is_array($search_like)):
        //遍历所有的参数,为空的不参与搜索,后期交由handle去处理.
                foreach ($search_like as $key => $value):
                    if (trim($value) == "") unset($search_like[$key]);    //删除搜索条件中值为空的参数
                    else {
                        $this->_params += array_map('base_encode', array("search_like_$key"=>"$value"));
                    }
                 endforeach;
        endif;
        
        $search_deep = $_POST['search_deep'];  //接收search_deep 的参数 = $_POST('search_deep');
        if(is_array($search_like)):
        //遍历所有的参数,为空的不参与搜索,后期交由handle去处理.
                foreach ($search_deep as $key => $value):
                    if (trim($value) == "") unset($search_deep[$key]);    //删除搜索条件中值为空的参数
                    else {
                        $this->_params += array_map('base_encode', array("search_deep_$key"=>"$value"));
                    }
                 endforeach;
        endif;
        
        $search_equal = $_POST['search_equal'];  //接收search_equal 的参数
             //相等条件查询配置.
          if(is_array($search_equal)):
                 //遍历所有的参数,为空的不参与搜索,后期交由handle去处理.
                foreach ($search_equal as $key => $value):
                    if (trim($value) == "")
                    {
                        unset($search_equal[$key]);    //删除搜索条件中值为空的参数
                        
                    }else {
                        $search_equal[$key] = trim($value);
                        $this->_params += array_map('base_encode', array("search_equal_$key"=>"$value"));
                    }
            endforeach;
         endif;
         
         $search_start = $_POST['search_start'];  //接收search_equal 的参数
         if(is_array($search_start)):
            //遍历所有的参数,为空的不参与搜索,后期交由handle去处理.
                    foreach ($search_start as $key => $value):
                        if (trim($value) == "") unset($search_start[$key]);    //删除搜索条件中值为空的参数
                        else {
                            $value = trim($value);
                    
                            $this->_params += array_map('base_encode', array("search_start_$key"=>"$value"));
                        }
                     endforeach;
            endif;
            
            
            //<=条件配置
            $search_end = $_POST['search_end'];  //接收search_equal 的参数
            if(is_array($search_end)):
            //遍历所有的参数,为空的不参与搜索,后期交由handle去处理.
                    foreach ($search_end as $key => $value):
                        if (trim($value) == "") unset($search_end[$key]);    //删除搜索条件中值为空的参数
                        else {
                            $value = trim($value);
                            $this->_params += array_map('base_encode', array("search_end_$key"=>"$value"));
                        }
                     endforeach;
            endif;
            
            //<=条件配置
            $search_end_day = $_POST['search_end_day'];  //接收search_equal 的参数
            if(is_array($search_end_day)):
            //遍历所有的参数,为空的不参与搜索,后期交由handle去处理.
                    foreach ($search_end_day as $key => $value):
                        if (trim($value) == "") unset($search_end_day[$key]);    //删除搜索条件中值为空的参数
                        else {
                            $value = trim($value);
                            $this->_params += array_map('base_encode', array("search_end_$key"=>"$value"));
                        }
                     endforeach;
            endif;
        
                    // 判断是否为空
                    
            $isnull = $_POST['search_isnull'];  //接收isnull 的参数
            if(is_array($isnull)):
            foreach ($isnull as $key => $value):
                        if (trim($value) == "" || trim($value) == 0 ) unset($isnull[$key]);    //删除搜索条件中值为空的参数
                        else {
                            $parameters += array_map('base_encode', array("search_isnull_$key"=>"$value"));
                        }
                     endforeach;
            endif;
            
            
            foreach($_POST as $key => $val):
        
               if( strpos($key,"earch_mult",1) == 1)  //判断是否为复合搜索.
               {
                 $str_val =  array();  //生成parm所用参数数组.
                 $str_cond = array();  //生成查询条件数组.
                
                 $key_piece = str_replace("search_mult_","",$key);  //找出mul 部分.
                 if(sizeof($_POST["$key"]) >0):
                     foreach($_POST["$key"] as $val)
                     {
                         $str_val[]= base_encode($val);  //生成parm所用参数数组.
                         $str_cond []= "$key_piece = '$val'  ";  //生成查询条件数组.
                     }
                    
                     $str_mul = implode("_xyx_",$str_val);   $this->_params[$key] = $str_mul; //合并生成parameters.
                  
                    endif;
               }
            endforeach;
        
        //$this->_params = $_POST;
        return $this->_params;
    }
}
















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值