Jump to content

Codeigniter mysql result


c_pattle

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; ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.