2017-01-27 28 views
0

當我嘗試print_r($結果)它獲取數組。數組([0] =>數組([id] => 6 [0] => 6 [ref] => GSO 646 & 647 [1] => GSO 646 & 647)[1] => ] => 7 [0] => 7 [ref] => GSO 962 [1] => GSO 962)[2] =>陣列([id] => 8 [0] => 8 [ref] => GSO ASTM F 1923 [1] => GSO ASTM F 1923)錯誤獲取我的數組MVC

但我有一個錯誤,也許錯誤是在foreach,因爲它說無效的供應foreach()。任何人都可以幫我嗎?謝謝你

模型

require_once("DatabaseManager.php"); 

class Model 
{ 

    public $connection; 

    public function __construct(){ 
     $connection = new DatabaseManager(); 
     $this->connection = $connection->getConnection(); 
    } 

    public function getAllRecords() 
    { 
     $sql = "SELECT * FROM reference"; 
     $query = $this->connection->query($sql); 
     $result = $query->fetchAll(); 
     //print_r($result); 
    } 
} 

控制器

require_once("../model/Model.php"); 

class Controller 
{ 
    private $model; 

    public function __construct() 
    { 
     $this->model = new Model(); 
    } 

    public function displayAllRecords() 
    { 
     $result = $this->model->getAllRecords(); 
     return $result; 
    } 
} 

VIEW

require '../controller/Controller.php'; 

$controller = new Controller(); 

foreach($controller->displayAllRecords() as $records) { 
. 
. 
} 
+0

你必須渲染或拋從控制器東西來查看。 –

+0

你是什麼意思@SaugatBhattarai?我試圖把返回$結果;仍然有錯誤 –

+0

Array([0] => Array([id] => 6 [0] => 6 [ref] => GSO 646&647 [1] => GSO 646&647)[1] => Array([id ] => 7 [0] => 7 [ref] => GSO 962 [1] => GSO 962)[2] =>陣列([id] => 8 [0] => 8 [ref] => GSO ASTM F 1923年[1] => GSO ASTM F 1923)時,我的print_r($結果); –

回答

0

嘗試返回$這個 - >模型 - > getAllRecords();從您的控制器displayAllRecords()方法,這樣

public function displayAllRecords() 
{ 
    return $this->model->getAllRecords(); 
} 

public function getAllRecords() 
    { 
     $sql = "SELECT * FROM reference"; 
     $query = $this->connection->query($sql); 
     return $result = $query->fetchAll(); 
     //print_r($result); 
    } 
+0

仍然得到一個錯誤.. –

+0

請給你的錯誤文本 – Aram810

+0

警告:的foreach()提供的無效參數 –