Codeigniter pagination does not work for me. It displays page numbers and first 5 search results, but when I click on the page '2' it loads 'search_nok' view with message "Please select your options". Could you please check my code below and help me to find the mistake.
Here is my controller:
public function search($offset = 0) {
$limit = 5;
$this->load->library('form_validation');
$this->load->model('model_x');
$this->form_validation->set_rules('country', 'Country','required');
if($this->form_validation->run()) {
$country = $this->input->post('country');
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/abc/cont/search/'; //where 'http://localhost/abc' is my base url
$config['total_rows'] = 14;
$config['per_page'] = 5;
$data['pagination'] = $this->pagination->initialize($config);
if ($this->model_x->did_search($country, $limit, $offset)){
$data["results"] = $this->model_x->did_search($country, $limit, $offset);
$this->load->view("search_ok",$data);
}
}
else
{
$data['message'] = 'Please select your options.';
$this->load->view("search_nok",$data);
}
}
Here is my view:
<?php
echo $this->pagination->create_links();
foreach($results as $row){
$country = $row['country'];
$city = $row['city'];
?>
<table>
<tr>
<td >
<b><?php echo $country; ?></b>
</td>
<td>
<b><?php echo $city; ?></b>
</td>
</tr>
</table>
<?php }?>
I think you are confused about Pagination. First of all, technically all of the pagination stuff:
<?php
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/abc/cont/search/'; //where 'http://localhost/abc' is my base url
$config['total_rows'] = 14;
$config['per_page'] = 5;
should be in your controller, secondly, you should actually be limiting the results in your model as well, since you haven't shown your model I will come up with an example
$this->db->limit ($limit $offset //these will be defined in your controller)
$data = $this->db->get('whatever_table')->whatever_result_type.
return $data
you should define $limit in your controller and pass it over your your model function as a parameter that the function accepts
the controller should also have $offset and $limit defined
public function search($offset = 0) {
$limit = 5;
when you call your model make sure to pass these over
$this->model_x->did_search($country, $limit, $offset);
then instead of just $this->pagination->initialize($config);
do this(still in your controller remember):
$data['pagination'] = $this->pagination->initialize($config);
then echo pagination wherever you want it in your view
Related
If I do not include pagination i.a. comment out $this->db->limit and remove $limit & $start from the get_category_posts() method, the values are displayed as they're meant to in a table. However, as soon as adding the limit the links are displayed but not the 'posts'. I feel there is something wrong with the pagination setup or limit business, or maybe even the position of where I have $this->db->limit (currently undernearth $this->db->from('posts'). I've been up all night trying to get this working and have have no luck on the internet, most people seem to have problems with dislaying links not actually the data itself. If somebody could point me in the direction that would be incredibly appreciated. Thank you.
MODEL:
public function get_category_posts($slug = FALSE, $limit, $start){
if($slug === FALSE){
$this->db->select('post_id, user_id_fk, post_title, post_content, post_date, slug, username');
$this->db->join('category_link_table', 'posts.post_id = category_link_table.post_id_fk');
$this->db->join('category', 'category_link_table.cat_id_fk = category.cat_id');
$this->db->join('users', 'posts.user_id_fk = users.user_id');
$this->db->from('posts');
$this->db->limit($limit, $start);
$this->db->where('category.parent_id', 0);
$query = $this->db->get();
foreach($query->result() as $row){
$data [] = $row;
}
return $data;
//return $query->result_array();
}
}
CONTROLLER:
public function catpage(){
$config['base_url'] = base_url() . "post/catpage";
$config['total_rows'] = $this->post_model->record_count();
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['posts'] = $this->post_model->get_category_posts(null, $config['per_page'], $page);
$data['links'] = $this->pagination->create_links();
$this->load->view('templates/header', $data);
$this->load->view('posts/catpage', $data);
$this->load->view('templates/footer');
}
VIEW:
<?php if(isset($posts) & ($posts <> NULL)){
foreach($posts as $post_item): ?>
<tr>
<td><p><?php echo $post_item->post_title; ?></p></td>
<td><p><?php echo $post_item->username; ?></p></td>
<td><p><?php echo $post_item->post_date; ?></p></td>
<td>Rating data</td>
<td>View data</td>
</tr>
<?php
endforeach;
}?>
Also I have a page that deals with individual slugs, so therefore I pass NULL into the methods as it of course requests 3 parameters.
Thank you.
I'm learning CI and latest I've been tasked to do is pagination. So I followed this tutorial as it seemed relatively nicely made and explained. Link
The pagination works and its great. Now I wanted to make a read more under each post, making each post open separately with its full description. That also works, but when I click a link to go back to the pagination index, the list starts from the beginning, no matter which post I clicked. I'm not sure how to add the page I want the return link to take me back so I'll just post this here and hopefully it won't be too hard for someone to tell me.
If someone is confused what I'm after, just look at the last view readmore_paginate. In it, the last link should contain a number back
to the page, but idk how to put or what to put there.
Controller
public function paginate()
{
$config = array();
$config['base_url'] = base_url()."welcome/paginate";
$config['total_rows'] = $this->blog_model->countPosts();
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; //TERNARY OPERATOR (? = TRUE) (: = FALSE)
$data['results'] = $this->blog_model->fetchPosts($config['per_page'], $page);
$data['links'] = $this->pagination->create_links();
$this->load->view('header');
$this->load->view("p_content", $data);
$this->load->view('footer');
}
public function readMore_Paginate()
{
$id = $this->input->get('postid');
$data['post'] = $this->blog_model->getSpecificPost($id);
$this->load->view('header');
$this->load->view('readmore_paginate', $data);
$this->load->view('footer');
}
Model
public function countPosts()
{
return $this->db->count_all("Blogposts");
}
public function fetchPosts($limit, $start)
{
$this->db->select('*');
$this->db->from('Blogposts');
$this->db->join('Blogcategories', 'Blogposts.postcatid=Blogcategories.id');
$this->db->limit($limit, $start);
$query = $this->db->get();
if($query->num_rows() > 0)
{
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
View p_content
<div class="col-md-8">
<table class="table">
<tbody>
<?php
foreach ($results as $key) {
echo "<tr><th><h2><kbd><font color='yellow'>".$key->postname."</font></kbd></h2><kbd><font color='lime'>".date("d M Y",strtotime($key->postdate))."</font></kbd> <kbd><font color='cyan'>".$key->catname."</font></kbd></th></tr>";
echo "<tr><td><blockquote>".mb_substr($key->postdesc, 0,80,'UTF-8')."...";?>
read more</blockquote>
<?php } ?>
</tbody>
</table>
<p><?php echo $links; ?></p>
</div>
View readmore_paginate
<div class="col-md-8">
<table class="table">
<tbody>
<?php
foreach ($post as $key) {
echo "<tr><th><h2><kbd><font color='yellow'>".$key['postname'].
"</font></kbd></h2><kbd><font color='lime'>".
date("d M Y",strtotime($key['postdate']))."
</font></kbd> <kbd><font color='cyan'>".
$key['catname']."</font></kbd></th></tr>";
echo "<tr><td><blockquote>".$key['postdesc']."</blockquote>"; ?>
Back to Posts
<?php }
?>
</tbody>
</table>
</div>
Controller
$page = $this->input->get('page') ?
$this->input->get('page') :
$this->uri->segment(3) ?
$this->uri->segment(3) :
0;
$data['results'] = $this->blog_model->fetchPosts($config['per_page'], $page);
$data['links'] = $this->pagination->create_links();
$data['page'] = $page; // add this
view p_content
read more</blockquote>
view readmore_paginate
Back to Posts
I have a problem with pagination and codeigniter. I have a quick_searh view from witch I am submitting the information to a index controller function and there setting the pagination and calling the quick_search method to get the data I want. It just doesnt work . I've spent more then 5 hours rewriting those methods and even starting with quick_search and then passing to index function but nothing worked, please help.
public function index(){
// search parameters config
$lawyer_name = $this->input->post('lawyer_name');
$kanzlei = $this->input->post('kanzlei');
$area_of_expertise = $this->input->post('area_of_expertise');
$post_code = $this->input->post('post_code');
$city = $this->input->post('city');
$result = $this->quick_search(
$this->uri->segment(3),
$lawyer_name,
$kanzlei,
$area_of_expertise,
$post_code,
$city);
if(isset($result)){
// pagination config
$this->load->library('pagination');
$this->load->library('table');
$config['total_rows'] = count($result);
$config['base_url'] = 'http://localhost/anwalt/index.php/search/index';
$config['per_page'] = 5;
$config['num_links'] = 5;
$this->pagination->initialize($config);
$data['search_result_array'] = $result;
$data['main_content'] = 'pages/quick_search_results';
$this->load->view('templates/home_body_content', $data);
}
}
the quick_search function:
public function quick_search($offset, $lawyer_name, $kanzlei, $area_of_expertise, $post_code, $city){
// no input in the quick search
if( empty($lawyer_name) && empty($kanzlei) && empty($area_of_expertise)
&& empty($post_code) && empty($city))
{
$result = 'nothing';
} else {
$this->load->model('quick_search_model');
$result = $this->quick_search_model->get_search_results(
$offset,
$lawyer_name,
$kanzlei,
$area_of_expertise,
$post_code,
$city
);
}
return $result;
}
the sql is like this:
$sql = "SELECT users.user_id, users.canonical_name, first_name, last_name, city, phone_number, kanzlei
from users
inner join user_normal_aos
on users.user_id = user_normal_aos.user_id
inner join normal_areas_of_expertise
on user_normal_aos.normal_areas_of_expertise_id = normal_areas_of_expertise.normal_areas_of_expertise_id
where ".implode(" AND ", $where);
if(empty($offset)){
$offset = 0;
}
$sql = $sql." LIMIT ".$offset.", 4";
The data are displayed but I dont see the pagination in there .. and even when I want to change the url for segmenting it says I dont have any data.
The view is like:
<h1>Quick search results</h1>
<?php
if($search_result_array == "nothing"){
echo "<h3>You havent inputed anything</h3>";
} else {
echo $this->table->generate($search_result_array);
}
echo $this->pagination->create_links();
As per your search variables you can use this:
$lawyer_name = $this->input->post('lawyer_name');
$kanzlei = $this->input->post('kanzlei');
$area_of_expertise = $this->input->post('area_of_expertise');
$post_code = $this->input->post('post_code');
$city = $this->input->post('city');
/*pagination start*/
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/index/lawyer/'.$lawyer_name.'/kanzlei/'.$kanzlei.'/area_of_expertise/'.$area_of_expertise.'/post_code/'.$city.'/page/';
$config['total_rows'] = $this->model->count_all_results(); ###implement this function to count all the vodeos as per the search variables, just use the same function as "quick_search" but without the limit clause
$config['per_page'] = count($result);;
$config['uri_segment'] = 10;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev';
$config['cur_tag_open'] = '<span class="active_page">';
$config['cur_tag_close'] = '</span>';
$this->pagination->initialize($config);
/*pagination end*/
You can not use $this->pagination->create_links(); method in view.
Use $data['pagination'] = $this->pagination->create_links(); in controller just before loading view
and echo $pagination in view
hope this will help you.
Hey, guys. I have a problem with pagination in my Codeigniter project. I am using the pagination library, and I have a search form. When I search for something, the results display on the page. When the resulting rows are more than the limit, I show the pagination links. But when I click on link number 2 to go to second page, the pagination links disappear. My search code is given below...
Controller function:
function search_branch()
{
$this->load->library('pagination');
$search_this=$this->input->post('inputsearchbranch');
$limit = 20;
$data['fields'] = array(
'branch_name' => 'Branch Name',
'city'=>'City',
'district'=>'District',
'tahsil'=>'Tahsil',
'Branch_manager'=>'Branch Manager'
);
$this->load->model('mod_user');
$searchbranch=$this->mod_user->search_branch($search_this,$limit);
$data['searchbranch'] = $searchbranch;
$config = array();
$config['base_url'] = site_url("ctl_home/search_branch/");
$config['total_rows'] = count($searchbranch);
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('view_searchbranch',$data);
}
My view:
<center>
<form class="userinfo" action="" method="post">
//table in which search record display
</form>
</center>
<?php if (strlen($pagination)): ?>
<div class="pagination">
<?php echo $pagination; ?>
</div>
<?php endif; ?>
Try to changing the "$config['uri_segment'] = 3;" to $config['uri_segment'] = 5;
And you are count the total count with the limited record. The total count value will be counted with all records.
function search_branch()
{
$this->load->library('pagination');
$search_this=$this->input->post('inputsearchbranch');
$limit = 20;
$data['fields'] = array(
'branch_name' => 'Branch Name',
'city'=>'City',
'district'=>'District',
'tahsil'=>'Tahsil',
'Branch_manager'=>'Branch Manager'
);
$this->load->model('mod_user');
$searchbranch=$this->mod_user->search_branch($search_this,false);
$config['total_rows'] = count($searchbranch);
$searchbranch=$this->mod_user->search_branch($search_this,$limit);
$data['searchbranch'] = $searchbranch;
$config = array();
$config['base_url'] = site_url("ctl_home/search_branch/");
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('view_searchbranch',$data);
}
& also use
$offset = $this->uri->segment(4);
Add new parameter to search_branch function and pass that to db..
$searchbranch=$this->mod_user->search_branch($search_this,$limit,$offset);
example Function($where,$limit,$offset)
$this->db->where($where);
$query = $this->db->get('mytable', $limit, $offset);
Can anyone explain why I cannot get a variable called $siteID to pass to another function within the same controller?
In the third function called "get_orders_by_site" I have loaded a different model, which returns information about 'orders' raised at the currently viewed building/site/property.
The sites controller works perfectly, first function lists a table with all my properties, then when one is clicked - the second function gets the siteID of that selection, and returns with further 'detail/data' - sites controller function1/2 all relate to the same model, and return information from the SAME table.
I'm trying to implement a third function, which will do a similar task, but return with information/data from a different table (the site.siteID, is also a FK in the orders.siteID table i've created in phpmyadmin).
If I need to explain further please let me know - Many thanks!
amended code
Sites Controller
<?php
class Sites extends CI_Controller {
//Searches for a list of sites
public function search($sort_by = 'site_title', $sort_order = 'asc', $offset = 0)
{
$limit = 20;
$data['columns'] = array(
'site_title' => 'Site Name',
'site_uprn' => 'Unique Property Reference'
);
$this->load->model('site_model');
$results = $this->site_model->get_sites($limit, $offset, $sort_by, $sort_order);
$data['sites'] = $results['rows'];
$data['num_results'] = $results['num_rows'];
//pagination for list returned
$this->load->library('pagination');
$config = array ();
$config['base_url'] = site_url("Sites/search/$sort_by/$sort_order");
$config['total_rows'] = $data['num_results'];
$config['per_page'] = $limit;
$config['uri_segment'] = 5;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['sort_by'] = $sort_by;
$data['sort_order'] = $sort_order;
$this->load->view('search', $data);
}
//Displays individual site details
//passes selected siteID to the model, and returns only database/info for that particular building/property
public function details($siteID){
$this->load->model('site_model');
$data['site']=$this->site_model->get_site($siteID);
$this->load->view('site', $data);
$this->load->view('orders', $data);
}
// this second function should do a similar method as above, however I've loaded a different model, as i'm getting information from a different database table - but I still want the data returned to be limited by the building/site ID which the user selects.
public function orders_by_site($siteID, $sort_by = 'orderID', $sort_order = 'asc', $offset = 0)
{
$this->load->model('site_model');
$this->load->model('order_model');
$limit = 20;
$data['columns'] = array(
'orderID' => 'Order No.',
'initiated_date' => 'Initiated Date',
'target_date' => 'Target Date',
'status' => 'Status',
'priority' => 'Priority',
'trade_type' => 'Trade Type'
);
$results = $this->site_model->get_site($siteID);
$results = $this->order_model->get_orders($siteID, $limit, $offset, $sort_by, $sort_order);
$data['orders'] = $results['rows'];
$data['num_results'] = $results['num_rows'];
//pagination for orders table
$this->load->library('pagination');
$config = array ();
$config['base_url'] = site_url("Orders/orders_by_site/$sort_by/$sort_order");
$config['total_rows'] = $data['num_results'];
$config['per_page'] = $limit;
$config['uri_segment'] = 6;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['sort_by'] = $sort_by;
$data['sort_order'] = $sort_order;
$this->load->view('orders', $data);
}
}
End Sites Controller
Site Model
//Get all site data
function get_sites($limit, $offset, $sort_by, $sort_order){
$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
$sort_columns = array('site_title', 'site_uprn');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'site_title';
$q = $this->db->select('siteID, site_title, site_uprn')
->from('sites')
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
$ret['rows'] = $q->get()->result();
//count query for sites
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('sites');
$tmp = $q->get()->result();
$ret['num_rows'] = $tmp[0]->count;
return $ret;
}
//Get individual site data
function get_site($siteID){
$this->db->select()->from('sites')->where(array('siteID' => $siteID));
$query = $this->db->get();
return $query->first_row('array');
}
}
Orders Model
//order table
function get_orders($siteID, $orderID, $limit, $offset, $sort_by, $sort_order){
$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
$sort_columns = array('orderID', 'initiated_date', 'target_date','completion_date','status','priority','total_amount','job_description','requestor_name','requestor_telno','trade_type');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'orderID';
$q = $this->db->select()->from('orders')->where(array('siteID' => $siteID))->limit($limit, $offset)->order_by($sort_by, $sort_order);
$ret['rows'] = $q->get()->result();
}
//order details
function get_order($orderID){
$this->db->select()->from('orders')->where(array('orderID' => $orderID));
$query = $this->db->get();
return $query->first_row('array');
}
}
Site View - only showing the extract where I'm trying to embed the orders view
<h5>Order Details</h5>
<?php include('orders.php')?>
</div>
</div>
</div>
Orders View
<div id="site_filter">
<div class="result_counter">
<h5>Found <?php echo $num_results; ?> Orders</h5>
</div>
<div class="pagination">
<?php if(strlen($pagination)): ?>
Page: <?php echo $pagination; ?>
<?php endif; ?>
</div>
</div>
<div class="clear_float"></div>
<table class="table">
<thead>
<?php foreach($columns as $column_name => $column_display): ?>
<th <?php if ($sort_by == $column_name) echo "class=\"sort_$sort_order\"" ?>>
<?php echo anchor("Sites/orders_by_site/$column_name/" .
(($sort_order == 'asc' && $sort_by == $column_name) ? 'desc' : 'asc') ,
$column_display); ?>
</th>
<?php endforeach; ?>
</thead>
<tbody>
<?php foreach($orders as $order): ?>
<tr>
<td><?php echo $order->orderID; ?></td>
<td><?php echo $order->initiated_date; ?></td>
<td><?php echo $order->target_date; ?></td>
<td><?php echo $order->status; ?></td>
<td><?php echo $order->priority; ?></td>
<td><?php echo $order->trade_type; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
OK, a couple of things here - and I'm gonna need some help
In the orders_by_site method, you're overwriting the results variable:
$results = $this->site_model->get_site($siteID);
$results = $this->order_model->get_orders($siteID, $limit, $offset, $sort_by, $sort_order);
Also, the way you're loading views is incorrect. You're loading your views like this:
$this->load->view('site', $data);
$this->load->view('orders', $data);
And what you need to be doing is this:
// you need to get the "contents" of the `orders` view in a variable
// and pass that to the `site` view
$data['orders'] = $this->load->view('orders', $data, TRUE);
$this->load->view('site', $data);
And change your site view to this:
<h5>Order Details</h5>
<?php echo $orders; ?>
</div>
</div>
</div>
I'm sure there's more going on than that, but that's all I can gather from what I've seen in your question and comments.
#swatkins -
Thank you very much for your help, you highlighted some issues I had overlooked - I've gone about this in a different fashion now.
Originally I was trying to use Active Record (i believe) to select data from one table, based on the selection of a record from a different table - and then pass this selection to another model and use it in a get_where statement.
I've managed to get this to work using the $this->uri->segment() method in my controller, and then passing this to the corresponding model.
Now I'm able to utilise the user selection of a 'building/propery' name, with it's address etc - and then I have a second model, which retrieves the 'orders/jobs' that have been raised at that building.