简体   繁体   中英

Put Mysql Query Result info multidimensional array

#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...

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);

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

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

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

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

echo $json = json_encode($res);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM