0

I would like to optimize the script a little and save cpu some work, so I have to create for loop. In this loop I will work with some data from database and below is my current code:

$result = $this->Model->function();
for ($i = 0; $i < $result->num_rows(); $i++)
{
    echo $result->row_array($i)['row'];
}

What do I need here is to check if the next row exists. This one would be on the top of all code in for loop in if statement, but there is some problem. If I type $result->num_rows()+1 in for loop and echo the last row (which doesn't exist) out, the value of it isn't null, but it's same as row before.

How do I check if current row is null?

2 Answers 2

1

Or rather than have the boolean checked every time, plus you don't call the num_rows method a second time:

$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'];
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this for example:

$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'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.