Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP+;MySQL+;CodeIgniter:检查当前行是否存在_Php_Mysql_Codeigniter_Codeigniter 2 - Fatal编程技术网

PHP+;MySQL+;CodeIgniter:检查当前行是否存在

PHP+;MySQL+;CodeIgniter:检查当前行是否存在,php,mysql,codeigniter,codeigniter-2,Php,Mysql,Codeigniter,Codeigniter 2,我想优化脚本一点,节省cpu一些工作,所以我必须创建for循环。在这个循环中,我将处理数据库中的一些数据,下面是我当前的代码: $result = $this->Model->function(); for ($i = 0; $i < $result->num_rows(); $i++) { echo $result->row_array($i)['row']; } $result=$this->Model->function(); 对于($i=0;$in

我想优化脚本一点,节省cpu一些工作,所以我必须创建for循环。在这个循环中,我将处理数据库中的一些数据,下面是我当前的代码:

$result = $this->Model->function();
for ($i = 0; $i < $result->num_rows(); $i++)
{
    echo $result->row_array($i)['row'];
}
$result=$this->Model->function();
对于($i=0;$i<$result->num_rows();$i++)
{
echo$result->row_数组($i)['row'];
}
我需要在这里检查下一行是否存在。在if语句的for循环中,此代码将位于所有代码的顶部,但存在一些问题。如果我在for循环中键入
$result->num_rows()+1
,并将最后一行(不存在)回显,则该行的值不为null,但与前面的行相同


如何检查当前行是否为空?

您可以这样做,例如:

$result = $this->Model->function();
$y = $result->num_rows();
$y--;
for ($i = 0; $i < $result->num_rows(); $i++)
{
    if ($i == $y)(
        echo 'last row';
    )
    echo $result->row_array($i)['row'];
}
$result=$this->Model->function();
$y=$result->num_rows();
$y--;
对于($i=0;$i<$result->num_rows();$i++)
{
如果($i=$y)(
回声“最后一行”;
)
echo$result->row_数组($i)['row'];
}

或者不是每次都要检查布尔值,再加上第二次不调用num_rows方法:

$result = $this->Model->function();
$y = $result->num_rows();
for ($i = 0; $i < $y-1; $i++)
{        
    echo $result->row_array($i)['row'];
}
echo 'last row';
echo $result->row_array($y-1)['row'];
$result=$this->Model->function();
$y=$result->num_rows();
对于($i=0;$i<$y-1;$i++)
{        
echo$result->row_数组($i)['row'];
}
回声“最后一行”;
echo$result->row_数组($y-1)['row'];