学习CodeIgniter - 相当于PHP

时间:2013-04-16 19:28:52

标签: php codeigniter

第一次!

我正在学习Code Igniter,作为我的第一个项目,我正在重写CI中的现有网站。

在现有网站上,所有动态或静态页面都使用PHP include来加载sidebar.php,它是从数据库中的categories表填充的。

<div id="sidebar">
<?php

$result = mysql_query('SELECT category_id, name, url FROM categories ORDER BY category_id    ASC');

while ($row = mysql_fetch_array($result)) {
$name=$row['name'];
$url=$row['url'];

print "<p><a href=\"category/$url\">$name</a></p>";

}

?>

</div>

所以现在我已经开始使用CI,我认为要采用的方法是使用数据库调用创建侧边栏模型,侧边栏控制器,侧边栏视图,然后在默认页面控制器中加载此视图。

所以,在/ application / models中我有sidebar_model.php

<?php
class Sidebar_model extends CI_Model {

public function __construct()
{
    $this->load->database();
}

public function get_categories()
{
    $query = $this->db->get('categories');
    return $query->result();    
}

}

然后在应用程序/控制器中有sidebar.php

<?php
class Sidebar extends CI_Controller {

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

}

public function index()
{
    $this->load->model('sidebar_model');

    $data['result'] = $this->sidebar_model->get_categories();

    $this->load->view('templates/sidebar_view', $data);
}


}

然后在applications / views / templates中有sidebar_view.php

<div id="sidebar">
<?php foreach($result as $row): ?>

<p><?php echo $row['name'] ?></p>

<?php endforeach ?>
</div>

这是从我的主页控制器调用的 -

<?php

class Pages extends CI_Controller {

public function view($page = 'home')
{

    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->load->view('templates/header', $data);
    $this->load->view('pages/'.$page, $data);
    $this->load->view('templates/sidebar_view', $data);
    $this->load->view('templates/footer', $data);

}
}

我遇到的麻烦是,虽然页面控制器显然正在加载侧边栏视图(框中显示的是正确的CSS样式)但它会抛出PHP错误。

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: result

Filename: templates/sidebar_view.php

Line Number: 2 

有人能指出我在正确的方向吗?虽然只是使用侧边栏的php include会很快捷,但它似乎不像MVC的做事方式。

为漫长的帖子道歉,并提前感谢!

3 个答案:

答案 0 :(得分:0)

Pages控制器中的$ data数组需要有一个“results”条目。传递给视图的数组中的值将转换为局部变量以供在视图中使用。您可以在补充工具栏控制器(您不需要)中正确执行此操作。

答案 1 :(得分:0)

您正在加载Pages控制器并调用Sidebar控制器的视图,变量$resultSidebar控制器将加载并且您正在加载Pages时获取数据您必须在sidebar控制器中加载pages的模型 喜欢

class Pages extends CI_Controller {

public function view($page = 'home')
{
  $this->load->model('sidebar_model');
  $data['result'] = $this->sidebar_model->get_categories();
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->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/sidebar_view', $data);
$this->load->view('templates/footer', $data);

}
}

答案 2 :(得分:0)

如果您需要所有页面的侧边栏,您可以这样做,首先需要扩展MY_Controller,然后使用MY_Controller扩展所有控制器

MY_Controller放入应用程序核心目录

<? 
 MY_Controller extends CI_Controller{

   public $_sidebar = '';

   public function __construct() {
      parent::__construct();
      $this->_sidebar = $this->sidebar();
   }

   private function sidebar(){
       $this->load->model('sidebar_model');

      $data['result'] = $this->sidebar_model->get_categories();

      return $this->load->view('templates/sidebar_view', $data,TRUE);
    }    

 }

现在是您的页面控制器

<?php

class Pages extends MY_Controller {

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

public function view($page = 'home')
{

    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
    $data['sidebar'] = $this->_sidebar;
    $this->load->view('templates/header', $data);
    $this->load->view('pages/'.$page, $data);
    $this->load->view('templates/footer', $data);

}
}

现在你有侧栏作为变量,每次只需在你的主模板html中定义侧边栏,你的控制器就可以使用它了

相关问题