Какое-то время назад у меня получалось довольно легко ...
Взгляните на простой пример того, как я это сделал ... Это было для сайта помощи.
Модель
<?php
class Template_model extends Model {
function Template_model()
{
parent::Model();
}
function load_pages()
{
$data = array();
$this->db->where('status',0);
$this->db->order_by('sort', 'ASC');
//$this->db->get('pages');
$query = $this->db->get('pages');
if ($query->num_rows() > 0){
foreach ($query->result_array() as $row){
$data[] = array(
"name" => $row['name'],
"url" => $row['url']
);
}
}
$query->free_result();
return $data;
}
}
Контроллер
<?php
class View extends Controller {
function View()
{
parent::Controller();
}
function index()
{
redirect('view/topic/orders');
//echo "oops";
}
function topic()
{
$page = $this->uri->segment(3);
//get the page id
$this->load->model('view_model');
$id = $this->view_model->get_page_id_by_url($page);
$id = $id['id'];
//get the page title
$data['title'] = $this->view_model->get_page_title($id);
//load the post by page id
$data['posts'] = $this->view_model->get_page_posts($id);
//load the header
$this->load->model('template_model');
$data['pages'] = $this->template_model->load_pages();
//load the view
$data['main_content'] = 'view/page';
$this->load->view('includes/template', $data);
}
function article()
{
$url = $this->uri->segment(3);
//get the post by url
$this->load->model('view_model');
$data['post'] = $this->view_model->get_post_by_url($url);
$id = $data['post']['page_id'];
//print_r($data['post']);
//get the page title
$data['title'] = $this->view_model->get_page_title($id);
//load the post by page id
$data['posts'] = $this->view_model->get_page_posts($id);
//load the header
$this->load->model('template_model');
$data['pages'] = $this->template_model->load_pages();
//load the view
$data['main_content'] = 'view/article';
$this->load->view('includes/template', $data);
}
}
/* End of file view.php */
/* Location: ./system/application/controllers/view.php */
и вид ..
<div id="navigation">
<ul>
<?php
if(!empty($pages))
{
foreach($pages as $page)
{
?>
<li <?php if($on_page == $page['url']) { echo 'class="selected"'; }?>><a href="<?php echo base_url().'view/topic/'.$page['url'];?>"><?php echo $page['name'];?></a></li>
<?
}
}?>
<!-- top navigation use selected class for selected item -->
<div id="rNav" ><ul><li <?php if($on_page == 'admin') { echo 'class="selected"'; }?>><a href="<?php echo base_url();?>admin">Admin</a></li></ul></div>
<div id="rNav" ><ul><li <?php if($on_page == 'logout') { echo 'class="selected"'; }?>><a href="<?php echo base_url();?>logout">Logout</a></li></ul></div>
</ul>
</div>