CodeIgniter Pagination - codeigniter

I after some help with codeIgniter pagination (fixign a bug).. I can see the number of records and the pagination link in the view but when click on the next link, it does not show the next/previous 10 records. Can someone please help me with this?
I have the following code in my controller
function customerlist_pagination()
{
$search = $this->input->post('search');
$data['title'] = "Customer Clist";
$data['heading'] = "List of customers";
$data['result'] = $this->customers_model->getAllcustomers_pagination($search_para, FALSE, "limit_rows");
$data['num_recs'] = $this->customers_model->getAllcustomers_pagination($search_para, FALSE, "num");
$config['base_url'] = base_url().'index.php/customer/customerlist_pagination/';
$config['total_rows'] = $data['num_recs'];
$config['per_page'] = 10;
$config['uri_segment'] = 3;
$config['next_link'] = 'Next >';
$config['prev_link'] = '< Previous ';
$this->pagination->initialize($config);
$this->load->view('customer_view_table',$data);
}
In the model, I have the following code:
function getAllSeizures_pagination($search_para, $archive_search = FALSE, $result_type)
{
$search_para = $search_para."%";
$selected_fields = "customer.firstName
customer.lastName, customer.address,
customer.city, customer.postcode";
$from = "customer";
$where = "customer.deleted=0 ";
if ($archive_search == TRUE) {
$where .= "AND customer.archived= 1 ";
}else{
$where .= "AND customer.archived= 0 ";
}
$where .= "AND (customer.firstName LIKE '$search_para' OR customer.postcode LIKE '$search_para' OR customer.city LIKE '$search_para')";
$query = $this->db->select($selected_fields, false)
->from($from)
->where($where)
->order_by('customer.idcustomer', 'desc');
if($result_type == "limit_rows")
{
$query = $this->db->limit(10, 0)
->get();
$query = $query->result();
}
if($result_type == "num") {
$query = $this->db->get();
$query = $query->num_rows();
}
return $query;
}
Thanks alot
Regards
Prats

The pagination class passes the offset to you in the uri_segment specified. Since you set it to 3, you should capture that info in the first variable of your controller method, like so:
function customerlist_pagination( $offset )
{
}
Then, when you call your $this->customers_model->getAllcustomers_pagination(), you have to pass it the $offset, so that later on in your code, where you limit your results:
$query = $this->db->limit(10, 0)->get();
you should instead use this $offset to limit your results:
$query = $this->db->limit(10, $offset)->get();

Related

$this->pagination->create_links(); returning nothing

Link for pagination not working
This is my controller.
Here Is my code. What's wrong in this.
Here links are not Initialize. Tell me the correct solutions
The create_links(); the function seems to not be working. I don't get any errors, but it just returns a blank string. I'm aware the documentation says http://codeigniter.com/user_guide/libraries/pagination.html The create_links() function returns an empty string when there is no pagination to show. but so how do I fix that? Thanks you!
public function Index()
{
$this->load->model('Testmodel');
$conf = array();
$conf['base_url'] = base_url()."index.php/user/view";
$conf['total_rows'] = $this->db->get('user')->num_rows();
$conf['per_page'] = 5;
$conf['use_page_numbers'] = TRUE;
$conf['num_links'] = 2;
$conf['cur_tag_open'] = ' <a class="current">';
$conf['cur_tag_close'] = '</a>';
$conf['next_link'] = 'Next';
$conf['prev_link'] = 'Previous';
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 0;
}
$data['records'] = $this->Testmodel->fetch_data($conf['per_page'], $page);
$this->pagination->initialize($conf);
$data['links'] = $this->pagination->create_links();
}
Model Code:
public function fetch_data($limit, $start) {
$this->db->select('user.*,country.countryName,state.stateName,city.cityName');
$this->db->from('user');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;

Pagination with search in Codeigniter is not working

I have a problem in pagination with search in codeigniter
URL is not change when select the other page link
the data is not change when select the other link
This my controller
public function search_code(){
$this->load->library('pagination');
$param = new stdClass();
$param->item_code = $this->input->get('by_item_code');
$param->description = $this->input->get('by_description');
if (!isset($page) || $page == '') {
$page = 1;
}
$param->per_page = 50;
$param->limit = ($page - 1) * $param->per_page;
$paginate_url = base_url('warehouse/search_code?item_code='.$param->item_code.'&by_description='.$param->description.'&page='.($page+1));
$data['total_result'] = $this->m_stock->count_stock_search ($param);
$config['uri_segment'] = 3;
$config['num_links'] = 4;
$config['base_url'] = $paginate_url;
$config['total_rows'] = $data['total_result'];
$config['per_page'] = $param->per_page;
$config['use_page_numbers'] = TRUE;
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['item'] = $this->m_stock->search_code ($param);
$this->load->view('v_all_stocks', $data);
}
This my model
function search_code($param){
$result = array();
$sql = "select ic.id, ic.item_code, ic.description, maxdt.maxdt, lc.balance,lc.dt,lc.createdate,lc.id_lines_code, io.ONHANDQTY ";
$sql .= "from tbl_item_code ic ";
$sql .= "left join ( tbl_lines_code lc inner join ( select id, max(createdate) maxdt from tbl_lines_code where active = 1 group by id ) maxdt ";
$sql .= "on lc.id = maxdt.id and lc.createdate = maxdt.maxdt ) on ic.id = lc.id ";
$sql .= "left join item_ostendo io on io.ITEMCODE = ic.item_code ";
$sql .= "where ic.active = 1 ";
if ($param->item_code != '') {
$sql .= "AND ic.item_code LIKE '%$param->item_code%' ";
}
if ($param->description != '') {
$sql .= "AND ic.description LIKE '%$param->description%' ";
}
$sql .= "group by ic.id order by ic.item_code ";
if ($param->limit > 0)
$sql .= " LIMIT ".$param->limit.", ".$param->per_page;
else
$sql .= " LIMIT ".$param->per_page;
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$result = $query->result();
}
return $result;
}
and about view i checked it's can working
but i don't know why i select the link of number the data is show like the last page but the number of link is working
Show all data not search yet the link show like this
when search show like this
but when select number 2 of link it's show like this and the data is same every page
You need to change your:
$paginate_url = base_url('warehouse/search_code?item_code='.$param->item_code.'&by_description='.$param->description.'&page='.($page+1));
This:
$config['uri_segment'] = 3;
defines that segment for the offset clause.
I would suggest:
$query = "?item_code='.$param->item_code.'&by_description='.$param->description.'&page='.($page+1))";
$paginate_url = base_url('warehouse/search_code/page/').$query;
$config['uri_segment'] = 4;
Take this as offset:
$param->limit = $this->uri->segment(4);
You have to change paging base url as below :
$paginate_url = base_url('warehouse/search_code').'?item_code='.$param->item_code.'&by_description='.$param->description;

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');

code igniter pagination for displaying specific data from database?

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();
}

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