0

Hello guys,

Working on a stock market table with rates of different currencies.

What I would like to achieve is that i have an array of rates and would like the difference of today and the previous day, and it works, but i'm getting an empty value for the first row, which i dont know how to escape.

                                while($arr = mysql_fetch_array($sql))
                                {
                                    if(!is_null($previous) && $previous['euro'] == $arr['euro'])
                                    {
                                        $euro = '';
                                        $usd = ''; 
                                    }
                                    else
                                    {
                                        $euro = $previous['euro']-$arr['euro'];
                                        $usd = $previous['usd']-$arr['usd'];
                                        $huf = $previous['huf']-$arr['huf'];

                                        if('-'.$arr['euro'] == $euro)
                                        {
                                            $euro_ok = null;
                                            $usd_ok = null;
                                            $huf_ok = null;
                                        }
                                        else
                                        {
                                            $euro_ok = $euro;
                                            $usd_ok = $usd;
                                            $huf_ok = $huf;
                                        }

                                        $data .= '<tr><td style="width: 100px; background-color: #CCC;">'.date("d-M-Y", strtotime($arr['data'])).'</td><td>'.$arr['euro'].'</td><td>'.$this->formatare($euro_ok).'</td><td>'.$arr['usd'].'</td><td>'.$this->formatare($usd_ok).'</td><td>'.$arr['huf'].'</td><td>'.$this->formatare($huf_ok).'</td></tr>';
                                    }

                                    $previous = $arr;
                                    $i++;
                                }

Untitled32

2
Contributors
7
Replies
9
Views
5 Years
Discussion Span
Last Post by Szabi Zsoldos
0

for first case your previous element is blank, you are setting it at line 30. So you must have some reference value in the begingin for $previous

0

Yes it's blank, it's getting the same value as the first element, i've tried to eliminate it so the first value should be my second row :(

Edited by Szabi Zsoldos

0

try this, add 1 lines before while

$previous = mysql_fetch_array($sql)

  while($arr = mysql_fetch_array($sql))
  {

Edited by urtrivedi

1

try this

$arr = mysql_fetch_array($sql);
                                while($previous = mysql_fetch_array($sql))
                                {
                                    if(!is_null($previous) && $previous['euro'] == $arr['euro'])
                                    {
                                        $euro = '';
                                        $usd = ''; 
                                    }
                                    else
                                    {
                                        $euro = $previous['euro']-$arr['euro'];
                                        $usd = $previous['usd']-$arr['usd'];
                                        $huf = $previous['huf']-$arr['huf'];

                                        if('-'.$arr['euro'] == $euro)
                                        {
                                            $euro_ok = null;
                                            $usd_ok = null;
                                            $huf_ok = null;
                                        }
                                        else
                                        {
                                            $euro_ok = $euro;
                                            $usd_ok = $usd;
                                            $huf_ok = $huf;
                                        }

                                        $data .= '<tr><td style="width: 100px; background-color: #CCC;">'.date("d-M-Y", strtotime($arr['data'])).'</td><td>'.$arr['euro'].'</td><td>'.$this->formatare($euro_ok).'</td><td>'.$arr['usd'].'</td><td>'.$this->formatare($usd_ok).'</td><td>'.$arr['huf'].'</td><td>'.$this->formatare($huf_ok).'</td></tr>';
                                    }

                                     $arr=$previous;
                                    $i++;
                                }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.