Hello Readers, If you are using CodeIgniter you want to fetch limited MySQL results then in this blog we will learn how to to this.
Suppose you want only first 200 chars of a field then you should only fetch limited results, this will reduce the unnecessary request and speed up your page speed. So let's get started building this.
Step 1:- First create a model and write its MySql syntax for fetching the limited results. It will go like this:-
public function viewBlogs($page_num,$per_page)
{
$this->db->select('blogs.id, title, LEFT(content , 200) as content, blogs.modified, user_id, users.first_name, users.last_name, photo');
$this->db->from('blogs');
$this->db->join('users','users.id = blogs.user_id');
$this->db->join('blog_photos','blog_photos.blog_id = blogs.id');
$this->db->where('blogs.deleted', '0');
$this->db->where('blogs.status', '1');
$this->db->order_by("id", "desc");
$this->db->limit($page_num, $per_page);
$get_list = $this->db->get()->result_array();
//echo "<PrE>";print_r($get_list);die();
return $get_list;
}
In the code above we are fetching data for showing featured blogs on the blog page. Every blog will show its photo and first 200 chars of its content. Also, you can increase of decrease this count by changing this number. The output of this query will go like this:-
0 Comment(s)