I wonder if somebody can help me please. I’m pretty new to PHP and am trying to get the results of a MySQL query to output as JSON but have no idea how to do this.
This is the query that I’m using to get the results:
<?php
$sql_select = "SELECT * ";
$sql_from = "from feeds,shipping,merchant,product_categories_map where feeds.enabled=1 and feeds.stock=1 and feeds.deleted=0 and feeds.brand_name=shipping.merchant and feeds.best_seller=1 and feeds.brand_name=merchant.name and product_id=product_feed_id and product_feed_id!=1 and ( ";
$sql_orderby = " ORDER BY merchant.pos ASC";
$sql_group = " GROUP BY feeds.product_name";
$sql_limit = " LIMIT 25";
$query=$sql_select . $sql_from . $sql_group . $sql_orderby . $sql_limit;
$product_results=dbselect( $query,"dbLinkInt" );
print_r($product_results)
?>
This is returning the results like this:
Array ( [0] => Array ( [id] => 6177 [salesID] => 5055534302033 [product_name] => name [product_brand] => Brand [brand_name] => Brand name [promo_text] => [delivery_cost] => 1.99 [weight] => 2.5 [price] => 38.99 [serving] => 83 [image_url] => http://www.domain/images/10529260-1395068394-711188.jpg [large_image] => http://www.domain/images/10529260-1395068394-711188.jpg [product_url] => http://www.domain.com/10529261.html?switchcurrency=GBP&shippingcountry=GB [deep_link] => http://www.link.com/pclick.php?p=1234567890&a=123456&m=7890 [source] => N/A [protein] => 91g [carbs] => 0.2g [fat] => 1.1g [calories] => 375 [stock] => 1 [merchant] => seller [name] => Seller name [category_id] => 1 [product_id] => 6177 ) [1] => Array ( [id] => 6177 [salesID] => 5055534302033 [product_name] => name [product_brand] => Brand [brand_name] => Brand name [promo_text] => [delivery_cost] => 1.99 [weight] => 2.5 [price] => 38.99 [serving] => 83 [image_url] => http://www.domain/images/10529260-1395068394-711188.jpg [large_image] => http://www.domain/images/10529260-1395068394-711188.jpg [product_url] => http://www.domain.com/10529261.html?switchcurrency=GBP&shippingcountry=GB [deep_link] => http://www.link.com/pclick.php?p=1234567890&a=123456&m=7890 [source] => N/A [protein] => 91g [carbs] => 0.2g [fat] => 1.1g [calories] => 375 [stock] => 1 [merchant] => seller [name] => Seller name [category_id] => 1 [product_id] => 6177 ))
But this is how I want them to be returned although I’m not worried about the name as I can change that:
{"totalResults":"182","displayNumber":"25","page":[{"product":"item one","brand": "brand x","weight":"1g","serving":"6","source":"coffee","protein":"0","fat":"3","calories":"15","carbs":"0","price":"2.99","link":"http://www.somewhere.com","image":"http://www.somewhere.com/images","status":"0"},{"product":"item two","brand": "brand z","weight":".51g","serving":"2","source":"tea","protein":"0","fat":"0","calories":"5","carbs":"0","price":"12.99","link":"http://www.somewhere.com","image":"http://www.somewhere.com/images","status":"0"},{"product":"item three","brand": "brand x","weight":"1g","serving":"6","source":"coffee","protein":"0","fat":"3","calories":"15","carbs":"0","price":"2.99","link":"http://www.somewhere.com","image":"http://www.somewhere.com/images","status":"0"}]}
Thanks in advance for any help.