Jump to content

Recommended Posts

I was just wondering if there is a way to get the next row in advanced when looping through a mysql results.  At the moment I have

 

<? $i=1; ?>
<? foreach ($query->result() as $row): ?>
    
    <?= $row->order_id ?>

<? $i++; ?>
<? endforeach; ?>

 

At the moment this just echo's the order_id in the row its currently looping through but is there a way to get the order_id of the next row in advance?

 

The reason I want to do this is because if I know the order_id is going to change on the next row then I will want to echo something different. 

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/253232-codeigniter-mysql-result/
Share on other sites

Hi c_pattle,

 

I haven't seen it done with objects, but it is possible with arrays.

 

I would do the following:

 

<?php $i=1; ?>
<?php foreach ($query->result_array() as $row): ?>
    
    <?php echo $row['order_id']; ?>
    <?php $next = $query->next_row('array'); ?> // I placed array in param for it to return an array

<?php $i++; ?>
<?php endforeach; ?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.