PHP foreach循环,比较两个值

I'm building a simple weight log, where there'a table that display the user's progress. I'm using Codeigniter.

    <?php foreach($logs as $log) : ?>
        <tr>
        <td><?php echo $log->date ;?></td>
        <td><?php echo $log->weight ;?> kg.</td>
        <td><!-- variation between this record and previous record. weigth[a] - weight[b] --></td>
        <td></td>
        <td><?php echo anchor('logs/edit/'.$log->id, 'Editar'); ?> <?php echo anchor('logs/delete/'.$log->id, 'Delete'); ?></td>
        </tr>
    <?php endforeach ;?>

I'm trying to calculate the variation between the first row and the second row, so as to get the weight loss or gain between logs. I was wondering how to access the loop's previous record so as to substract it from the current record's weight.

---------------------------------------
  DATE     |   WEIGHT  |   VARIATION
---------------------------------------
  Nov 20   |    70 kg  |     -1 kg      << LAST LOGGED WEIGHT, FIRST IN ARRAY
.......................................
  Nov 15   |    71 kg  |      -
---------------------------------------
查看全部
doucan2102
doucan2102
2010/11/09 01:31
  • foreach
  • loops
  • php
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复