2016-04-28 109 views
2

我已经写了下面的代码正确:Magento的选择查询不循环使用foreach循环

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" 
    } 
]} 
+0

家伙请人帮助没有人在那里? –

+0

如果您还需要其他东西,您也可以告诉我 –

回答

0

尝试这样,

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

它返回null –

+0

您正在使用具有不同形式的$ json ['brands']数组使用品牌信息检索产品。在此代码之前检查$ json ['brands']数组,并在此代码之后对产品检索代码进行适当更改。 – Shivanand

+0

结果显示1,它不显示数据 –