CRUD Question with Codeigniter
I have a database with blog entries in it. The desired output I am trying to acheive is 3 entrys displayed using the css3 multi paragraph and then another 3(or more) formatted with the codeigniter Character_limiter. So I need 3 displays formatted one way and 3+ formatted another way on the same page.
So far this is what I have, but i do not know how to format the sql to achieve what I want. I can call all posts in descending order like I want, but dont know how to separate the code to achieve my output Controller:
$this->db->order_by('id', 'DESC');
$this->db->limit('2');
$query = $this->db->get('posts');
if($query->result())
$data = array();
{
$data['blog'] = $query->result();
}
$data['title'] = 'LemonRose';
$data['content'] = 'home/home_content';
$this->load->view('template1', $data);
View:
<?php if (isset($blog)): foreach ($blog as $row): ?>
<span class="title"><?php echo $row->title; ?> </span>
<?php echo $row->date; ?>
<div class="split"><?php echo $row->post =$this->typography->auto_typography($row->post); ?></div>
<?php echo 'Post #',$row->id; ?>
<p> Trackback URL: <? echo base_url()."trackbacks/track/$row-开发者_运维知识库>id";?></p>
<hr />
<?php endforeach;
endif;
?>
The split class is multiple columns. I tried 2 different querys but dont know how to separate all the post displays. Also one query always overides the second and produces all split or all character limited paragraphs.
Im lost here lol. Thanks
I need 3 displays formatted one way and 3+ formatted another way on the same page.
This sounds like display logic, which means it's a job for your view. Your controller can simply do a single query getting all or some of the entries (say the most recent 10) and pass it off to the view to decided how to display the data.
In your view you can simply use conditional logic in your foreach() loop to determine how to output the post data.
$limit = 3; // how many posts should we show in full?
$i = 1; // count
foreach ($blog as $row):
if( $i < $limit ) // we are under our limit
{
echo $row->post; // show full post
}
else // we are over our limit
{
echo $this->typography->auto_typography($row->post); // show truncated post
}
$i++; // increase count
endforeach;
unset($limit,$i,$row)
This means your controller could pass the full data to any number of views, each having their own way to display the data, and the view above could be called by any controller and faithfully display the posts passed to it in the way prescribed.
If this doesn't sit well with you, then you can manipulate the data in your controller or model (or create a helper function) with use of array_walk() and a helper function:
// user function for array_walk
function truncate_post(&$value,$key,$limit)
{
if($key >= $limit)
{
$value->post = $this->typography->auto_typography($value->post);
}
}
$query = $this->db->get('posts', 10); // get 10 posts
$results = $query->result();
array_walk($results, 'truncate_post', 3); // truncate all but top 3 posts
$data['blog'] = $results;
$this->load->view('template1', $data);
精彩评论