code igniter pagination for displaying specific data from database? - codeigniter

I want to display some specific data from database through pagination using CI.but pagination by default passess parameter in function which creates problem for me.
Here is my Controller
function page($catagoryid){
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/pagination/page/';
$count = $this->db->query("select * from tbl_products where category_id='$categoryid'");
$total=$count->num_rows();
$per_page = 4;
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->model("mpagination");
$data['list'] = $this->mpagination->get_s(,$categoryid,$config['per_page'],$this->uri->segment(3));
if ($data['list']!== null){
$this->load->view('pagination_view', $data);
}
else {
$this->load->view('noresult');
}
}
Here is my Model
function get_s($categoryid,$num, $offset) {
$this->db->select ('*');
->where('category_id',$categoryid); // field name
$sql = $this->db->get('tbl_products',$num, $offset); // table name
if ($sql->num_rows () >0) {
return $sql->result();
}
else {
return null;
}
}
Here is my view
<?php
foreach($list as $row):?>
<?php echo $row->_prduct_id." ".$row->name." ".$row->description;?>
<br/>
<?php endforeach;?>
<br/>
<?php echo $pagination; ?>
This code work at first but when i clicked on second link of pagination it will not work because by default pagination sends its value to url as parameter and doesnot show data.
what can i do?

I didn't understand your code properly but this is the way you need to do pagination.
public function page($category_id)
{
$result = $this->model_file->model_function($category_id);
$start_index = 0;
$total_rows = count($result);
$items_per_page = 10;
$filtered = array_splice($result,$start_index,$items_per_page);
$model['data'] = $filtered;
$model['page_link'] = create_page_links (base_url()."/controller_file_name/page", $items_per_page, $total_rows);
$this->load->view('view_file_name_path',$model);
}
function create_page_links($base_url, $per_page, $total_rows) {
$CI = & get_instance();
$CI->load->library('pagination');
$config['base_url'] = $base_url;
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$CI->pagination->initialize($config);
return $CI->pagination->create_links();
}

Try like this
public function index($offset = 0)
{
$language_id = 1;
$this->load->library('pagination');
$limit = 10;
$total = $this->legend_model->get_legend_count($language_id);
$config['base_url'] = base_url().'legend/index/';
$config['total_rows'] = $total;
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['first_link'] = '<< First';
$config['last_link'] = 'Last >>';
$config['next_link'] = 'Next ' . '>';
$config['prev_link'] = '<' . ' Previous';
$config['num_tag_open'] = '<span class="number">';
$config['num_tag_close'] = '</span>';
$config['cur_tag_open'] = '<span class="current"><a href="#">';
$config['cur_tag_close'] = '</a></span>';
$this->pagination->initialize($config);
$data['offset'] = $offset;
$data['legends'] = $this->legend_model->get_legend($language_id, $limit, $offset);
$this->template->write('title', 'Legend : Manage Legend');
$this->template->write_view('content', 'legend/index', $data);
$this->template->render();
}

Related

pagination not working in codeigniter

