doy57007 2014-08-26 06:08
浏览 80
已采纳

PHP codeIgniter查询区分大小写,希望使其不敏感

i know only using LOWER and strlower can fix the problem. but problem i am facing is of syntax as i am using datatables library and they have not set it for insensitive so i need to do little update.

last time it worked for me with

like('LOWER(' .$field. ')', strtolower($value));

but now the main query is

$sWhere .= $this->select[$mColArray[$i]] . " LIKE '%" . $sSearch . "%' OR ";

so i tried like this

$sWhere .= $this->select[."LOWER(".$mColArray[$i].")".] . " LIKE '%" . strtolower($sSearch) . "%' OR ";

but i am not good with this notations, please can anyone help me out, cuz i get error with this..

This line is from the Ignited-dataTables Library from below function.

 protected function get_filtering()
    {
        if($this->check_mDataprop())
            $mColArray = $this->get_mDataprop();
        elseif($this->ci->input->post(mysql_real_escape_string('sColumns'))){
            $mColArray = explode(',', $this->ci->input->post('sColumns'));
            $mColArray = array_filter($mColArray);
            if(empty($mColArray)){
                $mColArray = $this->columns;
            }
        }
        else
            $mColArray = $this->columns;

        $sWhere = '';
        $sSearch = mysql_real_escape_string($this->ci->input->post('sSearch'));
        $mColArray = array_values(array_diff($mColArray, $this->unset_columns));
        $columns = array_values(array_diff($this->columns, $this->unset_columns));

        if($sSearch != '')
            for($i = 0; $i < count($mColArray); $i++)
                if($this->ci->input->post('bSearchable_' . $i) == 'true' && in_array($mColArray[$i], $columns))
                    $sWhere .= $this->select[$mColArray[$i]] . " LIKE '%" . $sSearch . "%' OR ";

        $sWhere = substr_replace($sWhere, '', -3);

        if($sWhere != '')
            $this->ci->db->where('(' . $sWhere . ')');

        for($i = 0; $i < intval($this->ci->input->post('iColumns')); $i++)
        {
            if(isset($_POST['sSearch_' . $i]) && $this->ci->input->post('sSearch_' . $i) != '' && in_array($mColArray[$i], $columns))
            {
                $miSearch = explode(',', $this->ci->input->post('sSearch_' . $i));

                foreach($miSearch as $val)
                {
                    if(preg_match("/(<=|>=|=|<|>)(\s*)(.+)/i", trim($val), $matches))
                        $this->ci->db->where($this->select[$mColArray[$i]].' '.$matches[1], $matches[3]);
                    else
                        $this->ci->db->where($this->select[$mColArray[$i]].' LIKE', '%'.$val.'%');
                }
            }
        }

        foreach($this->filter as $val)
            $this->ci->db->where($val[0], $val[1], $val[2]);
    }
  • 写回答

3条回答 默认 最新

  • dougui1977 2014-08-26 11:38
    关注

    Problem Solved. Search works and is now insensitive. New Code.

    protected function get_filtering()
    {
        if($this->check_mDataprop())
            $mColArray = $this->get_mDataprop();
        elseif($this->ci->input->post(mysql_real_escape_string('sColumns'))){
            $mColArray = explode(',', $this->ci->input->post('sColumns'));
            $mColArray = array_filter($mColArray);
            if(empty($mColArray)){
                $mColArray = $this->columns;
            }
        }
        else
            $mColArray = $this->columns;
    
        $sWhere = '';
        $sSearch = mysql_real_escape_string($this->ci->input->post('sSearch'));
        $mColArray = array_values(array_diff($mColArray, $this->unset_columns));
        $columns = array_values(array_diff($this->columns, $this->unset_columns));
    
        if($sSearch != '')
            for($i = 0; $i < count($mColArray); $i++)
                if($this->ci->input->post('bSearchable_' . $i) == 'true' && in_array($mColArray[$i], $columns))
                    $sWhere .= "UPPER(".$this->select[$mColArray[$i]] . ") LIKE '%" . strtoupper($sSearch) . "%' OR ";
    
        $sWhere = substr_replace($sWhere, '', -3);
    
        if($sWhere != '')
            $this->ci->db->where('(' . $sWhere . ')');
    
        for($i = 0; $i < intval($this->ci->input->post('iColumns')); $i++)
        {
            if(isset($_POST['sSearch_' . $i]) && $this->ci->input->post('sSearch_' . $i) != '' && in_array($mColArray[$i], $columns))
            {
                $miSearch = explode(',', $this->ci->input->post('sSearch_' . $i));
    
                foreach($miSearch as $val)
                {
                    if(preg_match("/(<=|>=|=|<|>)(\s*)(.+)/i", trim($val), $matches))
                        $this->ci->db->where($this->select[$mColArray[$i]].' '.$matches[1], $matches[3]);
                    else
                        $this->ci->db->where('UPPER('.$this->select[$mColArray[$i]].') LIKE', '%'.strtoupper($val).'%');
                }
            }
        }
    
        foreach($this->filter as $val)
            $this->ci->db->where($val[0], $val[1], $val[2]);
    }
    

    i Changed

    $sWhere .= $this->select[$mColArray[$i]] . " LIKE '%" . $sSearch . "%' OR ";
    

    in to

    $sWhere .= "UPPER(".$this->select[$mColArray[$i]] . ") LIKE '%" . strtoupper($sSearch) . "%' OR ";
    

    And Changed

    $this->ci->db->where($this->select[$mColArray[$i]].' LIKE', '%'.$val.'%');
    

    in to

    $this->ci->db->where('UPPER('.$this->select[$mColArray[$i]].') LIKE', '%'.strtoupper($val).'%');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建