number of rows dynamic in codeigniter pagination links - codeigniter

I am working on Codeigniter (Gocart) Project.I am stuck on one task,I need your help.
I want to show option to select number of records per page.By default controller index action is as follows
function index($order_by="id", $sort_order="DESC", $code=0, $page=0, $rows=15)
and I am having dropdown in views for number of rows.
then in controller I have written a condition.
if(isset($_REQUEST['num_rows'])){
$rows = $_REQUEST['num_rows'];
}
else{
$rows = $rows;
}
and this is the codeigniter pagination code of controller.
$this->load->library('pagination');
$config['base_url'] = site_url($this->config->item('admin_folder').'/products/index/'.$order_by.'/'.$sort_order.'/'.$code);
$config['total_rows'] = $data['total'];
$config['per_page'] = $rows;
$config['uri_segment'] = 7;
$config['first_link'] = 'First';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$this->pagination->initialize($config);
and in view
<?php echo $this->pagination->create_links();?>
Now how will I set number of rows to dynamic for paginate links.I hope you got my question

First of all, This should do the trick:
if(isset($_REQUEST['num_rows'])){
$rows = $_REQUEST['num_rows'];
}
Else condition isn't really required here as you are assigning $rows to itself.
Other than this, CodeIgniter purges the $_REQUEST variable for security reasons. So I believe IF condition is always false. Unless you have set 'global_xss_filtering' variable to true in config file.
You can also use $this->input->get('num_rows') or $this->input->post('num_rows') depending on which method you are using.
Everything else seems OK to me.

Related

Pagination links always should be ten what ever which link is active,

Here my code is
$config['full_tag_open'] = '<ul class="pagination"> ';
$config['full_tag_close'] = '</ul><!--pagination-->';
$config['first_link'] = '« First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last »';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';
$config['next_link'] = 'Next →';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = '← Previous';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li ><a class="mj_blackbtn" href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';
Here links
enter link description here
Tell me particular css to show only 10 links and hide all except ten , what ever which link is active should show always only 10 links ever, I tried $Confiig['num_links'], but it distrb css, it keep 10 before and 10 after active
try this You can define current page on Codeigniter with and manage pages in controller.
$config["cur_page"] = $your_last_page_number;
I think you cannot show 10 link, because if you are at page 5, then what should it show?
1 2 3 4 [5] 6 7 8 9 10 ha?
The problem is CI always shows the links before and the links after equally, I think you should set
$config['num_links'] = 4;
to show 9 links instead of 10.

Link Pagination does'nt shown on codeigniter

I have to add pagination to my site and the link does not appear / shown
I Have an controller like this :
$data['a'] = 9;
$data['title'] = 'Articles';
$data['content'] = 'home/articles';
$this->load->library('pagination');
$q = $this->db->get_where('articles',array('article_category_id' => 18));
$config['uri_segment'] = 2;
$config['total_rows'] = $q->num_rows();
$config['per_page'] = 3;
$config['base_url'] = base_url() . 'article/';
$this->pagination->initialize($config);
$this->db->order_by('id','desc');
$this->db->limit($config['per_page'],$this->uri->segment(2));
$q = $this->db->get_where('articles',array('article_category_id' => 18));
$data['blogs'] = $q;
$data['pagination'] = $this->pagination->create_links();
$this->load->view('home/index', $data);
and view like this :
//Body of the site
Halaman : <?php echo $pagination; ?>
Site View

Codeigniter 3 pagination links and single post link throw 404

