Loop through results from mysql, and sum values - php

I use a while loop to process table record like this:
$get_s = "SELECT * FROM sells ORDER BY sells_date";
if ($result = mysqli_query($AECONNECT, $get_s)) {
while ($show_s = mysqli_fetch_assoc($result)) {
$quantity = $show_s['sells_quantity'];
}
mysqli_free_result($result);
}
I have all my table records, and now I want to sum up all quantity fields , but I don't know how to do it.
For example, if I got 10 records quantity for records like 2, 1, 5, 1, 3, 6 etc, I would like to sum them like this: 2+1+5+1+3+6 = 18

If you can do something in mysql - do it. Use SUM aggregation function:
$get_s = "SELECT SUM(sells_quantity) as sells_sum FROM sells ORDER BY sells_date";
if ($result = mysqli_query($AECONNECT, $get_s)) {
$show_s = mysqli_fetch_assoc($result);
echo $show_s['sells_sum'];
}
mysqli_free_result($result);
Still, if you need certain values of rows - you can count sum in a loop:
$get_s = "SELECT * FROM sells ORDER BY sells_date";
if ($result = mysqli_query($AECONNECT, $get_s)) {
$total = 0;
while ($show_s = mysqli_fetch_assoc($result)) {
$quantity = $show_s['sells_quantity'];
$total += $quantity;
}
mysqli_free_result($result);
echo $total;
}
But mysql SUM() is preferrable.

Related

How to calculate the sum of number array elements from multiple database rows using php?

I need to count the number of elements that exist in the column of a MySQL database row for multiple rows and them add them all together. This is the code I used to count the number of array elements (which are numbers separated by commas):
$result = substr_count($count_fetch['micro_analysis'], ",") + 1;
But now I need to do this for each row, which could vary depending on the query. I need to add the $result of each row together to get a final sum of all the rows. I used the following code but I get the incorrect value, can someone please point me in the right direction?
$sql = "SELECT * FROM samples_database WHERE order_id = $order_id";
$count_query = mysqli_query($conn, $sql);
$count_fetch = mysqli_fetch_assoc($count_query);
foreach ($count_fetch as $row) {
$result = substr_count($count_fetch['micro_analysis'], ",") + 1;
$end_result += $result;
}
echo $end_result;
You are just fetching 1 row and then trying to count over that row, instead you need to loop over the rows and add the fields in that...
$sql = "SELECT * FROM samples_database WHERE order_id = $order_id";
$end_result = 0;
$count_query = mysqli_query($conn, $sql);
while( $row = mysqli_fetch_assoc($count_query)) {
$end_result += substr_count($row['micro_analysis'], ",") + 1;
}
echo $end_result;
Replace your code with the following:
$sql = "SELECT * FROM samples_database WHERE order_id = $order_id";
$count_query = mysqli_query($conn, $sql);
$count_fetch = mysqli_fetch_assoc($count_query);
$end_result=0;//initialize variable to 0
foreach ($count_fetch as $k => $row) {
if($k == 'micro_analysis'){
$result = substr_count($row, ",") + 1; //change over here
$end_result += $result;
}
}
echo $end_result;

total array sum using pdo

I'm trying to get total price from cart table. The product_price in other table which is product. I'm only getting the latest price not the total price. Thanks
// function total_price (){
$total = 0;
global $db;
$ip = getIp();
$sql = $db->query("SELECT * from cart WHERE ip_add='$ip'");
$no=$sql->rowCount(); // number of rows affected by the last SQL statement
if ($no == 0){
echo "";
} else {
foreach($sql as $row)
$product_id = $row["p_id"];
$sql = $db->query("SELECT product_price from product WHERE product_id='$product_id'");
$no=$sql->rowCount(); // number of rows affected by the last SQL statement
if ($no == 0){
echo "";
}
else
{
foreach($sql as $row)
$product_price = array($row["product_price"]);
$values = array_sum($product_price );
$total += $values;
}
}
echo "RM" . $total;
}
if I'm reading the structure right this one query should be all you need:
select sum(product_price) from product
inner join cart on product.product_id=cart.product_id

Multiply column values from multiple rows and then add

I need to get the price and amount sold from a database, and then multiply them together to get an output for each one, and then add them all together. I think I could use a foreach loop, but I'm not sure if the variable would retain it's value for each time it goes through. This is what I've tried so far:
$query = "SELECT * FROM `inv`";
$data = mysqli_query($dbc, $query);
while($row = mysqli_fetch_array($data)){
if(isset($fin2)){
$fin = $fin2;
} else {
$price = str_replace('$', '', $row['price']);
$fin2 = ($row['sold'] * $price + $fin);
}
if(isset($fin)){
$fin = ($fin + $fin2);
echo $fin;
}
As you can see, I'm kind of confused, and any help would be appreciated.
Why not do the calculation in the database?
SELECT SUM(price * sold) as total
FROM `inv`;
EDIT:
SELECT SUM((replace(price, '$', '') + 0) * sold) as total
FROM `inv`;

Issue with php array_sum displaying multiple values

I'm having an issue with with a PHP function displaying multiple sums instead of just one when I use array_sum. I'm using phpMyAdmin and the two database tables for this function. These are the tables:
games:
cart:
function total_price() {
$total = 0;
global $con;
$ip = getIP();
$select_price = "select * from cart where ip_add = '$ip'";
$run_price = mysqli_query ($con, $select_price);
while ($g_price = mysqli_fetch_array($run_price)) {
$Cart_Game_ID = $g_price['g_ID'];
$cart_price = "select * from games where Game_ID = '$Cart_Game_ID'";
$run_game_price = mysqli_query($con, $cart_price);
while($pp_price = mysqli_fetch_array($run_game_price)){
$Game_Price = array($pp_price['Game_Price']);
$values = array_sum($Game_Price);
$total += $values;
}
echo "$" . $total;
}
}
Every game in my database costs $20. There are currently 5 games in my database. When I run this function I get $20$40$60$80$100. All i need is the $100. Not the previous 4 sums.
Move this outside the While Loop:
echo "$" . $total;

php math question

I have a db table which have a row of a product amount. I want to create a loop that will calculate for me the sum for all the amounts.
$results = mysql_query("SELECT *
FROM prod");
while($info = mysql_fetch_array($results)) {
$amount = $info['amount'];
}
amount is the var for each product cost. I want to get a sum for all the vars together - how can I do it?
Use the SUM() function in a SQL query.
$result = mysql_query("SELECT SUM(amount) AS sum_amount FROM prod");
if ( $result )
$sum = mysql_result($result, 0, 0);
Easier to do in MySQL:
SELECT SUM(amount) FROM prod
Then fetch the result.
First:
You can do this with mysql
SELECT SUM(amount) FROM ....
Second:
while ($info = mysql_fetch_array($results)) {
$amount[] = $info['amount'];
}
$sum = array_sum($amount);
You can add the amounts to a variable:
$results = mysql_query("select * from prod");
$sum = 0
while($info = mysql_fetch_array($results)) {
$amount = $info['amount'];
$sum += $amount
}
Or you can do it in SQL:
SELECT SUM(column) FROM table

Categories