【问题标题】:View shows a blank page with no error查看显示没有错误的空白页面
【发布时间】: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


【解决方案1】:

在您的控制器中,尝试删除第三个参数 (TRUE)。

这用于返回数据(如JSON等)

    //this->load->view('pro_index', $data, TRUE);
    this->load->view('pro_index', $data);

【讨论】:

  • 谢谢!删除 TRUE 修复了它。哇!
【解决方案2】:

试试这个

public function index()
{
$this->load->model('product');
$this->data['products']=$this->product->get_products();
$this->load->view('pro_index', $this->data, TRUE);
}

【讨论】:

  • 遇到致命错误:无法访问 $this->data ['products']=$this->product->get_products () 上的空属性;
  • 我认为函数调用是错误的 $this->product->get_products();到 $this->get_products();
  • 这样做会返回致命错误:在同一行调用未定义的方法::get_products ()
  • 将模型重命名为 Product_model
  • 将模型重命名为 Product_model 加载模型为 $this->load->model('Product_model');更改此行 $this->product->get_products();到 $this->Product_model->get_products();
【解决方案3】:

您正在将带有“$data”的数组数据返回到您的视图页面,并且您正在使用“foreach ($products as $row)”进行循环。您必须将 foreach 循环 $products 更改为 $data。

在模型中

$this->load->view('pro_index', $data, TRUE);

在查看页面中

foreach ($products 作为 $row)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 2018-11-16
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2017-09-05
    • 2021-06-11
    • 1970-01-01
    相关资源
    最近更新 更多