#更新了这个表:明细表
我想把结果放在这个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
}到目前为止,在@IT高盛的帮助下,我一直在努力做到这一点:
$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);上述代码的输出如下:
{
"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}
我很好奇,如果我将查询更改为从表中选择*,它一直循环到"image10",如果我将查询更改为只从表中选择1列,则在"image2“处停止循环!
有人能帮我吗?请帮助我用正确有效的代码进行重组(并解开正反斜杠),我已经困了3天了.
发布于 2022-07-22 16:28:49
在与@IT高盛代码结合之后,我找到了答案:
$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);发布于 2022-07-22 09:50:31
您需要循环记录(行)并使用正确的键(根据$count)聚合到数组,您需要循环记录(行),并使用正确的键(根据$count)聚合到数组中。
编辑:我修正了现在不再推荐的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);https://stackoverflow.com/questions/73078195
复制相似问题