my controller page
public function index() {
$data['error']="";
$data['h']="";
$this->load->library('pagination');
$config['base_url'] = base_url().'Imagec/index';
$config['total_rows'] = 10;
$config['per_page'] = 2;
$config["uri_segment"] = 4;
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data['h']=$this->Inserts_model->select($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view('select_view', $data);
}
my mode page
public function select($limit, $start)
{
$this->db->limit($limit, $start);
$query = $this->db->get('student');
return $query->result();
}
my view page
<p><?php echo $links; ?></p>
here all my code here when click on links the NOT FOUND error occur's
Try this 100% will work
Controller :
public function index() {
$data['error'] = "";
$data['h'] = "";
$this->load->library('pagination');
$data['h'] = $this->inserts_model->select();
$config['base_url'] = base_url() . '/index.php/imagec/index';
$config['total_rows'] = count($data['h']);//it will give you total no of records
$config['per_page'] = 2;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['h'] = $this->inserts_model->select($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
// echo "<pre>";print_r($data);die;
$this->load->view('select_view', $data);
}
Model :
public function select($limit=0, $start=0) {
if (empty($start) && !empty($limit)) {
$this->db->limit($limit);
}
if (!empty($start) && !empty($limit)) {
$this->db->limit($limit, $start);
}
$query = $this->db->get('student');
return $query->result();
}

Can't display all item if order by field nama

I have model code function in Codeigniter like below, if me sort order by RAND() can display all item but if order by field name like part_no can't display all item, in next page same display with page 1.
Model
function listProductPerCategory($id, $limit, $start){
$this->db->select('
category.id, category.category, product.id, product.id_category,
product.name, product.picture, product.description, product.permalink, product.part_no
');
$this->db->join('category', 'product.id_category = category.id');
$this->db->where('product.id_category', $id);
$this->db->order_by('product.part_no');
$this->db->limit($limit, $start);
$query = $this->db->get($this->tableProduct);
return $query->result();
}
Controller
function listcategory($page = NULL) {
$id = $this->input->get('list');
$data['categoryHead'] = $this->M_home->listCategory();
$this->load->view('frontend/template/header', $data);
$data['productsLast'] = $this->M_home->listProductLast();
$data['productsLast2'] = $this->M_home->listProductLast2();
$data['category'] = $this->M_home->listCategory();
//paging
$config['base_url'] = base_url('categoryproduct'.'?'.http_build_query($_GET));
$config['total_rows'] = $this->M_home->listProductPerCategory_num_rows($id);
$config['per_page'] = $per_page = 12;
$config['uri_segment'] = 2;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev';
$config['page_query_string'] = TRUE;
//end paging
$this->pagination->initialize($config);
$data['paging'] = $this->pagination->create_links();
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data['listCategory'] = $this->M_home->listProductPerCategory($id, $per_page, $page);
$this->load->view('frontend/category', $data);
$this->load->view('frontend/template/footer');
}
You have understand that
In SQL LIMIT, start is starting number of record not PAGE.
But from what you passing to listProductPerCategory is page number
So from page 1 to page 2 it is only one record move on next page not 12 records.
So you need to fix in function listProductPerCategory (dont forget to change variable name which may you confuse yourself , change $start -> $page)
So you should get this as below
function listProductPerCategory($id, $limit, $page){
if($page < 1){
$page = 1;
}
if($page > 1){
$start = $limit * $page;
}else{
$start = 0;
}
$this->db->select('
category.id, category.category, product.id, product.id_category,
product.name, product.picture, product.description, product.permalink, product.part_no
');
$this->db->join('category', 'product.id_category = category.id');
$this->db->where('product.id_category', $id);
$this->db->order_by('product.part_no');
$this->db->limit($limit, $start);
$query = $this->db->get($this->tableProduct);
return $query->result();
}
Controller -
Try this -
function listcategory($page = 0){
$id = $this->input->get('list');
$data['categoryHead'] = $this->M_home->listCategory();
$this->load->view('frontend/template/header', $data);
$data['productsLast'] = $this->M_home->listProductLast();
$data['productsLast2'] = $this->M_home->listProductLast2();
$data['category'] = $this->M_home->listCategory();
//paging
$config['base_url'] = base_url('categoryproduct'.'?'.http_build_query($_GET));
$config['total_rows'] = $this->M_home->listProductPerCategory_num_rows($id);
$config['per_page'] = $per_page = 12;
$config['num_links'] = 2;
$config['uri_segment'] = 3;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev';
$config['page_query_string'] = TRUE;
//end paging
$this->pagination->initialize($config);
$data['paging'] = $this->pagination->create_links();
$data['page'] = $page;
// $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data['listCategory'] = $this->M_home->listProductPerCategory($id, $per_page, $page);
$this->load->view('frontend/category', $data);
$this->load->view('frontend/template/footer');
Order by condition is wrong in your model.
Try this -
$this->order_by('title','ASC or DESC or RANDOM');

i have set $config['per_page'] = 20 but it get only two result

i am using codeigniter pagination with per page 20 result but it get only two result when i click on pagination mean page number like 2, 3, 4
following is my controller
function restaurant_listing()
{
$config['base_url'] = base_url() . 'admin/restaurant/restaurant_listing/';
//$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
//$config['base_url'] .= preg_replace('#/+$#','',dirname($_SERVER['SCRIPT_NAME'])).'/';
$config['total_rows'] = $this->restaurant_model->record_count();
$config['per_page'] = 20;
$config['uri_segment'] = 4;
$config['num_links'] = 10;
$config['use_page_numbers'] = TRUE;
$config['cur_tag_open'] = '<b>';
$config['cur_tag_close'] = '</b>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data["results"] = $this->restaurant_model->restlisting($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$data['content'] = $this->load->view('admin/restaurant_listing', $data, true);
$this->load->view('admin/template', $data);
}
my view file contain
<div class="pagination"> <?php echo $links; ?> </div>
here i am showing pagination
maybe can fix your code i use this code for pagnation
controller code
public restaurant_listing()
{
$restaurant=$this->uri->segment(4);
$limit_ti=20;
if(!$restaurant):
offset_ti = 0;
else:
$offset_ti = $restaurant;
endif;
$this->load->model('Restaurant_model');// call model
$this->load->library('pagination'); // call library
$query=$this->restaurant_model->restlisting($limit_ti,$offset_ti); // geting an article
$total_page = $this->restaurant_model->record_count(); // count row
/** Pagination **/
$config['base_url'] = base_url().'admin/restaurant/restaurant_listing/';
$config['total_rows'] =$total_page->num_rows();
$config['per_page'] = 20;
$config['uri_segment'] = 4;
$config['num_links'] = 10;
$config['use_page_numbers'] = TRUE;
$config['cur_tag_open'] = '<b>';
$config['cur_tag_close'] = '</b>';
$this->pagination->initialize($config);
$data = array('query' => $query,'page'=>$restaurant);
$data['content'] = 'admin/restaurant_listing';
$this->load->view('template', $data);
}
models code
function restlisting($limit,$ofset){
$query=$this->db->query("select * from *name table* order by id ASC LIMIT $ofset,$limit");
return $query;
}
function record_count(){
$query=$this->db->query("select * from *name table*");
return $query;
}
view code
<div class="pagination"><?php echo $this->pagination->create_links(); // call the pagnation?></div>

codeigniter pagination links doesnt work properly

I am using Codeigniter library pagination for my search.The problem is even if i have 2 results from the database table,clicking the next or previous link shows only one result.
code :
controller
function search()
{
$this->load->library('pagination');
$perpage =1;
$page = 1;
$search_content = $this->input->get('con_search');
$data->search = $this->public_model->search_content($search_content, $perpage);
$total = count($this->public_model->search_content($search_content));
$config['base_url'] = current_url()."?con_search=".$search_content;
$config['total_rows'] = $total;
$config['per_page'] = 1;
$config['cur_page'] = $page;
$config['use_page_numbers'] = TRUE;
$config['query_string_segment'] = "page";
$config['display_pages'] =TRUE;
$config['page_query_string'] = TRUE;
$config['cur_tag_open'] = '<span class="current">';
$config['cur_tag_close'] = '</span>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$config['prev_tag_open'] = '<span class="prevtag">';
$config['prev_tag_close'] = '</span>';
$config['next_tag_open'] = '<span class="nexttag">';
$config['next_tag_close'] = '</span>';
$this->pagination->initialize($config);
$data->footer = $this->public_model->get_footer();
$this->load->helper('text');
$this->load->view('header', $data);
$this->load->view('search');
$this->load->view('footer');
}
model:
function search_content($search_txt, $perpage = 0)
{
if($perpage){
$this->db->limit($perpage);
}
$this->db->select('title, description, alias');
$this->db->where('status', 'yes');
$this->db->like('description', $search_txt);
$res = $this->db->get('tbl_content')->result_array();
return $res;
}
view :
if($search && is_array($search)) {
foreach($search as $ind=>$val)
{
$this->db->select('menu_type_id');
$this->db->where('menu_alias', $val['alias']);
$res = $this->db->get('tbl_menu')->row_array();
if($res) {
if($res['menu_type_id'] == 2)
$url = site_url('page/'.$val['alias']);
else
$url = site_url('page/footer/'.$val['alias']);
}
?>
<div class="box_1">
<div class="deal_contents">
<div class="bx_0"><?php echo $val['title'] ?></div>
<div class="bx_1" style="width:670px;"><?php echo strip_tags(character_limiter($val['description'],200)) ?></div>
<div class="bx_2" style="text-align:right; width:640px;">Read More</div>
</div>
</div>
<?php
}
echo $this->pagination->create_links();
}
thanks in advance
Do like this
$this->load->library('pagination');
$limit = 10;
$total = $this->legend_model->get_legend_count($language_id);
$config['base_url'] = base_url().'legend/index/';
$config['total_rows'] = $total;
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['first_link'] = '<< First';
$config['last_link'] = 'Last >>';
$config['next_link'] = 'Next ' . '>';
$config['prev_link'] = '<' . ' Previous';
$config['num_tag_open'] = '<span class="number">';
$config['num_tag_close'] = '</span>';
$config['cur_tag_open'] = '<span class="current"><a href="#">';
$config['cur_tag_close'] = '</a></span>';
$this->pagination->initialize($config);
$data['offset'] = $offset;
$data['legends'] = $this->legend_model->get_legend($language_id, $limit, $offset);
$this->template->write('title', 'Legend : Manage Legend');
$this->template->write_view('content', 'legend/index', $data);
$this->template->render();

issue with pagination and limit

I have 3 tables site_settings,sermons & Preachers. In site_settings table, set the max number of preachers to be displayed. Preacher table contains all the preacher details and sermons table contains sermons of all preachers. Am using pagination for displaying preachers(2/page). if the value of site_settings table is 1 or 2 then no of preachers will display 2, if 3 or 4 then preachers 4... I don't know what is happening. Is it a problem with pagination or limit? I am using the following code for this ( In Model)
Method in Model
function viewAll($offset=0, $limit=null) {
$this->db->select('settings_value');
$this->db->from('site_settings');
$this->db->where('settings_name', 'preachercount');
$count = $this->db->get()->row();
echo $count->settings_value;
$preacher = array();
$this->db->select('preacher.preacher_id,preacher.first_name,preacher.last_name,preacher.preacher_image,preacher.preacher_logo,preacher.preacher_bio_brief');
$this->db->distinct('preacher.preacher_id');
$this->db->from('preacher');
$this->db->join('sermons', 'sermons.preacher_id=preacher.preacher_id');
$this->db->order_by('preacher.sort_order');
$this->db->limit($count->settings_value);
$query = $this->db->get('', $limit, $offset);
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$preacher[$row->preacher_id]['preacher_id'] = $row->preacher_id;
$preacher[$row->preacher_id]['preacher_name'] = $row->first_name . ' ' . $row->last_name;
$preacher[$row->preacher_id]['preacher_image'] = $row->preacher_image;
$preacher[$row->preacher_id]['preacher_logo'] = $row->preacher_logo;
$preacher[$row->preacher_id]['preacher_bio_brief'] = $row->preacher_bio_brief;
$this->db->select('*');
$this->db->from('sermons');
$this->db->where('preacher_id', $row->preacher_id);
$this->db->order_by('sort_order ');
$sermon_array = array();
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row1) {
$sermon_array[$row1->sermon_id] ['sermon_image'] = $row1->sermon_image;
$sermon_array[$row1->sermon_id] ['sermon_title'] = $row1->sermon_title;
$sermon_array[$row1->sermon_id] ['audio_file'] = $row1->audio_file;
$sermon_array[$row1->sermon_id] ['sermon_description'] = $row1->sermon_description;
}
}
$preacher[$row->preacher_id]['sermon'] = $sermon_array;
}
return $preacher;
}
return false;
}
Method in controller
function list() {
$paginate_segment = $this->uri->segment(3) ? $this->uri->segment(3) : 0;
$winner_list = $this->sermon_model->viewAllpreachers(0, null);
$config['base_url'] = base_url() . 'sermons/index/';
$config['total_rows'] = ($winner_list) ? count($winner_list) : 0;
$config['per_page'] = 2;
$config['full_tag_open'] = '';
$config['full_tag_close'] = '';
$config['uri_segment'] = 3;
$config['num_links'] = 4;
$config['display_pages'] = TRUE;
$config['first_link'] = 'First';
$config['first_link'] = 'First';
$config['first_tag_open'] = '<div>';
$config['first_tag_close'] = '</div>';
$config['last_link'] = 'Last';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev';
$config['cur_tag_close'] = ' | ';
$config['num_tag_close'] = ' | ';
//initialize pagination
$this->pagination->initialize($config);
$this->data['preachers'] = $this->sermon_model->viewAllpreachers($paginate_segment, $config['per_page']);
$this->data['links'] = $this->pagination->create_links();
$this->data['page'] = $this->config->item('APP_template_dir') . 'site/home/sermons_view';
$this->load->vars($this->data);
$this->load->view($this->_container);
}
As stated in the comments, you're using a limit twice and need to combine those methods:
function viewAll($offset=0, $limit=null) {
$this->db->select('settings_value');
$this->db->from('site_settings');
$this->db->where('settings_name', 'preachercount');
$count = $this->db->get()->row();
// now use value of [$count] if [$limit] is null - else use [$limit]
$limit = (is_null($limit)) ? $count : $limit ;
$preacher = array();
$this->db->select('...');
$this->db->distinct('preacher.preacher_id');
$this->db->from('preacher');
$this->db->join('sermons', 'sermons.preacher_id=preacher.preacher_id');
$this->db->order_by('preacher.sort_order');
// then remove the [limit()] call
// $this->db->limit($count->settings_value); <-- NOT NEEDED
$query = $this->db->get('', $limit, $offset);
This effectively allows you to pass a $limit to the method, or use the settings from the db.

Resources