首页 > 解决方案 > Codeigniter 数据库未显示在视图中

问题描述

我正在学习 CI,我正在尝试执行本教程来获取数据。我的浏览器只是转储这个,没有别的

{条目} {id}

{标题} {新闻}

{/条目}

你能帮我解决这个问题吗?我很确定这与控制器文件中的解析器有关

现在这是我的观点:

<head>
<title>News Blog</title>
</head>
<body>
       {entries}
           <p>{id}</p>
           <h3>{title}</h3>
           <p>{news}</p>
       {/entries}
</body>

模型

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class NewsModel extends CI_Model {

    public function __construct() {
    $this->load->database();
        
    }
    
    public function getNews($slug = FALSE){
            $query = $this->db->get('news');
            return $query->result_array();

    }
    
}


和控制器

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class News extends CI_Controller {

    public function __construct() {
        parent::__construct();
        

        $this->load->model('newsModel');
    }

    public function index() {
   
        $news = $this->NewsModel->getNews();

// I believe the issue is in the next few lines

        $data['contactjs']=$this->parser->parse('templates/Javascript/contactjs',[],TRUE);
        $data['bootCSS']=$this->parser->parse('templates/CSS/bootCSS',[],TRUE);
        $data['CSS']=$this->parser->parse('templates/CSS/CSS',[],TRUE);
        $data['jQuery']=$this->parser->parse('templates/Javascript/jQuery',[],TRUE);
        $data['bootstrap']=$this->parser->parse('templates/Javascript/bootstrap',[],TRUE);



         $template = '{id} {title} {news}';
         $newsData = array('entries'=> $news);
         $newsData = $this->parser->parse('pages/news', [], TRUE);
         $data['news'] = $this->parser->parse('pages/news', $news, TRUE);
        
        $this->load->view('templates/header', $data);
        $this->load->view('pages/news');
        $this->load->view('templates/footer');
    }

}


标签: phpmysqlcodeignitercontroller

解决方案


你加载了解析器库吗?尝试输出 $news 变量,看看它是否包含一些数据。此外,请检查您的应用程序/日志文件夹以获取更多信息。可能有一个你没有看到的 php 错误。

下一行代码应该像没有解析器的任何其他视图一样加载您的页面/新闻视图:

$this->load->view('pages/news'); // This view doesn't contain any parsed data now

我认为您应该创建一个临时变量并将所有已解析视图的所有数据放入其中。然后将其加载到视图中。要检查,请尝试输出最后一个变量,看看它是否包含任何类似的数据;

echo "<pre>"; print_r($data['news']);

推荐阅读