Balwant Singh wrote:
> hi to all, > > may somebody guide me on the following: > > I want to retrieve data from MYSQL and then want that PHP do calculation
let hope your not asking how to connect to the DB. and perform a query...
> - substract a particular field from its previous row. i.e. suppose i > have two field named DATE, STOCK and have 10 rows. Now i want that STOCK > on the 10th row should be substracted from 9th and so on. May pls. help > me.
image you have an array of rows of data already retrieved from the DB (and each row has two items, say DATE and STOCK), lets assume that the order of the items is such that row 10 is first, row 9 second (easy to do with the ORDER BY clause of the SQL query, use ASC/DESC on the required field as required).
e.g. $rows.
<?
// here is a fake array to simulate the data from mysql $rows = array( array('STOCK' => 2), // 10 array('STOCK' => 4), // 9 array('STOCK' => 8), // 8 array('STOCK' => 16), // 7 array('STOCK' => 32), // 6 array('STOCK' => 64), // 5 array('STOCK' => 128), // 4 array('STOCK' => 256), // 3 array('STOCK' => 512), // 2 array('STOCK' => 1024), // 2 );
$calcStockVals = $origStockVals = array(); foreach ($rows as $row) { $prevRow = isset($prevRow) ? $row['STOCK'] - $prevRow : $row['STOCK'];
$origStockVals[] = $row['STOCK']; $calcStockVals[] = $prevRow; }
var_dump(array_reverse( $origStockVals ));
echo "\n ---- \n";
var_dump(array_reverse( $calcStockVals ));
?>
hope that gives you an idea.
> > > with best wishes > balwant >
-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php