Jump to content

FIND DIFF WITHIN AN ARRAY


peter162in

Recommended Posts

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;

?>

Link to comment
Share on other sites

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 ="";     


Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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