2016-04-28 114 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,它不顯示數據 –