[eluser]ToddyBoy[/eluser]
I have a similar situation. I'm building a CMS for a niche market. The CMS will allow the user to add blog posts, add events to a calendar, create polls, manage staff, etc. I can get this to work as each page of the CMS will only require details from one model inside one controller sending the data to a single view.
The problem is for the actual site that the CMS will be source of data. For example, the home page requires recent blog posts, upcoming calendar events and the main content of the page. The way I have set it up is to have a model for each 'module' of the CMS - recent blog posts, upcoming calendar events, etc. From reading the user guide, that is what models are for - interaction with a database. I would then pass the results of the function in the model to a view to be displayed. BUT, then I need to somehow get each view 'module' to display in the main content view of the controller. Here is what I mean...
Model - blog_widget_model.php
Code:
<?php
class Blog_widget_model extends Model {
function Blog_widget_model()
{
parent::Model();
}
function display()
{
$sql = "SELECT * FROM blog LIMIT 0,3";
$blog_widget = $this->db->query($sql);
return $blog_widget;
}
}
?>
Controller - home.php (my default controller)
Code:
<?php
class Home extends Controller {
function Home()
{
parent::Controller();
$this->load->helper('url');
$this->load->helper('form');
}
function index()
{
$this->load->model('blog_widget_model');
$data['blog_model'] = $this->blog_widget_model->display();
$data['topWidget'] = $this->load->view('widgets/blog', $data, TRUE);
$this->load->model('calendar_widget_model');
$data['calendar_model'] = $this->calendar_widget_model->display();
$data['middleWidget'] = $this->load->view('widgets/calendar', $data, TRUE);
$this->load->model('content_model');
$data['content_model'] = $this->content_model->display();
$this->load->view('header');
$this->load->view('nav');
$this->load->view('content', $data, TRUE);
$this->load->view('footer');
}
}
?>
The model and widget view for the calendar is basically the same as the blog model and widget view already shown.
View - widgets/blog.php
Code:
<h2>Blog</h2>
<?php foreach($data->result() as $row): ?>
<h3><?=anchor('blog/' . $row->blog_link, $row->blog_title);?></h3>
<p><?=$row->blog_excerpt?></p>
<?php endforeach; ?>
Inside the content view, I have the following code where I would like the blog widget to display.
Code:
<?=$topWidget?>
The reason I have called it $topWidget as on other pages of the site, the blog widget won't necessarily be required, so I can display another widget instead, but still have a generic term in the main content view, as the widget code is generated earlier in the controller for that page.
The outcome of all of this is the header and nav views load correctly but the content view is blank. I've taken the calendar and content models out to see if the blog model is the problem but I can't tell if it is or isn't.
Am I asking CodeIgniter to do something it can't? From my logical point of view, I can't see too many reasons why a structure like this would not work. It's probably just a matter of changing a variable or array name to something slightly different, I'm not sure, but it's driving me nuts! I need another set of eyes with more CodeIgniter knowledge to look at it for me.
So, am I close or a long way off with the code I have above?