0

I have a function to convert record from SQL query result into JSON array. This is my function:

function createJson(){
    $dir="img/e-commerce/product/"
    $sql=mysql_query("SELECT product_list.id AS listId, product_list.class AS listClass, 
                            product.prod_name AS prodName, product.prod_url AS prodUrl, 
                            product.prod_overview AS prodOverview, 
                            product_img.list_prod340x340 AS prodListImg, 
                            MIN(pricelist.price) AS price 
                    FROM (((product_list 
                        INNER JOIN product_img ON product_list.id = product_img.prod_list_id) 
                        INNER JOIN product ON product_list.id = product.prod_list_id) 
                        INNER JOIN pricelist ON product.id = pricelist.prod_id) 
                    GROUP BY listId, listClass, prodName, prodUrl, 
                            prodOverview, prodListImg 
                    ORDER BY product_list.id, price ASC");
 
    while($prodList= mysql_fetch_array($sql)){
        $data['items'][] = array( 
            'name' => $prodList['prodName'],
            'description' => $prodList['prodOverview'],
            'price' => $prodList['price'],
            'image' => $prodList['prodListImg'],
            'category' => $prodList['listClass'],
            'prod_url' => $prodList['prodUrl'],
            ));
    }
    $path = 'api/';
    $jsonfile = json_encode($data, JSON_PRETTY_PRINT);
    $fcon = file_put_contents($path.'anggota.json', $jsonfile);}

And this is how the result looks like:

{
    "items": [
        {
            "name": "Toyota Agya",
            "description": "Ringkasan produk",
            "price": "155500000",
            "image": "..\/img\/e-commerce\/producttoyota_agya_340x340.jpg",
            "category": "Hatchback",
            "prod_url": "harga-mobil-toyota-agya-terbaru"
        },
        {
            "name": "Toyota Calya",
            "description": "overview produk",
            "price": "151600000",
            "image": "..\/img\/e-commerce\/product\/toyota-calya_340x340.jpg",
            "category": "MPV",
            "prod_url": "toyota-calya"
        }
    ]}

It keep adding \ before /. Can you help me solve this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    If you are actually using the old `mysql_` database extension, then you are using a version of PHP that is way past its sell by date and is very insecure – RiggsFolly Jul 10 '22 at 08:06
  • For the `\/` I would check that your actual data records, it's possible the strings are being escaped when writing to the database, causing your output to look this way. – SnareChops Jul 14 '22 at 17:46

1 Answers1

0

A minor change to how you address the array as you build it is all you need

$data['items'][] = array( 
        'name' => $prodList['prodName'],
        'description' => $prodList['prodOverview'],
        'price' => $prodList['price'],
        'image' => $prodList['prodListImg'],
        'category' => $prodList['listClass'],
        'prod_url' => $prodList['prodUrl'],
        );
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149