Welcome Guest, Not a member yet? Register   Sign In
calculate difference between database-rows on output in an easy way?
#6

[eluser]Richard Schumann[/eluser]
ok i got it !
what was realy helpfull was a print_array debug function to realy see whats going on.

and your tip with result() and result_array() was realy helpfull.

the problem was also to dont have it in a new "result" - instead i wanna use $data .

i had some problems with FIRST or LAST empty array value and because i use often while and not for (dont ask me why)
this is now my solution what i will design a bit like make a + for positive values and make it green... and negative becomes red...

thanks for helping dude


CONTROLLER

Code:
$this->db-> from('weight');
$this->db-> where('user_id', $user_id);
$this->db-> order_by("timestamp", "desc");

$data['query'] = $this->db->get();
$data['result'] = $data['query']->result_array();
        
$rows = count($data['result']);    $i = 0;

while ($i < $rows-1 )
    {
    $data['result'][$i]['diff']  =  $data['result'][$i]['value'] - $data['result'][$i+1]['value'];  
    $i++;
    }

$data['result'][$i]['diff'] = "";  //fill last array to prevent an error by triggering a not existing arrayvalue...

VIEW
Code:
&lt;?php foreach ( $result as $row ): ?&gt;
<tr>
     <td width="100">&lt;?=mdate("%d.%m.%Y",$row['timestamp'])?&gt;</td>
     <td width="100" class="r">&lt;?=$row['value']?&gt;</td>
     <td width="100" class="r">&lt;?=$row['diff']?&gt;</td>
     <td width="60"  class="r">&lt;?=anchor('weight/edit/'.$row['id'],'edit');?&gt;</td>
</tr>
&lt;?php endforeach; ?&gt;


Messages In This Thread
calculate difference between database-rows on output in an easy way? - by El Forum - 02-06-2010, 01:26 PM



Theme © iAndrew 2016 - Forum software by © MyBB