R
R
Rostislav2014-11-27 18:01:41
PHP
Rostislav, 2014-11-27 18:01:41

How to splice data in php array?

The mysql database has a table s_orders, it contains the data I need, this is the customer name (name) and the order amount (total_price).
How to add the same customers (name) and order amount (total_price ) so that not all orders are displayed, but only the customer's name and the total amount of all his orders?
I take out clients with such a request

$addrs=array();
$result=mysql_query("SELECT name,address,phone,delivery_id,total_price FROM s_orders WHERE status = '2' AND paid = '1'  ORDER BY total_price");
$n=mysql_num_rows($result);
for($i=0;$i<$n;$i++) { 
   array_push($addrs, mysql_result($result,$i,"name")); 
   array_push($addrs, mysql_result($result,$i,"total_price")); 
}
mysql_close($db);
echo implode("", $addrs);

Thanks

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Melkij, 2014-11-27
@postislav

select name, sum(total_price) from s_orders where /**/ group by name

A
apasen, 2014-11-27
@apasen

GROUP BY user.id in muscle

R
Rostislav, 2014-11-27
@postislav

I changed it like this, but it displays only one client and the amount of only one order and not for all (

$result=mysql_query("SELECT name,address,phone,delivery_id,total_price, sum(total_price) FROM s_orders WHERE status = '2' AND paid = '1' AND name != 'Тест' ORDER BY name");

M
Mykola, 2014-11-27
@iSensetivity

SELECT name,address,phone,delivery_id,total_price, sum(total_price) FROM s_orders WHERE status = '2' AND paid = '1' AND name != 'Тест' group BY name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question