Home > Software design >  Put Mysql Query Result info multidimensional array
Put Mysql Query Result info multidimensional array

Time:07-23

#UPDATED I have this table :product_img_detail Table

i want to put the result to make this array of json:

{
    "selectedColor": "black",
      "black": {
        "thumb": {
          "image1": "../img/e-commerce/product/dark-small-1.jpg",
          "image2": "../img/e-commerce/product/dark-small-2.jpg",
          "image3": "../img/e-commerce/product/dark-small-3.jpg"
        },
        "large": {
          "image1": "../img/e-commerce/product/dark-large-1.jpg",
          "image2": "../img/e-commerce/product/dark-large-2.jpg",
          "image3": "../img/e-commerce/product/dark-large-3.jpg"
        }
      },
    "selectedSlide": 0
  }

and this is what i've tried so far with help of @IT goldman:

$sImg = "SELECT prod_thumb160x90, prod_img1280x720 FROM product_img_detail WHERE prod_id=".$data2['id'];
$qPrd = mysql_query($sImg);
$rPrd = mysql_fetch_array($qPrd);

$imgPath = "img/e-commerce/product/";

$count = 0;
$arrSm = [];
$arrLg = [];

foreach ($rPrd as $row) {
    $count  ;
    $arrSm["image$count"] = $imgPath.$rPrd['prod_thumb160x90'];
    $arrLg["image$count"] = $imgPath.$rPrd['prod_img1280x720'];
}

$arrImg = array("thumb"=>$arrSm, "large"=>$arrLg);
$res = array("selectedColor"=>"black", "black"=>$arrImg, "selectedSlide"=>0);

echo $json = json_encode($res);

the output of above code is:

  {
"selectedColor": "black",
"black": {
    "thumb": {
        "image1": "img\/e-commerce\/product\/dark-small-1.jpg",
        "image2": "img\/e-commerce\/product\/dark-small-1.jpg",
        "image3": "img\/e-commerce\/product\/dark-small-1.jpg",
        "image4": "img\/e-commerce\/product\/dark-small-1.jpg"
    },
    "large": {
        "image1": "img\/e-commerce\/product\/dark-large-1.jpg",
        "image2": "img\/e-commerce\/product\/dark-large-1.jpg",
        "image3": "img\/e-commerce\/product\/dark-large-1.jpg",
        "image4": "img\/e-commerce\/product\/dark-large-1.jpg"
    }
},
"selectedSlide": 0

}

I'm curious, if i change the query to select * from the table, it looping until "image10", and if i change the query to select only 1 column from the table, it stop looping at "image2"!

Anyone can help me? Please help i need to restructure with right & efficient code (and unescape the forwardslash), i've already stuck for 3 days...

CodePudding user response:

After combining with @IT goldman code, i've found the answer:

    $sImg = "SELECT * FROM product_img_detail WHERE prod_id=".$data2['id'];
$qPrd = mysql_query($sImg);

$imgPath = "img/e-commerce/product/";

$count = 0;
$arrSm = [];
$arrLg = [];

//foreach ($rPrd as $row) {
while($rPrd=mysql_fetch_array($qPrd)){
    $count  ;
    $arrSm["image$count"] = $imgPath.$rPrd['prod_thumb160x90'];
    $arrLg["image$count"] = $imgPath.$rPrd['prod_img1280x720'];
}

$arrImg = array("thumb"=>$arrSm, "large"=>$arrLg);
$res = array("selectedColor"=>"black", "black"=>$arrImg, "selectedSlide"=>0);

echo $json = json_encode($res);

CodePudding user response:

You need to loop the records (rows) and aggregate to the arrays with the proper keys (according to $count) You need to loop the records (rows) and aggregate to the arrays with the proper keys (according to $count)

EDIT: I fixed usage of the now deprecated mysql_fetch_array

<?php

$sql = "SELECT * FROM product_img_detail WHERE prod_id = " . $data2['id'];
$qPrd = mysql_query($sql);
$imgPath = "../img/e-commerce/product/";

$count = 0;
$arrSm = [];
$arrLg = [];

while ($row = mysql_fetch_array($qPrd, MYSQL_ASSOC)) {
    $count  ;
    $arrSm["image$count"] = $imgPath . $row['prod_thumb160x90'];
    $arrLg["image$count"] = $imgPath . $row['prod_img1280x720'];
}

mysql_free_result($qPrd);

$arrImg = array("thumb" => $arrSm, "large" => $arrLg);
$res = array("selectedColor" => "black", "black" => $arrImg, "selectedSlide" => 0);

echo $json = json_encode($res);
  • Related