I have read other articles on this from here but could not solve this problem. My problems are 2.
Pagination shows but clicking links of pagination generates 404.
I have list of articles in db that are being displayed but when I
click single article link for single article page, it shows 404.
I am on localhost wamp. Here is my code. The commented code is what I have been trying but did not work for me.
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
routes.php
$route['default_controller'] = 'home';
$route['404_override'] = 'not_found';
$route['translate_uri_dashes'] = FALSE;
$route['articles/(:any)'] = 'articles/$1';
//$route['articles'] = 'articles';
config / baseurl
$config['base_url'] = 'http://localhost/practice/project-code-igniter/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Controller - Articles.php
class Articles extends CI_Controller {
public function index($start=0)
{
// $this->output->cache('86400');
$this->load->view('header');
//load model
// $this->load->model('articles_model');
// load 'get_articles' function from 'articles_model' model and store it in data
$this->load->library('pagination');
$data['articles']=$this->articles_model->get_articles(5,$start);
$config['base_url'] = base_url().'articles/';
$config['total_rows'] = $this->db->get('articles')->num_rows();
$config['per_page'] = 5;
$config["uri_segment"] = 3;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 2; //NUMBER OF LINKS BEFORE AND AFTER CURRENT PAGE IF ON PAGE ONE WILL SHOW 4 PAGES AFTERWARDS IF YOU HAVE ENOUGH RESULTS TO FILL THAT MANY
//config for bootstrap pagination class integration
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = "<< First";
$config['last_link'] = "Last >>";
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
//$data['pages'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
//$page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;
// //call the model function to get the department data
//$data['pages'] = $this->articles_model->get_articles($config["per_page"], $data['page']);
$data['pages'] = $this->pagination->create_links();
//$this->load->view('page_articles');
// load view and load model data with it
$this->load->view('page_articles',$data);
$this->load->view('footer');
}
// single article
// function post($perma){
// $this->load->view('header');
// $this->load->model('articles_model');
// $data['articles']=$this->articles_model->get_single_article($perma);
// $this->load->view('page_article',$data);
// $this->load->view('footer');
// }
}
Model / Articles_model
class Articles_model extends CI_Model {
// function get_articles() {
public function get_articles($limit, $start) {
// $query = $this->db->query("SELECT * FROM articles");
// $this->db->select()->from('articles');
// $this->db->select()->from('articles')->where('active',1)->order_by('date_added','desc')->limit(0,20);
$this->db->select()->from('articles')->where('status','1')->order_by('date_added','desc')->limit($limit, $start);
$query = $this->db->get();
// return object
// return $query->result();
// return array
return $query->result_array();
}
function get_articles_count(){
$this->db->select('id')->from('articles')->where('status',1);
$query = $this->db->get();
return $query->num_rows();
}
// for single article
// function get_single_article($perma){
// $this->db->select()->from('articles')->where(array('status'=>1,'permalink'=>$perma));
// $query = $this->db->get();
// return $query->first_row('array');
// }
}
View / page_articles
<?php
if(!isset($articles)) { ?> <div class="alert alert-info">No records</div> <?php } else
{
?>
<ol>
<?php
foreach($articles as $row)
{ ?>
<li>
<h4><a href="<?php echo base_url(); ?>articles/<?php echo $row['id']; ?>/<?php echo $row['permalink']; ?>/">
<?php echo $row['name']; ?>
</a></h4>
<p><?php echo substr(strip_tags($row['detail']),0,100).".."; ?>
<br>
Read more
</p>
<br>
</li>
<?php } ?>
</ol>
<?php
}
?>
</p>
<div>
<?php echo $pages; ?>
</div>
One problem...
public function index($start=0)
you seem to use $start for your page number and again for your offset value...
$data['articles']=$this->articles_model->get_articles(5,$start);
If $start is representing your page number, then you cannot also use it for your offset value.
Your offset needs to be calculated...
$offset = ($start - 1) * 5;
$data['articles']=$this->articles_model->get_articles(5, $offset);
Example:
With 5 records per page, page 4 would start with record #16. So 15 is the correct offset to get records #16, 17, 18, 19, & 20 for page 4.
Another potential issue...
$config["uri_segment"] = 3;
You've set the page number as segment 3, but your route is showing the page number at segment 2...
$route['articles/(:any)'] = 'articles/$1';
It also does not match this setting, which would also put your page number at segment 2...
$config['base_url'] = base_url().'articles/';
Based on your comment,
waow. What I did just now... I changed $config['base_url'] = base_url().'articles/'; to $config['base_url'] = base_url().'articles/index/'; and the pagination links show the page now but records are not changing. The 404 goes away and page is changing - records are first 5 on all pages....
then the route should be this...
$route['articles/index/(:any)'] = 'articles/$1';
So it matches the new base_url you describe...
$config['base_url'] = base_url().'articles/index/';

pagination in codeigniter - weird working

