开发者

How to use arrays to store values fetch from sql query in php

开发者 https://www.devze.com 2023-02-09 00:45 出处:网络
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.

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:0开发者_如何学Python9'","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:

How to use arrays to store values fetch from sql query in php


I think you misunderstood how mysql_fetch_assoc works: Every call returns one row from the result set. $grep is an array with structure columnname => value.

That also means that $qrep['PID'] cannot be an array and your foreach loop won't work. In every loop, $qrep['PID'] contains one of the values you see in the PID column in your screen shot.

I suggest you add print_r($qreq); in your while loop so you get more familiar with the structure of the array.

As each $qrep is an array itself, creating a result array is just adding each of these to a new array:

$result = array();
while(($qrep=mysql_fetch_assoc($qreports))) {
    $result[] = $qrep;
}

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_P with S_PRICE:

$total = 0;
foreach($result as $report) {
    $total += $report['QTY_P'] * $report['S_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:

$total = 0;
$total_per_customer = array();
foreach($result as $report) {
    $customer = $report['Cust_Name'];
    if(!array_key_exists($customer, $total_per_customer)) {
        $total_per_customer[$customer] = 0;
    }
    $price = $report['QTY_P'] * $report['S_PRICE'];
    $total_per_customer[$customer] += $price;
    $total += $price;
}

In the end, $total_per_customer is an array with customer names as keys and the sum of the prices as values. $total will contain the sum of all prices.


You question is a bit unclear, but i think the answer wont be far a way from the following suggested references:

  • As you loop through the rows use something like $array[] = $qrep['columnName'] to populate an array.
  • Then, check out PHP's array_sum().
0

精彩评论

暂无评论...
验证码 换一张
取 消