0

I have this JSON which has the same ord_name , cust_id , Ord_num

{"orders":[{"id":"1","ord_name":"Nestea Bottle","ord_desc":"Nestea in a bottle","ord_price":"15","ord_qty":"2","customer_id":"54feec24bff73","ord_num":"13211554feec24bff73","price_x_quan":"30.00","image":"http://192.168.43.52/MMOS/uploads/nestea_bottled.jpg","subtotal":"","imgimg":"uploads/nestea_bottled.jpg"},{"id":"2","ord_name":"Nestea Bottle","ord_desc":"Nestea in a bottle","ord_price":"15","ord_qty":"4","customer_id":"54feec24bff73","ord_num":"13211554feec24bff73","price_x_quan":"60.00","image":"http://192.168.43.52/MMOS/uploads/nestea_bottled.jpg","subtotal":"","imgimg":"uploads/nestea_bottled.jpg"},{"id":"3","ord_name":"Nestea Bottle","ord_desc":"Nestea in a bottle","ord_price":"15","ord_qty":"1","customer_id":"54feec24bff73","ord_num":"13211554feec24bff73","price_x_quan":"15.00","image":"http://192.168.43.52/MMOS/uploads/nestea_bottled.jpg","subtotal":"","imgimg":"uploads/nestea_bottled.jpg"}],"o_total":[{"total":"105"}]} 

my problem is , how to merge or just Overwrite programmatically the JSON with the 'same' ord_num , customer_id and ord_name

field that will update are qty = 7 , price_x_quan

What i want : Nestea in a bottle will have qty = 7 , price_x_quan = 105

this is my code for ordershow

    <?php
mysql_connect('localhost','root','')or die ('No Connection');
mysql_select_db('dbmoms');

//$ord  = $arr['ord_num']
$sum=0;
$total = $sum;


$sql1 ="SELECT * FROM orders ORDER BY id desc  LIMIT 1"; 
if($row=mysql_fetch_array(mysql_query($sql1))){
    $order_id=$row['ord_num'];
}
$sql ="SELECT * FROM orders  WHERE ord_num = '$order_id' "; 


$result = mysql_query($sql);
$arr["orders"] = array();
    while($row = mysql_fetch_assoc($result)){
    $arr['orders'][]= $row ;
    $sum = $sum+$row['price_x_quan'];




}
$arr['o_total'][] = array('total' => "$sum" );


$json_encoded_string = json_encode($arr); 
 $json_encoded_string = str_replace("\\/", '/', $json_encoded_string);

echo $json_encoded_string;



?>

please help !

Mohammad Arman
  • 7,020
  • 2
  • 36
  • 50
Rose_xxi
  • 13
  • 5
  • What would your desired JSON result look like, when echoed in `echo $json_encoded_string`? – mhall Mar 21 '15 at 18:49
  • Should there be three objects, like now, for the Nestea Bottles or just one (with the calculated totals)? – mhall Mar 21 '15 at 18:56
  • @mhall i want to show only One entry of Nestea Bottles with the calculated totals – Rose_xxi Mar 21 '15 at 21:24

2 Answers2

0

Above:

$arr['orders'][]= $row ;

Put:

// Specify custom values for Nestea..
if( $row[ 'ord_desc' ] == 'Nestea in a bottle' )
{
    $row[ 'ord_qty' ] = '7';
    $row[ 'price_x_quan' ] = '105.00';

}

Vladimir Ramik
  • 1,920
  • 2
  • 13
  • 23
0

You can do this right in your MySQL query using SUM function with a GROUP BY clause.

As a side note you should consider using mysqli (where the trailing i stands for improved) or PDO for accessing your database, instead of the deprecated mysql interface. Now why on earth should I bother to do that?

$sql1 ="SELECT * FROM orders ORDER BY id desc  LIMIT 1";
if($row=mysql_fetch_array(mysql_query($sql1))){
    $order_id=$row['ord_num'];
}

// Use SUM and GROUP BY to let the database do the math, introducing
// the calculated 'sum_price_x_quan' and 'sum_ord_qty' columns
$sql = "
    SELECT    ord_num,
              ord_desc,
              sum(price_x_quan) as sum_price_x_quan,
              sum(ord_qty)      as sum_rod_qty
    FROM      orders
    WHERE     ord_num = '$order_id'
    GROUP BY  ord_num
";

$result = mysql_query($sql);

$arr = array();
if ($row = mysql_fetch_assoc($result)) {
    $arr['orders'][] = $row ;
    $arr['o_total'][] = array('total' => $row["sum_price_x_quan"]);
}

$json_encoded_string = json_encode($arr);

echo $json_encoded_string;

Output:

{
    "orders":  [
        {
            "ord_num":          "13211554feec24bff73",
            "ord_desc":         "Nestea in a bottle",
            "sum_price_x_quan": "105.00",
            "sum_ord_qty":      "7"
        }
    ],
    "o_total": [
        {
            "total": "105.00"
        }
    ]
}
Community
  • 1
  • 1
mhall
  • 3,671
  • 3
  • 23
  • 35