My pagination not working properly.
For example my $count = 3, $limit = 1 so i should have 3 pages
And this pagination look like this: 1 2 3 >
1 - Its not clickable and always bold (as active)
2 - When i click on it my uri segment(4) change on '1', dont change on bold (as active)
3 - When i click on it my uri segment(4) change on '2', dont change on bold (as active)
' > ' - Working only when i dont have uri segment(4) and when i click segment(4) change on '1' but when i click again its not working. When segment(4) = '2' and click it change on '1'.
' < ' - doesnt show at all
Controller:
$value = $this->uri->segment(3);
$this->load->library('pagination');
$limit = 1;
$offset = $this->uri->segment(4);
$this->db->limit($limit, $offset);
$this->db->where('when_data >='. date('Y-m-d'));
$this->db->select('*');
$this->db->from('meeting');
$this->db->join('category', 'category.id = meeting.id_cat');
$this->db->join('users', 'users.id = meeting.id_p');
$data['meeting'] = $query = $this->db->get();
$count = $query->num_rows();
$config['base_url'] = base_url() . 'main/kategoria/'. $value;
$config['total_rows'] = $count;
$config['per_page'] = $limit;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['cur_tag_open'] = '<b>';
$config['cur_tag_close'] = '</b>';
$config['next_link'] = '>';
$config['prev_link'] = '<' ;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('spotkania/kategoria', $data);
View:
<?php echo $pagination; ?>
Any idea?
you missed to add this config
$config['uri_segment'] = 4;//your case its 4.

Clicking on CI Pagination links produces wrong queries

I'm currently having problem when clicking on they pagination link of my site using CodeIgniter. The search function works fine, however, when I click on any of the pagination links, it seems like it does the filter again and again.
For example:
I choose "Rent" as my choice to search for:
Then click search I get 27 results. After that I click on the "2" pagination button at the bottom:
I get another 73 results and so on and so forth.
My Controller:
$this->load->library('pagination');
$search['base_url'] = base_url() . 'page_search/';
$search['per_page'] = 5;
$search['num_links'] = 5;
$search['num_tag_open'] = '<li>';
$search['num_tag_close'] = '</li>';
$search['first_link'] = 'First';
$search['first_tag_open'] = '<li class="first">';
$search['first_tag_close'] = '</li>';
$search['last_tag_open'] = '<li class="last">';
$search['last_tag_close'] = '</li>';
$search['next_tag_open'] = '<li>';
$search['next_tag_close'] = '</li>';
$search['prev_tag_open'] = '<li>';
$search['prev_tag_close'] = '</li>';
$search['last_link'] = 'Last';
$search['next_link'] = '»';
$search['prev_link'] = '«';
$search['full_tag_open'] = '<div class="pagination pagination-centered"><ul>';
$search['full_tag_close'] = '</ul></div>';
$search['cur_tag_open'] = '<li class="active"><a href="#">';
$search['cur_tag_close'] = '</a></li>';
$search['total_rows'] = $this->db->get_where( 'listing', $where )->num_rows();
$search['uri_segment'] = 2;
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize( $search );
$this->db->order_by("id", "desc");
$this->db->where( $where );
$data['results'] = $this->db->get( 'listing', $search['per_page'], $this->uri->segment(2);
$data['rows'] = $this->db->get_where( 'listing', $where )->num_rows();
$data['content'] = 'website/page-search';
$this->load->view('template/website/theme', $data);
Which $where is the query which I am sure it works fine.
UPDATE:
Also I have this in my route:
$route['page_search/(:num)'] = "page_search/index/$1";
Here's the link to the live site http://angkor21.com/
You are posting your search form and making your $where statement, thats cool, but in page 2, is your $where statement working correctly? I think this is where you need to watch out for. Print your generated query in the second page like this:
$this->db->last_query();
You need to somehow pass on these search parameter(s) to the next page to correctly generate the $where condition. For example:
if( $this->input->post(null) ){ #if the form is submitted
$saleType = $this->input->post('sale_type');
$propType = $this->input->post('prop_type');
$city = $this->input->post('city');
$district = $this->input->post('district');
$commune = $this->input->post('commune');
}else{
$saleType = $this->uri->segment(3);
$propType = $this->uri->segment(5);
$city = $this->uri->segment(7);
$district = $this->uri->segment(9);
$commune = $this->uri->segment(11);
}
$search['base_url'] = base_url() . 'page_search/sale_type/'.$saleType.'/prop_type/'.$propType.'/city/'.$city.'/district/'.$district.'/commune/'.$commune.'/page/';
$search['uri_segment'] = 13;

Resources