php - 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');
}
}
解决方案
你加载了解析器库吗?尝试输出 $news 变量,看看它是否包含一些数据。此外,请检查您的应用程序/日志文件夹以获取更多信息。可能有一个你没有看到的 php 错误。
下一行代码应该像没有解析器的任何其他视图一样加载您的页面/新闻视图:
$this->load->view('pages/news'); // This view doesn't contain any parsed data now
我认为您应该创建一个临时变量并将所有已解析视图的所有数据放入其中。然后将其加载到视图中。要检查,请尝试输出最后一个变量,看看它是否包含任何类似的数据;
echo "<pre>"; print_r($data['news']);
推荐阅读
- android - 如何在不添加活动的情况下添加用于在片段之间滑动的 viewPager
- javascript - 使用带有登录系统的 Electron 在 2 个 HTML 文件之间传递输入变量
- symfony - Symfony Doctrine DQL 如何在主 SELECT 的 SUM 中添加 SELECT
- reactjs - React 测试组件模拟点击不识别功能
- python - 熊猫中列的可能组合
- java - 如何在 Activity 对 ViewModel 的 LiveData 对象的观察调用中为 ActionBar 执行 setTitle()?
- c++ - 如何找到矩阵中特定项目之间的最短路径?
- javascript - 反应头盔未在视图页面源中显示元标记,但在检查时显示在元素中
- java - 尝试在空对象引用新活动上调用虚拟方法“java.lang.String android.content.Context.getPackageName()”
- javascript - React Native 不要在 componentDidMount 中使用 setState (react/no-did-mount-set-state)