gpt4 book ai didi

php - Magento 选择查询未使用 foreach 循环正确循环

转载 作者:行者123 更新时间:2023-11-29 03:25:25 25 4
gpt4 key购买 nike

我写了下面的代码:

try {
$json = array('success' => true);
$read = $this->read;
$readresult = $read->fetchAll("SELECT * FROM brand");

foreach($readresult as $r) {
$json['brands'][] = array(
'id' => $r['brand_id'],
'name' => $r['name'],
'description' => $r['description'],
);
}

return $json;
} catch (Exception $e) {
$message = $e->getMessage();
echo json_encode(array("status" => "500", "error" => $message));
}

在此代码中,我试图显示数据库表中的所有品牌记录。

但问题是当我试图输出结果时它只显示一条记录。

谁能检查一下是什么问题。

上面代码的输出是:

{
"success":true,
"products":[
{
"id":"4",
"name":"Test",
"href":"http:\/\/localhost:8‌​1\/magento\/index.php\/catalog\/product\/view\/id\/4\/",
"thumb":"http:\/\/localho‌​st:81\/magento\/media\/catalog\/product\/cache\/0\/thumbnail\/9df78eab33525d08d6e‌​5fb8d27136e95\/images\/catalog\/product\/placeholder\/thumbnail.jpg",
"pirce":"$11‌​1,111.00"
}
]}

最佳答案

这样试试,

     $json['brands'] = array();
foreach($readresult as $r) {
$brand = array(
'id' => $r['brand_id'],
'name' => $r['name'],
'description' => $r['description'],
);
array_push($json['brands'],$brand);
}

关于php - Magento 选择查询未使用 foreach 循环正确循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36910307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com