【发布时间】:2016-07-01 01:59:46
【问题描述】:
目前正在学习 CodeIgniter,使用版本 3.0、PHP 5.5、WAMP 上的 MySQL 和 Sublime Text 3 作为我的编辑器。我正在尝试加载视图,但即使打开了 error_reporting,页面也是空白且没有错误。当我回显我的数组时,它会显示数据。下面是我的表格和代码
以下是我的模型、控件和视图的代码
模型(product.php)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Model {
function get_products() {
$this->db->select()->from('products')->order_by('name','desc');
$query=$this->db->get();
return $query->result_array();
}
}
控制器(products.php)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Products extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->model('product');
$data['products']=$this->product->get_products();
$this->load->view('pro_index', $data, TRUE);
//echo "<pre>"; print_r($data['products']); "</pre>";
}
}
查看(pro_index.php)
<!DOCTYPE html>
<html lang="">
<head>
<!-- Required meta tags always come first -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title Page</title>
</head>
<body>
<?php
if (isset($products)) {
foreach ($products as $row) {
echo "<div>"."<h2>".$row["name"]."</h2>"."</div>";
echo "what is this?";
}
}
?>
</body>
</html>
我可能做错了什么?
【问题讨论】:
-
你试过 print_r($products);在视图中?
-
刚刚做了,结果还是一样
-
尝试分别打开不同的 PHP 文件,看看它们是否会产生致命错误。
-
尝试回显 print_r($data['products']);在你的控制器中
-
确保模型产生正确的数据
标签: php mysql codeigniter