[eluser]Crazy88s[/eluser]
Hi,
Working on my first application using CI. What's the best practice in regards to conditional code and loops. Is it better to do it in the View or in the Model.
For example, in my model I have the following code:
Code:
function getSermonsForSideBar ()
{
$this->db->orderby('daterecorded desc');
$query = $this->db->get('sermons');
if ($query->num_rows() > 0) {
$output = '<ul>';
foreach ($query->result() as $sermon) {
$output .= '<li><a href="/podcasts/'.$sermon->FileName.'">'.$sermon->Title.'</a></li>';
}
$output .= '</ul>';
return $output;
} else {
return '<p>Sorry, no sermons on file.</p>';
}
}
And in the controller I have:
Code:
function index()
{
$data['SermonSideBarContent'] = $this->sermon->getSermonsForSideBar();
$this->load->view('common\header');
$this->load->view('common\index', $data);
$this->load->view('common\footer');
}
As you can see, I'm building the unorder list here in the model and passing the complete result back to the controller.
OR
In the view I could do:
Code:
<?php
if($query->num_rows() > 0):
foreach ($query->result() as $row):
?>
<ul>
<li>
<?php echo $row->Title; ?>
</li>
</ul>
<?php
endforeach;
endif;
?>
The view method seems kinda messy considering I could just put <?= $SermonSideBarContent;?> in the view and let the model handle the rest.
What's the best practice here?
Thanks!