我一直在试图学习一些PHP。我试图打印出我的数据库的内容。我一直在跟着一个教程,但遇到以下警告我根本无法转移。PHP foreach MVC Warning
警告
Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/Lab10/app/views/View.php on line 20
Foreach循环
$HTMLItemList = "";
foreach ($this->model->itemList as $row)
$HTMLItemList .= "<li><strong>" . $row ["title"] . ": </strong>" . $row ["price"] . "<blockquote>" . $row ["description"] . "</blockquote></li>";
$HTMLItemList = "<ul>" . $HTMLItemList . "</ul>";
型号 - > itemList中
public$itemList=null;
public function prepareItemList() {
$this->ItemList = $this->itemsDAO->getItems();
}
itemsDAO-> getItems()
public function getItems() {
$sqlQuery = "SELECT *";
$sqlQuery .= "FROM items";
$sqlQuery .= "ORDER BY items.title;";
$result = $this->getDbManager() -> executeSelectQuery ($sqlQuery);
return $result;
}
您的项目列表为空或坏的类型,你确定你是prepareItemList()的foreach之前和itemList中是数组打电话? – zdeniiik 2015-03-31 10:57:58
上面的代码是我一直在做的,我应该在我的foreach循环之前调用prepareItemList()几行吗? – patrick 2015-03-31 11:01:25
是的,我想。在访问itemList之前必须调用prepareItemList() – zdeniiik 2015-03-31 11:03:39