combine get_where and limit in active record codeigniter - php

I am little confused here.
I have a modal like this :
public function selectRequestPerUser($nama_user, $start_row, $limit) {
$query = $this->db->get_where('tbl_requestfix', array('nama_user' => $nama_user), $start_row, $limit);
return $query->result_array();
}
So, I use this modal to create a pagination in CI like this :
$nama = $this->session->userdata('nama');
$start_row = $this->uri->segment(2);
$per_page = 3;
if(trim($start_row) == ''){
$start_row = 0;
};
$this->load->library('pagination');
$config['base_url'] = base_url().'control_closing/';
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$request = $this->model_request->selectRequestPerUser($nama, $start_row, $per_page);
$data['data_request'] = $request;
$this->load->view('view_closing', $data);
in view, just :
<?php echo pagination ?>
It just give me a blank page. I think in my modal get_where is the problem. Anybody can help ?

The syntax of get_where() is
$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);
You need to change this line in your model (alter $start_row and $limit) to make it work,
$query = $this->db->get_where('tbl_requestfix', array('nama_user' => $nama_user), $limit, $start_row);

Related

Pagination Shows same results

Using codeigniter 3 I have setup pagination. When I click on the next link I get the same results minus the first post, I cannot seem to find what is causing this.
I tried removing the offset and not allowing numbers, and I still have no change.
controller
public function index($offset = 0){
//Pagination
$config['base_url'] = base_url().'posts/index/';
$config['total_rows'] = $this->db->count_all('posts');
$config['uri_segment'] = 3;
$config['use_page_numbers'] = TRUE;
$config['per_page'] = 5;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$config['display_pages'] = FALSE;
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$data['title'] = 'Newest';
$data['posts'] = $this->post_model->get_posts(FALSE,$config['per_page'],$offset);
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
}
model
public function get_posts($slug = FALSE, $limit=FALSE,$offset = FALSE){
if($limit){
$this->db->limit($limit,$offset);
}
if($slug === FALSE){
$this->db->order_by('created_time','DESC');
$this->db->join('categories','categories.id = posts.category_id');
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts',array('slug'=>$slug));
return $query->row_array();
}
view
<ul class="pagination">
<?php echo $links; ?>
</ul>
///////////////////UPDATE/////////////
So this is how my code looks now after updating as your said.
public function index($offset=0){
//Pagination
$config['base_url'] = base_url().'posts/index/';
$config['total_rows'] = $this->db->count_all('posts');
$config['uri_segment'] = 3;
$config['num_links'] = 10;
$config['per_page']=3;
$limit = $config['per_page'];
$offset = ($offset) * ($config['per_page']);
this->pagination->initialize($config);
$data['posts'] = $this->post_model->get_posts(FALSE,$limit,$offset);
in the view I have it set to
<?php echo $this->pagination->create_links(); ?>
So now the issue is, it shows 3 rows(results) and when I click next it shows nothing and that's it, so now I am only getting 3 results only on the first page and none on the next pages. What is wrong?
Index Function Not Working Segment Use Query String
if $offset is current_page_number starting on 0, definition should be:
//number of rows to get
$limit = $config['per_page'];
//where start to get it
$offset = ($offset) * ($config['per_page']);
$data['posts'] = this->post_model->get_posts(FALSE,$limit,$offset);
If you define offset as current_page, starting on 1.
public function index($offset = 1){
......
//number of rows to get
$limit = $config['per_page'];
//where start to get it
$offset = ($offset - 1) * ($config['per_page']);
$data['posts'] = this->post_model->get_posts(FALSE,$limit,$offset);
Talking about "Index Function Not Working Segment Use Query String"...
If you have $config['enable_query_strings'] = TRUE you should GET it
public function index() {
$page = $this->input->get('page');
...
$offset = ($page - 1) * ($config['per_page']);
...

Codeigniter pagination fetch only the first page unable to navigate second and third page

i have an issue with codeigniter pagination, the same technique i did in the older codeigniter version it works fine but not in this latest version(3.1.9). The first front page fetch data fine but when i click on page 2, 3 or 4 still it display those of the first front row data.
Here is my controller:
$total_rows = $this->estate_model->estate_count();
$config = pagination_configuration(base_url("real_estate"), $total_rows, 10, 3, 5, true);
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(3) : 0;
$page_num = $page-1;
$page_num = ($page_num<0)?'0':$page_num;
$page = $page_num*$config["per_page"];
$data["links"] = $this->pagination->create_links();
$obj_result = $this->estate_model->get_all_active_estate($config["per_page"], $page);
Model:
public function get_all_active_estate($limit, $start)
{
$this->db->select('*');
$this->db->from('estate');
$this->db->where('status', 'active');
$this->db->limit($limit, $start);
$Q = $this->db->get();
if ($Q->num_rows() > 0) {
$return = $Q->result();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
Please help.. thank you
try
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
instead
$page = ($this->uri->segment(2)) ? $this->uri->segment(3) : 0;

Error in generating page number links using Codeigniter

I am creating a pagination using Codeigniter and my problem is I have an error in displaying the page number links in my URL.
example I go to page 2 my URL would be like this:
http://localhost/my_project/inbound/listing/1
if page 3
http://localhost/my_project/inbound/listing/2
Here's my controller
$config = array();
$config['base_url'] = base_url('inbound/listing');
$config['total_rows'] = $this->mod->countList();
$config['per_page'] = 1;
$config['uri_segment'] = 3;
//fd($config);
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
fp($page, 'pink'); //print out result
$order = ($this->input->get('order'))? $this->input->get('order'): '';
$sort = ($this->input->get('sort'))? $this->input->get('sort'): '';
// Query data
$data['data_list'] = $this->mod->listing($config['per_page'], $page); //where, limit, page, field, sort
fP($data['data_list']); //print out result
$data['pagination'] = $this->pagination->create_links();
Then my model for generating links:
function listing($limit, $start)
{
//DATE_FORMAT(`post_date_added`, "%m/%d/%Y %H:%i") as `proper_post_date_added`,
$this->db->select('*');
$this->db->from('inventory');
$this->db->limit($limit, $start);
$query = $this->db->get();
//$rows = $query->result_array();
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Can you help me with this?
If you want to go to page number based on your url, modify your listing function to be like this:
function listing($limit, $start)
{
$offset_1 = $start - $limit;
$offset_2 = $offset_1 < 0 ? 0 : $offset_1;
//DATE_FORMAT(`post_date_added`, "%m/%d/%Y %H:%i") as `proper_post_date_added`,
$this->db->select('*');
$this->db->from('inventory');
$this->db->limit($limit, $offset_2);
$query = $this->db->get();
//$rows = $query->result_array();
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}

How to write Segment in pagination library code igniter

Mysql Segment ?? not understood properly
in pagination...
Here is my code:
$data['query']= $this->db->select('auto_id,title,image,thumb')
->get('gallery',$config['per_page']=12,$this->uri->segment(3));
where per_page=12
try to use like this->
function records($sort_by = 'rpd_id', $sort_order = 'asc', $offset = 0) {
$limit = 10;
$data['results'] = $this->rcbs_Model->get_all_data_from_request($limit, $offset, $sort_by, $sort_order);
$config = array();
$config['base_url'] = base_url("rcbs/records/$sort_by/$sort_order");
$config['total_rows'] = $this->rcbs_Model->count_records();
$config['per_page'] = $limit;
$config['uri_segment'] = 5;
$data['sort_by'] = $sort_by;
$data['sort_order'] = $sort_order;
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$this->load->view('booking_data', $data);
}
here $sort_by = 'rpd_id' (rpd_id)is the default table column by which
data will be sorted.
"get_all_data_from_request" is the function by which you will get all
data from your table using select query.
base_url("rcbs/records/$sort_by/$sort_order"); rcbs -Controller name
'records'->function name itselft
$this->rcbs_Model->count_records() count all the records from the
table.

CodeIgniter $this->pagination->create_links(); returning blank string

public function viewconsultant($id1='')
{
$id1 = $id1 ? $id1 : 1;
$pagenum=5;
$start=($id1-1)*$pagenum;
$users = $this->db->get_where('connections', array('user_id'=>97));
$pageall = count($users);
$config['total_rows']=$pageall;
$config['per_page']=$pagenum;
$config['num_links']=4;
$config['base_url']="localhost/~chrisfu/linkedin2/index.php/connections/viewconsultant";
$config['use_page_numbers']=true;
$this->load->library('pagination');
$this->pagination->initialize($config);
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $start);
$data['connections'] = $query->result();
$this->load->view('profile_view',$data);
}
The create_links() does not work well, it just displays a blank string. I tried this localhost/~chrisfu/linkedin2/index.php/connections/viewconsultant/2, it can jump to second page, but not pagination links created blow the table. please help!!
The problem is that you are not creating the links..
$this->load->library('pagination');
$this->pagination->initialize($config);
// add the line below to your code
$data['links'] = $this->pagination->create_links();
// debug
// echo $data['links'];
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $start);
$data['connections'] = $query->result();
$this->load->view('profile_view',$data);
Also, you can omit the first line in your method by doing the following:
public function viewconsultant($id1=1)
{
// line below no longer necessary
//$id1 = $id1 ? $id1 : 1;
}
Try this:
function viewconsultant($id1=1, $offset = 0){
$this->load->library('pagination');
$pagenum=5;
//$users = $this->db->where('connections', array('user_id'=>97))->limit('20', $offset);
$total = $this->db->where('connections', array('user_id'=>97));
$pageall = $total->num_rows();
//$users = $this->db->get_where('connections', array('user_id'=>97));
//$start=($id1-1)*$pagenum;
//$pageall = count($users);
$config['total_rows'] = $pageall;
$config['per_page'] = $pagenum;
$config["uri_segment"] = 3;
$config['num_links'] = 4;
$config['base_url'] = base_url()."/index.php/connections/viewconsultant/";
$config['use_page_numbers'] = true;
$this->load->library('pagination');
$this->pagination->initialize($config);
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $this->uri->segment(3));
$data['connections'] = $query->result();
$data['links'] = $this->pagination->create_links();
$this->load->view('profile_view',$data);
}
Now, just echo $links in your view file to get the pagination.

Categories