What I want to do here is to be able to assign the values from sql query into arrays.
And then compute for the total.
<?php
include('conn.php');
$qreports=query_database("SELECT S_PRICE, PID, QTY_PUR, ITEM_ID, CUSID, CNUM, Cust_Name, TDATE FROM prod_table, customer_credit, sales_trans_items WHERE prod_table.PID=sales_trans_items.ITEM_ID AND sales_trans_items.CNUM=customer_credit.CUSID AND sales_trans_items.TDATE='2011-02-06 09:14:09'","onstor",$link);
$grandtotal=0;
$customer="";
while($qrep=mysql_fetch_assoc($qreports)){
$pid=$qrep['PID'];
foreach ($pid as $k => $v) {
$sids=$v;
$qtypur=$qrep['QTY_PUR'][$k];
$sprice=$qrep['S_PRICE'][$k];
$customer=$qrep['Cust_Name'][$k];
$tdate=$qrep['TDATE'][$k];
$stot=$qtypur * $sprice;
$grandtotal =$grandtotal + $stot;
}
}
echo "Customer: ". $customer."<br/>";
echo "Profit: ". $grandtotal;
?>
I tried the query on phpmyadmin before I place it in the code, and it output this:

I think you misunderstood how
mysql_fetch_assocworks: Every call returns one row from the result set.$grepis an array with structurecolumnname => value.That also means that
$qrep['PID']cannot be an array and yourforeachloop won’t work. In every loop,$qrep['PID']contains one of the values you see in thePIDcolumn in your screen shot.I suggest you add
print_r($qreq);in yourwhileloop so you get more familiar with the structure of the array.As each
$qrepis an array itself, creating a result array is just adding each of these to a new array:Now you are performing some more computation where I am not sure what you want to get in the end but it seems you are multiplying each
QTY_PwithS_PRICE:(You could however also compute this in your while loop)
Assuming you want to get the overall sum and the sum for each customer:
In the end,
$total_per_customeris an array with customer names as keys and the sum of the prices as values.$totalwill contain the sum of all prices.