我是CI的新手。我使用 MY_Controller.php 作为主控制器。我可以将 ajax 作为 div#page 加载器打开。现在,我的问题是虽然我加载了 /about 页面,但我得到了服务模型的数据库条目。如何获取 about 控制器的 about 表?
..
function render_page($view) {
if( ! $this->input->is_ajax_request() )
{
$this->load->view('templates/header', $this->data);
}
$this->load->view($view, $this->data);
if( ! $this->input->is_ajax_request() )
{
$this->load->view('templates/menu');
$this->load->view('templates/footer', $this->data);
}
}..
我的服务模型:
class Services_model extends CI_Model {
function getAll() {
$q = $this->db->get('services');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
}
我的家庭控制器:
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$data);
}
当我在主视图中使用它们时,我可以看到 services_table 没有问题:
<ul class="blog-medium">
<?php foreach($records as $row): ?>
<li>
<div class="blog-medium-text">
<h1><a href="./post.html"><?php echo $row->title; ?></a></h1>
<p class="blog-medium-excerpt"><?php echo $row->content; ?><br />
<a href="./post.html" class="read_more">Devamını Okumak için →</a></p>
</div>
<?php endforeach ?>
我想在关于页面中使用相同的方式。关于_型号:
class About_model extends CI_Model {
function getAll() {
$q = $this->db->get('abouts');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
}
关于控制器:
public function view($page = 'about')
{
$this->load->helper('text');
$this->data['records']= $this->about_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$data);
}
这是我关于的视图文件:
<div id="content">
<?php foreach($infos as $row): ?>
<h3 style="text-align: center;"> <?php echo $row->title; ?></h3>
<div class="hr"> </div>
<?php echo $row->content; ?>
<?php endforeach; ?>
我收到错误消息告诉我:
Severity: Notice
Message: Undefined variable: infos
Filename: pages/about.php
Line Number: 3
为什么我不能得到 abouts 表?