So here is what I had before. If I'd go to ciblog/categories/posts/5/, it would take me to a page showing all posts with a category with the id of 5.
And here is what I want to do. I want to go to ciblog/categories/posts/5 and it would show be 5 posts lets say. Then when I go to ciblog/categories/posts/5/3 it would offset by 3 so now I have the next 3 posts.
Currently I have 4 posts, just for testing. If I directly through my url go to ciblog/categories/posts/5 it shows 3 posts, and if I go to ciblog/categories/posts/5/3 it shows my one other post, so the amount of posts I am getting is correct based on the url.
BUT at the bottom it always shows that I am on page 2. When I use F12 and look at the elements both my pagination links show ciblog/categories/posts/5
So its not adding on the number at the end
Here is what I have for the pagination for this page. I used print_r and everything in $config is correct.
public function posts($id, $offset=0){
$category = $this->category_model->get_category($id);
if(empty($category))
{
show_404();
}
$config['base_url'] = base_url().'categories/posts/'.$id;
$config['total_rows'] = $this->post_model->get_posts_by_category_count($id);
$config['per_page'] = 3;
$config['uri_segment'] = 3;
$config['attributes'] = array('class' => 'pagination-link');
$this->pagination->initialize($config);
$data['title'] = $category->name;
$data['posts'] = $this->post_model->get_posts_by_category($id,$config['per_page'],$offset);
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
}
Routes:
$route['categories/posts/(:num)/(:num)'] = 'categories/posts/$1/$2';
Reference:
I have been following a tutorial here: https://www.youtube.com/watch?v=WoQTjJepDWM&index=8&list=PLillGF-RfqbaP_71rOyChhjeK1swokUIS
Code for this tutorial is here: https://github.com/bradtraversy/ciblog
I am now adding on to this. The only changes I have made are shown above
$config['base_url'] = base_url().'categories/posts/'. $id . '/' . $offset;
$config['uri_segment'] = 4;
Because your pagination depends on offset, not id of category, then you should let it find for the offset with uri_segment 4, and your base_url must send an offset variable to find the offset.
Related
$category_id=$_GET['category_id'];
$data['adsd']=$this->mymodel->select_ads_by_category($category_id);
$config["base_url"]=site_url('main/category_ads_display?category_id='.$category_id.'');
$config["total_rows"]=count($data['adsd']);
$limit=$config["per_page"]=1;
$config["uri_segment"]=3;
$config["use_page_numbers"]=TRUE;
$this->pagination->initialize($config);
if($this->uri->segment(3))
$page=($this->uri->segment(3)-1)*$limit;
else
$page=0;
Hello friends i have pasted my code above,,everything is working fine,but when i click on links of pagination the url is like this =localhost/blabla/main/category_ads_display?category_id=5/2(not working)
but i need the url to be as = localhost/blabla/main/category_ads_display/2?category_id=5(working)
please help me on this
Try this way
$category_id=$this->uri->segment(3);
$data['adsd']=$this->mymodel->select_ads_by_category($category_id);
config["base_url"]=site_url('main/category_ads_display/'.$category_id);
$config["total_rows"]=count($data['adsd']);
$limit=$config["per_page"]=1;
$config["uri_segment"]=4;
$config["use_page_numbers"]=TRUE;
$this->pagination->initialize($config);
if($this->uri->segment(4))
$page=($this->uri->segment(3)-1)*$limit;
else
$page=0;
Now your new link will be like this localhost/blabla/main/category_ads_display/5/2 for page 2 and 5 will be your category id
I am trying to give facility of pagination and search keyword in one report with Code-igniter Framework.
Below is the Controller code for that Purpose:
$queryString="?start_date=".$values['start_date']."&end_date=".$values['start_date']."&keyword=".$values['keyword']."";
if($this->uri->segment(3)){
$offset=$this->uri->segment(3);
$config['base_url'] = base_url()."reports/mysearch_search/".$offset.$queryString;
}else{
$config['base_url'] = base_url()."reports/mysearch_search/".$queryString;
}
$query = $this->db->query("SELECT * FROM (`dlrReport`) WHERE LEFT(`res_submit_date`,10) >= '".$values['start_date']."' AND LEFT(`res_submit_date`,10) <= '".$values['end_date']."' AND (number LIKE '%".$values['keyword']."%' OR source LIKE '%".$values['keyword']."%')");
$config['total_rows']=$query->num_rows();
$config['per_page'] = 2;
$this->pagination->initialize($config);
$where = array('LEFT(res_submit_date,10) >=' => $values['start_date'],'LEFT(res_submit_date,10) <=' =>$values['end_date']);
$this->db->where($where);
$this->db->where("(number LIKE '%".$values['keyword']."%' OR source LIKE '%".$values['keyword']."%')");
$res = $this->db->get('dlrReport', $config['per_page'], $this->uri->segment(3));
$dlr['details']= $res->result();
$dlr['start_date']=$values['start_date'];
$dlr['end_date']=$values['end_date'];
$dlr['keyword']=$values['keyword'];
$this->load->view('mysearch',$dlr);
Getting Below link in pagination links which is not working:
http://Host/project/reports/mysearch_search/?start_date=2014-01-23&end_date=2014-01-23&keyword=/2
Url i am expecting in pagination links is like:
http://Host/project/reports/mysearch_search/2?start_date=2014-01-23&end_date=2014-01-23&keyword=
How can i will Get right Url for searching Purpose?
Here is nice example from codeignter itself. Codeigniter Pagination check this.
By default codeigniter is expecting the pagination url like this.
http://localhost/project/reports/mysearch_search/2014-01-23/2014-01-23/2
$start_date =$this->uri->segment(5);
$end_date = $this->uri->segment(6);
$keyword = $this->uri->segment(7);
and if you need this kind of url then you have to change the
http://example.com/index.php?c=test&m=page&per_page=20
then you have to change to set this on config file
$config['enable_query_strings'] set to TRUE
Problem Description
I have a situation where I have parent / child tables that I have to display on the same view - in two different
sections. Each section needs its own set of pagination links. The contents (and therefore, pagination links) in section 2 must change depending on what is selected in the parent section / section 1.
So specifically, I have a categories table, and for each category, i have products within. I'd like to show a list of categories on the top of the page, and the bottom will display the products, if any, in the selected category.
First Attempt:
I've tried to solve this problem 2 different ways. The first time I wrote the code, I had one method in the controller which would look up all subcategories based on an id that the user selected. It looked something like this:
public function browsecategory($category_id)
{
//find all products in category.
$this->load->model('category/product_category_model');
//pagination for products
$this->load->library('pagination');
$config['base_url'] = site_url('/product/browsecategory/'.$category_id);
$config['uri_segment'] = 4;
$config['total_rows'] = count($productrecords);
$config['per_page'] = 10;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$offset = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
$productrecords = $this->product_model->limit($config['per_page'], $offset)->find_all();
//find any subcategories for this category
$this->load->model('category/category_model');
//pagination for subcategories
$this->load->library('pagination');
$config['base_url'] = site_url('/product/browsecategory/'.$category_id);
$config['uri_segment'] = 5;
$config['total_rows'] = count($this->category_model->find_all_by('parent_id', $category_id););
$config['per_page'] = 10;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$offset = ($this->uri->segment($config['uri_segment'])) ?
$this->uri->segment($config['uri_segment']) : 0;
$subcategoriesrecords = $this->category_model->limit($config['per_page'], $offset)->find_all_by('parent_id', $category_id);
Template::set('categorydetails', $categorydetails);
Template::set('productrecords', $productrecords);
Template::set('subcategoriesrecords', $subcategoriesrecords);
Template::render();
}//end browsecategory
The view looked something like this:
<?php if (isset($subcategoriesrecords) && is_array($subcategoriesrecords) && count($subcategoriesrecords)) : ?>
<?php echo $this->pagination->create_links(); ?>
<div>
<h1 class="page-header">Sub-categories in <i> <?php echo $categorydetails->title; ?></i>:</h1>
</div>
<br />
<table class="table table-striped table-bordered">
</table>
<?php endif; ?>
<?php if (isset($productrecords) && is_array($productrecords) && count($productrecords)) : ?>
<?php echo $this->pagination->create_links(); ?>
<div>
<h1 class="page-header">Products in category <i> <?php echo $categorydetails->title; ?></i>:</h1>
</div>
<br />
<?php endif; ?>
This didn't work because i realize that i was really only creating one pagination object. Although I had two sets of code, one for each dataset, I was overwritting ( i think) the first pagination object with the second one I declared.
In any case, when the system would dislpay the two sets of data on the view, clicking on the pagination links for products would trigger paging for the categories. No matter what I did, I couldn't page through the products.
Second Attempt
I decided to try to use ajax. So in my controller, i split up the functionality to look up the categories and products into two methods, like so:
public function browsecategory($category_id)
{
//find any subcategories for this category
$this->load->model('category/category_model');
$this->load->model('category/product_category_model');
$subcategoriesrecords = $this->category_model->find_all_by('parent_id', $category_id);
//look up category information ... used for display purposes.
$categorydetails = $this->category_model->find_by('category_id', $category_id);
//pagination for subcategories
$this->load->library('pagination');
$config['base_url'] = site_url('/product/browsecategory/'.$category_id);
$config['uri_segment'] = 4;
$config['total_rows'] = count($subcategoriesrecords);
$config['per_page'] = 5;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$offset = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
$subcategoriesrecords = $this->category_model->limit($config['per_page'], $offset)->find_all_by('parent_id',$category_id);
//check for any products within this current category.. if exists, include ajax to display
if ( count ($this->product_category_model->find_all_by('category_id', $category_id)) > 0 ) {
//add jquery logic to call getproductsincategory on document load
$inline = "$.ajax({
url:'http://myserver/myapp/product/getproductsincategory/".$category_id."',
type:'POST',
dataType:'html',
contentType : 'text/html',
success: function(returnDataFromController) {
$('#products').html(returnDataFromController);
},
error: function(jqXHR, textStatus, errorThrown)
{
alert(errorThrown);
}
});
";
Assets::add_js( $inline, 'inline' );
}
Template::set('categorydetails', $categorydetails);
Template::set('subcategoriesrecords', $subcategoriesrecords);
Template::render();
}//end browsecategory
public function getproductsincategory($category_id)
{
//find all products in category.
$this->load->model('category/product_category_model');
$this->load->model('category/category_model');
//pagination for prodcuts
$this->load->library('pagination');
$config['base_url'] = site_url('/product/browsecategory/'.$category_id);
$config['uri_segment'] = 5;
$config['total_rows'] = count($productrecords);
$config['per_page'] = 10;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$offset = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
$productrecords = $this->product_model->limit($config['per_page'], $offset)->find_all_by('category_id',$category_id);
//build html string for displaying products
print $htmlstring;
}
Sadly, i still have the same results. The system displays two sets of links, but they only seem to be tied to the categories. I cannot page through products using the product pagination links.
Can someone tell me if this can be done... and if so, where I'm going wrong?
Thanks.
Check out https://github.com/EllisLab/CodeIgniter/wiki/AJAX-Pagination-with-CI-Pagination-Library There are also a bunch of tutorials out here on how to use it https://www.google.com/search?q=codeigniter+pagination+ajax
I think the main issue is that the generated pagination links are simply an href to the next/previous/first/last results. You will want to override the second set of pagination links so that they request and re-populate the second pane with the new data.
Ok, I followed the instructions in the example perfectly. Ultimately, pagination works, kind of.
I get all of the pages listed: 1 | 2 | > | Last. Etc.
The first one is active, like it should be. I did the querying correctly as well, because each link will result in the correct query.
However, when I click on number 2, it will show me the next set of products correctly, but it will display the pagination from the first page.
Whatever pagination button I click on, will return the main pagination set: 1 (selected) | 2 | > | Last. It never changes! I'm loosing my patience, can someone help?
I think I might know whats going on. You need to tell the pagination library which segment of the URL holds the offset.
For example, if your URL is /products/browse/all/20, you need to tell CodeIgniter that the 4th segment holds the offset
$config['uri_segment'] = 4;
The default for the library is URL segment #3. If the offset in your URL is not in position 3 and you forget to tell the pagination library this, it will interpret the wrong segment as being the offset. This can lead to the kind of behaviour you describe above where the pagination does not appear to change.
I also came across same error and finally was able to fix it. Just thought to share the code script, may be someone will be able to use it.
=====> Controller
// Default function
function index()
{
// Display listing
$this->listing();
}
function listing($argDataArr = array())
{
// Initialize pagination
$pageArr['base_url'] = $this->config->item('categoryBeAction')."/listing";
$pageArr['total_rows'] = 15; //assume
$pageArr['per_page'] = 5; //assume
//You need to tell the pagination library which segment of the URL holds the offset.
$pageArr['uri_segment'] = 4; //URL eg: http://localhost/myproject/index.php/backend/category/listing/5
$this->pagination->initialize($pageArr);
// Get list of categories
// Create data array and pass data to get function
$dataArr['limitRows'] = $pageArr['per_page'];
$dataArr['limitOffset'] = $this->uri->segment(4); //the dynamic value from this segment will be used as offSet
$viewArr['listArr'] = $this->category_model->get($dataArr);
//rest of the code...
}
======> Model
function get($argDataArr = array())
{
//Select the fields required
$this->db->select('id, name, parent_id, status');
$this->db->from($this->config->item('tbl_category','dbtables'));
$this->db->where('parent_id', $parentId);
$this->db->limit($argDataArr['limitRows'], $argDataArr['limitOffset']);
$this->db->order_by("name", "asc");
$query_result = $this->db->get();
return $query_result;
}
======> View page
<!-- Pagination -->
<tr>
<td align="right">
<?php echo $this->pagination->create_links(); ?>
</td>
</tr>
Which example?
echo $this->pagination->create_links();
^^Is this in your view?
I want to paginate query results in CodeIgniter to look like this:
Problem 1 :
The Pagination class always outputs numeric links. I just want to show next and back links.
Problem 2 :
$data['links'] = $this->pagination->create_links(); returns all of the links as a string. How can I separate the next and back links and put next to the right and back to the left ?
suppose url is: http://localhost/controller/method/
do following in your controller function
...
function method($page_num)
{
...
$data['next_link'] = $page_num + 1;
$data['prev_link'] = $page_num;
...
$this->load->view('<veiw_name>', $data);
}
do this in your view
...
Prev
Next
....