php - 在if条件php中使用for循环计数器

标签 php mysql datatable datatables

下面是我的数据表列搜索以及完整搜索的代码,

$columns = array(0 => 'ship_id' , 
                     1 => 'sr', 
                     2 => 'ce', 
                     3 => 'sto',
                     4 => 'supply' , 
                     5 => 'part',
                     6 => 'description',
                     7 =>  'quantity',
                     8 =>  'date',
                     9 =>  'shipn',
                     10 => 'ship',
                     11 => 'transport', 
                     12 => 'docket',
                     13 => 'delivery');

if(isset($searchValue) && $searchValue != '')
        {
            $searchingAll = array();
            for($i=0; $i<count($columns); $i++) //Loop search in all defined columns
            {
                $searchingAll = $this->db->or_like($columns[$i], $searchValue);
                $searchingColumns = NULL;
            }
        }
        else if($this->input->post('columns[1][search][value]', TRUE) != '')
        {
            for($i=0; $i<count($columns); $i++) //Loop search in all defined columns
            {
                $searchingAll = NULL;
                $searchingColumns = $this->db->or_like($columns[1], $this->input->post('columns[1][search][value]', TRUE));
            }   
        }
        else
        {
            $searchingAll = NULL;
            $searchingColumns = NULL;
        }

第一个 IF 条件将搜索整个数据表,然后 else-if 将搜索单个列,

我的数据表中有 12 列,因此我需要循环 12 次 else-if 条件,

在上面的代码中,目前我只显示了第 1 列,但我需要检查所有 12 列中是否在任何列搜索中输入了某些内容,然后它将进入 else-if 条件的 for 循环,

如何在 else-if 条件下检查所有 12 列?

谢谢

最佳答案

您没有使用在 else if 中定义的 $i
改用这个:

    $columns = array(0 => 'ship_id' , 
                 1 => 'sr', 
                 2 => 'ce', 
                 3 => 'sto',
                 4 => 'supply' , 
                 5 => 'part',
                 6 => 'description',
                 7 =>  'quantity',
                 8 =>  'date',
                 9 =>  'shipn',
                 10 => 'ship',
                 11 => 'transport', 
                 12 => 'docket',
                 13 => 'delivery');

    if(isset($searchValue) && $searchValue != '')
    {
        $searchingAll = array();
        for($i=0; $i<count($columns); $i++) //Loop search in all defined columns
        {
            $searchingAll = $this->db->or_like($columns[$i], $searchValue);
            $searchingColumns = NULL;
        }
    }
    else if($this->input->post('columns[1][search][value]', TRUE) != '')
    {
        for($i=0; $i<count($columns); $i++) //Loop search in all defined columns
        {
            $searchingAll = NULL;
            $searchingColumns = $this->db->or_like($columns[$i], $this->input->post('columns['.$i.'][search][value]', TRUE));
        }   
    }
    else
    {
        $searchingAll = NULL;
        $searchingColumns = NULL;
    }

关于php - 在if条件php中使用for循环计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33314273/

相关文章:

java - 从 mysql 插入时出错

javascript - 错误数据表 : Individual Select When Using innerHTML

php - Symfony 从模型生成数据库

php - 为什么我的代码出现 fatal error : Uncaught exception 'DOMPDF_Exception' with message 'Frame not found in cellmap

php - 尝试使用 post 数据使用 php 插入 mysql

php - 如何正确设置循环以显示所有信息

MySQL 查询在新服务器上花费太多时间

php - SQL 中每组有 N 个项目 || SQL子查询限制

c# - 如何改进我的第一个 OSS 项目

可编辑所有记录的 ASP.NET Gridview