peter162in Posted March 8, 2010 Share Posted March 8, 2010 NEED TO FIND DIFF . DATA ARRANGED IN DATE AND PRICE. FIND DIFF FOR EVERYDAY. MYSQL AND PHP. <?php include "dbcon1.php"; $sql = "SELECT date,price FROM `tb1` ORDER BY tb1.date DESC"; $result = mysql_query($sql) or die(mysql_error()); $1_header=<<<EOD <h2><center>PROFIT ANALYSIS<font color="red" size="6"></h2> <table width="70%" border="1" cellpadding="2" cellspacing="2" align="center"> <tr> <th>DATE</th> <th>PRICE</th> <th>DIFF</th> </tr> EOD; $1_details = ''; while ($row = mysql_fetch_array($result)) { $date = $row['date']; $price = $row['price']; $diff= ????????????????????????/ some body help $1_details .=<<<EOD <tr> <td>$date</td> <td> $price</td> <td> $diff</td> </tr> EOD; } $1_footer ="</table>"; $1 =<<<RESULT $1_header $1_details $1_footer RESULT; echo $1; ?> Quote Link to comment https://forums.phpfreaks.com/topic/194514-find-diff-within-an-array/ Share on other sites More sharing options...
gizmola Posted March 8, 2010 Share Posted March 8, 2010 You have to capture the starting price the first time. At the bottom of the loop set the previous price. $1_details = ''; $priordate = ''; $priorprice = null; while ($row = mysql_fetch_array($result)) { $date = $row['date']; $price = $row['price']; if ($priordate == '') { $priordate = $date; $priorprice = $price; } $diff = $price - $priorprice; $1_details .= $date $price $diff EOD; $priordate = $date; $priorprice = $price; } $1_footer =""; Quote Link to comment https://forums.phpfreaks.com/topic/194514-find-diff-within-an-array/#findComment-1023244 Share on other sites More sharing options...
peter162in Posted March 9, 2010 Author Share Posted March 9, 2010 THANK YOU FOR GREAT HELP. A SMALL PROBLEM FOR THE RESULT while ($row = mysql_fetch_array($result)){ $date = $row['date']; $price = $row['price']; if ($priordate == '') { $priordate = $date;// Date is not looping . I faced this type of problem earlier also because of '{' braces $priorprice = $price; } MY RESULT DATE PRICE DIFF 2009-08-01 1232 0 2009-08-04 1250 18 2009-08-05 1273 41 2009-08-19 1181 -51 PRICE DIFF DISPLAYED FROM THE START DATE ACTUAL RESULT I EXPECT IS DATE PRICE DIFF 2009-08-01 1232 0 2009-08-04 1250 18 2009-08-05 1273 23 2009-08-19 1181 -92 THE DATE IS NOT CAPTURING . PLEASE HELP ME. Quote Link to comment https://forums.phpfreaks.com/topic/194514-find-diff-within-an-array/#findComment-1023671 Share on other sites More sharing options...
gizmola Posted March 9, 2010 Share Posted March 9, 2010 No ... the loop was setup properly in my example, you can not arbitrarily move braces around and have the same effect. Let's see the actual code of the script you have at this point. Please use the [code=php:0] php code here .... [/code] on each side of your php code block. Quote Link to comment https://forums.phpfreaks.com/topic/194514-find-diff-within-an-array/#findComment-1023732 Share on other sites More sharing options...
peter162in Posted March 10, 2010 Author Share Posted March 10, 2010 My complete code here. some where i do a mistake .the $priorprice is not updating in the final result. it remains same. <?php include "dbcon1.php"; $sql = "SELECT date, price FROM `table1` ORDER BY table.date DESC"; $result = mysql_query($sql) or die(mysql_error()); $my_header=<<<EOD <h2><center>PROFIT ANALYSIS<font color="red" size="6"></h2> <table width="70%" border="1" cellpadding="2" cellspacing="2" align="center"> <tr> <th>DATE</th> <th>PRICE</th> <th>DIFF</th> </tr> EOD; $my_details = ''; $priordate = ''; $priorprice = null; while ($row = mysql_fetch_array($result)) { $date = $row['date']; $price = $row['price']; if ($priordate == '') { $priordate = $date; echo $priordate; $priorprice = $price; echo $priorprice; } echo $priorprice;// price captured remain as initial price $diff = $priorprice - $price; //PRIORPRICE($priorprice) REMAIN UNCHANGED AS DATE CAPTURED REMAIN THE INITIAL DATE $my_details .=<<<EOD <tr> <td>$date</td> <td><font color="red" size="4"> $price</td> <td><font color="red" size="4"> $diff</td> </tr> EOD; } $my_footer ="</table>"; $final =<<<RESULT $my_header $my_details $my_footer RESULT; echo $final; ?> I think while running if loop is executed once , then it is bypassed on mysql_fetch_array? help please Quote Link to comment https://forums.phpfreaks.com/topic/194514-find-diff-within-an-array/#findComment-1024218 Share on other sites More sharing options...
peter162in Posted March 10, 2010 Author Share Posted March 10, 2010 My complete code here. some where i do a mistake .the $priorprice is not updating in the final result. it remains same. <?php include "dbcon1.php"; $sql = "SELECT date, price FROM `table1` ORDER BY table.date DESC"; $result = mysql_query($sql) or die(mysql_error()); $my_header=<<<EOD <h2><center>PROFIT ANALYSIS<font color="red" size="6"></h2> <table width="70%" border="1" cellpadding="2" cellspacing="2" align="center"> <tr> <th>DATE</th> <th>PRICE</th> <th>DIFF</th> </tr> EOD; $my_details = ''; $priordate = ''; $priorprice = null; while ($row = mysql_fetch_array($result)) { $date = $row['date']; $price = $row['price']; if ($priordate == '') { $priordate = $date; echo $priordate; $priorprice = $price; echo $priorprice; } echo $priorprice;// price captured remain as initial price $diff = $priorprice - $price; //PRIORPRICE($priorprice) REMAIN UNCHANGED AS DATE CAPTURED REMAIN THE INITIAL DATE $my_details .=<<<EOD <tr> <td>$date</td> <td><font color="red" size="4"> $price</td> <td><font color="red" size="4"> $diff</td> </tr> EOD; } $my_footer ="</table>"; $final =<<<RESULT $my_header $my_details $my_footer RESULT; echo $final; ?> I think while running if loop is executed once , then it is bypassed on mysql_fetch_array? help please Quote Link to comment https://forums.phpfreaks.com/topic/194514-find-diff-within-an-array/#findComment-1024230 Share on other sites More sharing options...
gizmola Posted March 13, 2010 Share Posted March 13, 2010 You have to read *carefully* the code I originally posted. You just completely left out the setting of the variables at the bottom of the fetch loop. I corrected your code *again*. include "dbcon1.php"; $sql = "SELECT date, price FROM `table1` ORDER BY table.date DESC"; $result = mysql_query($sql) or die(mysql_error()); $my_header= PROFIT ANALYSIS </pre> <table width="70%" border="1" cellpadding="2"></table> cellspacing="2" align="center"><br> <tr> DATE PRICE DIFF </tr> <br>EOD;<br>$my_details = '';<br>$priordate = '';<br>$priorprice = null;<br>while ($row = mysql_fetch_array($result)) {<br> $date = $row['date'];<br> $price = $row['price'];<br> if ($priordate == '') {<br> $priordate = $date;<br> echo $priordate; <br> $priorprice = $price;<br>echo $priorprice;<br> }<br><br>echo $priorprice;// price captured remain as initial price<br> $diff = $priorprice - $price; //PRIORPRICE($priorprice) REMAIN UNCHANGED AS DATE CAPTURED REMAIN THE INITIAL DATE<br> $my_details .= <tr> $date $price $diff </tr> <br>EOD;<br> $priordate = $date;<br> $priorprice = $price;<br>}<br>$my_footer =""; <br> <br>$final = $my_header<br> $my_details<br> $my_footer<br>RESULT;<br> echo $final;<br>?><b Quote Link to comment https://forums.phpfreaks.com/topic/194514-find-diff-within-an-array/#findComment-1025517 Share on other sites More sharing options...
peter162in Posted March 13, 2010 Author Share Posted March 13, 2010 Grate !,Thanks a lot. It is working.. I forget to scroll down through the code. seen half only. sorry for that. thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/194514-find-diff-within-an-array/#findComment-1025531 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.