CodeIgniter Paging class, active link not working - php

I am using the paging class of the codeigniter for the first time. Everything else is working fine, but whichever page I am going, the active link is always the [1] and it doesn't change. Also the next button is always linked to the first page. Culdn't figure out why ! Please help !
Controller
public function unverified_images()
{
$data['title'] = 'Choose from the below Images to verify them >>';
$data['total_rows'] = $this->admin_model->all_unverified_images();
$config['base_url'] = base_url().'index.php/admin/admin/unverified_images';
$config['total_rows'] = $data['total_rows']->num_rows();
$config['per_page'] = 1;
$config['num_links'] = 8;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['query'] = $this->db->where('image_status', 0)
->get('tbl_img', $config['per_page'], $this->uri->segment(4));
$data['links']=$this->pagination->create_links();
$this->load->view('admin/image_verify', $data);
}
public function verify_image()
{
$data['title'] = 'Choose from the below Images to verify them >>';
$data['msg'] = $this->admin_model->verify_image();
$data['query'] = $this->admin_model->all_unverified_images();
redirect('admin/admin/unverified_images');
}
Model
function all_unverified_images()
{
$this->db->where('image_status', 0);
$query = $this->db->get('tbl_img');
return $query;
}
View
<?php echo $links; ?>

Sound like that CI can't figure out what page you supposed to be on.
Try adding the uri_segment or the cur_page for the $config array.
The uri_segment should tell CI what part of the url holds the current page number, from your example code it seem to be 4 (from the query) while CI's default is 3.

Related

Pagination Issue in CodeIgniter

I'm facing an issue with the pagination in my system. Pagination is working fine here.
But if I'm searching something in that text field and if the result is more than 5 ($config['per_page'] = 5;) its not showing the pagination buttons.
And actually, I have 6 results for the search keyword 'shihas'.
And also I can see the 6th result once I go to www.example.com/path/2 .
But still, the pagination link is not there. Please tell me a solution for this issue.
Controller:
public function index()
{
//all the posts sent by the view
$search_string = $this->input->post('search_string');
$order = $this->input->post('order');
$order_type = $this->input->post('order_type');
//pagination settings
$config['per_page'] = 5;
$config['base_url'] = base_url().'admin/posts';
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 20;
$config['full_tag_open'] = '<ul>';
$config['full_tag_close'] = '</ul>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a>';
$config['cur_tag_close'] = '</a></li>';
//limit end
$page = $this->uri->segment(3);
//math to get the initial record to be select in the database
$limit_end = ($page * $config['per_page']) - $config['per_page'];
if ($limit_end < 0){
$limit_end = 0;
}
//if order type was changed
if($order_type){
$filter_session_data['order_type'] = $order_type;
}
else{
//we have something stored in the session?
if($this->session->userdata('order_type')){
$order_type = $this->session->userdata('order_type');
}else{
//if we have nothing inside session, so it's the default "Asc"
$order_type = 'Asc';
}
}
//make the data type var avaible to our view
$data['order_type_selected'] = $order_type;
//we must avoid a page reload with the previous session data
//if any filter post was sent, then it's the first time we load the content
//in this case, we clean the session filter data
//if any filter post was sent but we are on some page, we must load the session data
//filtered && || paginated
if($search_string !== false && $order !== false || $this->uri->segment(3) == true){
/*
The comments here are the same for line 79 until 99
if the post is not null, we store it in session data array
if is null, we use the session data already stored
we save order into the var to load the view with the param already selected
*/
if($search_string){
$filter_session_data['search_string_selected'] = $search_string;
}else{
$search_string = $this->session->userdata('search_string_selected');
}
$data['search_string_selected'] = $search_string;
if($order){
$filter_session_data['order'] = $order;
}
else{
$order = $this->session->userdata('order');
}
$data['order'] = $order;
//save session data into the session
if(isset($filter_session_data)){
$this->session->set_userdata($filter_session_data);
}
$data['count_posts']= $this->posts_model->count_posts($search_string, $order);
$config['total_rows'] = $data['count_posts'];
//fetch sql data into arrays
if($search_string){
if($order){
$data['posts'] = $this->posts_model->get_posts($search_string, $order, $order_type, $config['per_page'],$limit_end);
}else{
$data['posts'] = $this->posts_model->get_posts($search_string, '', $order_type, $config['per_page'],$limit_end);
}
}else{
if($order){
$data['posts'] = $this->posts_model->get_posts('', $order, $order_type, $config['per_page'],$limit_end);
}else{
$data['posts'] = $this->posts_model->get_posts('', '', $order_type, $config['per_page'],$limit_end);
}
}
}else{
//clean filter data inside section
$filter_session_data['search_string_selected'] = null;
$filter_session_data['order'] = null;
$filter_session_data['order_type'] = null;
$this->session->set_userdata($filter_session_data);
//pre selected options
$data['search_string_selected'] = '';
$data['order'] = 'id';
//fetch sql data into arrays
$data['count_posts']= $this->posts_model->count_posts('','');
$data['posts'] = $this->posts_model->get_posts('', '', $order_type, $config['per_page'],$limit_end);
$config['total_rows'] = $data['count_posts'];
}
//initializate the panination helper
$this->pagination->initialize($config);
//load the view
$data['main_content'] = 'admin/posts/list';
$this->load->view('includes/template', $data);
}
You can use datatable which is so real time.
DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, based upon the foundations of progressive enhancement, and will add advanced interaction controls to any HTML table.
Pagination, instant search and multi-column orderingSupports almost any data source:DOM, Javascript, Ajax and server-side processingEasily theme-able: DataTables, jQuery UI, Bootstrap, FoundationWide variety of extensions inc. Editor, Buttons, FixedColumns and moreExtensive options and a beautiful,
expressive APIFully internationalisableProfessional quality: backed by a suite of 2900+ unit testsFree open source software (MIT license)! Commercial support available.
Just put code:
$(document).ready(function(){
$('#myTable').DataTable();
});

Creation of table with images CodeIgniter

The table displays only text, where images column should display actual image instead of just the name of image.
Everything else is fine and works perfectly but how to display the pictures?
controller code:
function getData()
{
//check if the user is already logged in
if($this->session->userdata('logged_in'))
{
//the user is already logged in -> display the secret content
redirect('logged');
//get the session data
$session_data = $this->session->userdata('logged_in');
//get the username from the session and put it in $data
$data['user'] = $session_data['username'];
$config['base_url'] = site_url('LittleController/getData/');
$config['total_rows'] = $this->littlemodel->record_count('products');
$config['per_page'] = 10;
$this->pagination->initialize($config);
$data['Products'] = $this->littlemodel->getData(10, $this->uri->segment(3));
foreach ($data['Products'] as &$row)
{
$img = $row['image'];
$row['image']= "<img src='resources/images/thumbs/.$img'>";
//img('resources/images/thumbs/'.$img);
}
//$data["links"] = $this->pagination->create_links();
$this->load->view('mainpage1', $data);
}
else
{
//user isn't logged in -> display login form
$data['user'] = "Guest";
$config['base_url'] = site_url('LittleController/getData/');
$config['total_rows'] = $this->littlemodel->record_count('products');
$config['per_page'] = 10;
$this->pagination->initialize($config);
$data['Products'] = $this->littlemodel->getData(10, $this->uri->segment(3));
$data["links"] = $this->pagination->create_links();
$this->load->view('mainpage1', $data);
}
}
model code:
public function getData($limit, $offset)
{
$this->db->limit($limit, $offset);
$this->db->select('productName');
$this->db->select('productLine');
$this->db->select('productScale');
$this->db->select('productDescription');
$this->db->select('buyPrice');
$this->db->select('image');
$resultset = $this->db->get('products');
return $resultset->result_array();
}
view code:
$tmpl = array ( 'table_open' => '<table z-index="1000" id="table">' );
$this->table->set_caption("List of Products");
$this->table->set_heading('Name','Product Line','Product Scale','Product Description','Price per unit(€)','Image');
$this->table->set_template($tmpl);
echo $this->table->generate($Products);
in the controller I have to change
$row['image']= "<img src='resources/images/thumbs/.$img'>";
to
$row['image']= img(array('src'=>'resources/images/thumbs/'.$img.'', 'alt'=>'','width'=>'100px', 'height'=>'100px'));
that's sets the issue.
First, you need to find which section is executed user login section.
If login section is not loaded, there is no option to display the image the product array.
so please ensure it
If login section is loaded, it can not find the product array() because before it redirects ('logged') so please correct it and works for you

Codeigniter Pagination first page is always current page

I am pulling my hair out on this one. I am using CodeIgniters pagination library and right now it is always stuck on page 1 as the current page. I have checked a bunch of StackOverflow questions and I don't have the same problem as anyone else.
Here is my url structure
website.com/leaders/page/[page_number]
Here is the pagination code in my controller
$this->load->library('pagination');
$config['per_page'] = $query_config['limit'];
$config['base_url'] = base_url() . 'leaders/page/';
$config['total_rows'] = 2000; // I actually use a function for this number
$config['full_tag_open'] = '<div id="paginate">';
$config['full_tag_close'] = '</div>';
$config['first_link'] = '« First';
$config['last_link'] = 'Last »';
$config['use_page_numbers'] = true;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
When I echo the pagination in the view it looks like it works. The urls on each link is correct and everything looks fine. The last link shows the last page url and the current page is 1. However when I click on page 2 or any other page from the pagination, it still shows page 1 as the current page even though the url is as follows
website.com/leaders/page/2
I used the $this->uri->segment(3) to grab the page number for my database queries so the page number is in the right segment. Just to double check I set the $config['uri_segment'] values to 1,2,3,4,5,6 just to make sure.
I found out the problem while writing this but I am still confused
Then I thought maybe there is something going on with the url itself as I have a route directing it to the index method in the controller. Here is what my routes file looks like
routes.php
$route['leaders/page/(:num)'] = 'leaders/index';
$route['leaders/page'] = 'leaders/index';
Then I tried setting the base_url for the pagination config to send it to the index directly like so:
$config['base_url'] = base_url . 'leaders/index';
Now it seems to be working properly. But how do I make it so that it works with the url structure I had before? I just think it looks nicer and I don't really need a method in the controller for this. Is there something conflicting in my routes.php file?
Thanks
define cur_page and define controller like :
public function index($page=''){
//...
$page = ($page!='')? $page : 0;
$config["cur_page"] = $page;
//...
}
Use this in your code, hope this will work-
if ($this->uri->segment(3) > 0) {
$offset = $this->uri->segment(3) * $config['per_page'] - $config['per_page'];
} else {
$offset = $this->uri->segment(3);
}
I've been working for about 6 hours to make CI pagination work as I expected, and I don't know if is the order of the config elements or just my browser joking with me.
Below is my configuration array for pagination to work properly.
As you can see this is normal code, but my problem was, when I rendered for the first time, my pagination view, all seems to be ok, but if $config['per_page'] = 10; was set to 10, when I clicked on 11 link of pagination links, link number 2 showed Cseguimiento/buscar_reportes/# and looks like current page was 2 not 11.
I was very tired and I started to change the order of $config array, and suddenly it worked. SO I preesnt it here.
$config['base_url'] = base_url().'Cseguimiento/buscar_reportes/';
$config['uri_segment'] = 3;
$config['use_page_numbers'] = TRUE;
$config['first_link'] = FALSE;
$config['last_link'] = FALSE;
$config['next_link'] = '>';
$config['prev_link'] = '<';
$config["full_tag_open"] = '<ul class="pagination">';
$config["full_tag_close"] = '</ul>';
$config["first_tag_open"] = '<li>';
$config["first_tag_close"] = '</li>';
$config["last_tag_open"] = '<li>';
$config["last_tag_close"] = '</li>';
$config["next_tag_open"] = '<li>';
$config["next_tag_close"] = '</li>';
$config["prev_tag_open"] = "<li>";
$config["prev_tag_close"] = "</li>";
$config["cur_tag_open"] = "<li>";
$config["cur_tag_close"] = "</li>";
$config["num_tag_open"] = "<li>";
$config["num_tag_close"] = "</li>";
$config['total_rows'] = $this->mseguimiento->filas($fecha_inicio,$fecha_fin);
$config['per_page'] = 10;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = round($choice);
$page = $config['uri_segment'] * $config['per_page'];
$this->pagination->initialize($config);
$offset = ($this->uri->segment(3)-1)*$config['per_page'];
$output = array(
'pagination_link' => $this->pagination->create_links(),
'lista_reportes' => $this->mseguimiento->fetch_details($this->pagination->per_page, $offset,$fecha_inicio,$fecha_fin)
);

is it possible to dynamically change the url in codeigniter?

i have a page where books are to be display either with "grid" view or with "list" view.
So my current url is
http://localhost/thebestbookfinder.com/viewallbooks/books/pgn/grid/8
and if i click on "list" link from my view page then my current page should load with only changing this part /grid/ to /list/ and other same.
i.e http://localhost/thebestbookfinder.com/viewallbooks/books/pgn/list/8
I tried this but this is not helpful. Please help me to solve this issue.
inMyViewPage.php
<td width="10%" style="font-size:12px;color: red;">
<?php
echo "".anchor('#', 'Grid', array('id' => ''))."";
echo "".anchor(base_url().'viewallbooks/books/pgn/'.$this->uri->segment(4),' List', array('id' => ''))."";
?>
</td>
Instead of link if form submition solve my problem then please help me with that
Controller.php
function books()
{
$config = array();
$config['base_url'] = base_url().'viewallbooks/books/pgn/'.$this->uri->segment(4);
$config["total_rows"] = $this->Booksmodel->record_count_for_secondtopBooks('3');
$config['uri_segment'] = 5;
$config['per_page'] = 8;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
$data["query"] = $this->Subjectmodel->get_subjects();
$data["query1"]= $this->Editionmodel->get_edition();
$data["query2"]=$this->Booksmodel->get_all_book_list_atHomeTop('3',$config["per_page"], $page);
$viewType = ($this->uri->segment(4)) ? $this->uri->segment(4) : 'list';
if($viewType=='list')
{
$this->load->view('commonfiles/bookslistview',$data);
}
else {
$this->load->view('commonfiles/booksgridview',$data);
}
}
Need to add route in route.php like this
$route['viewallbooks/books/(:any)'] = "viewallbooks/books/$1";
Function books() shold contain 3 params with values by default
Below is more correct varian
function books($img_type = 'png', $view_type = 'list', $page = 0) {
$config = array();
$config['base_url'] = base_url().'viewallbooks/books/'.$img_type.'/'.$view_type.'/'.$page;
$config["total_rows"] = $this->Booksmodel->record_count_for_secondtopBooks('3');
$config['uri_segment'] = 5;
$config['per_page'] = 8;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data["query"] = $this->Subjectmodel->get_subjects();
$data["query1"]= $this->Editionmodel->get_edition();
$data["query2"]= $this->Booksmodel->get_all_book_list_atHomeTop('3',$config["per_page"], $page);
if($viewType=='list')
$this->load->view('commonfiles/bookslistview',$data);
else
$this->load->view('commonfiles/booksgridview',$data);
}
Do this one:
<?php echo "".anchor(base_url().'viewallbooks/books/pgn/grid/'.$this->uri->segment(5),' Grid', array('id' => ''))."";
echo "".anchor(base_url().'viewallbooks/books/pgn/list/'.$this->uri->segment(5),' List', array('id' => '')).""; ?>
And no need to change anything in above controller.
You will have to do either:
Reload the page with the new url (grid) to render that template
OR:
Render both the grid and list view and then dynamically (with some css/javascript) hide content based on what you'll want to see.
OR:
Load the grid-view with AJAX and replacing the list with it.

In Codeigniter Pagination's generated page links, page 1 is always selected

I'm about to pull my hair over this!
On initial load of my page with pagination (by CI), all rows are displayed, even if I only want 3. On click of other pages, however, it works fine (the correct rows are displayed), but Page 1 is always "selected" (not clickable), even if I click on Page 2, 3, etc.
Any ideas?
My CONTROLLER:
function album($type, $album_id, $album_name) {
$this->load->library('pagination');
$config['base_url'] = base_url("photo_store/album/$type/$album_id/$album_name/");
$config['total_rows'] = $this->Media_model->get_photos($album_id, 'display_date DESC', NULL, NULL, TRUE);
$config['per_page'] = 3;
$this->pagination->initialize($config);
$album_photos = $this->Media_model->get_photos($album_id, 'display_date DESC', $config['per_page'], $this->uri->segment(6), FALSE);
$this->_load_view(array(
/* some other variables here */
'album_photos' => $album_photos
));
)
private function _load_view($more_data) {
$data = array_merge($more_data, array( /* some other variables here */ ));
$this->load->view('template', $data);
}
My MODEL:
public function get_photos($album_id=NULL, $order_by='display_date DESC', $limit=NULL, $offset=NULL, $count=FALSE) {
$result = array();
$query = $this->db->select('medium.*')->join('medium', "$this->item.medium_id = medium.id", 'inner')->order_by($order_by);
$limit = $limit ? $limit : '0';
$offset = $offset ? $offset : '0';
if ($limit!=='0' && $offset!=='0') {
$query->limit($limit, $offset);
}
if ($album_id) { $result = $query->get_where($this->item, array('album_id' => $album_id)); }
else { $result = $query->get($this->item); }
if ($count){ return $result->num_rows(); }
else { return $result->result(); }
}
My VIEW:
foreach ($album_photos as $photo) {
//display photos here
}
echo $this->pagination->create_links();
You can just add this to the config array so the pagination knows where to find the current page:
$config['uri_segment'] = 4;
I believe part of the problem is coming in here:
if ($limit!=='0' && $offset!=='0') {
$query->limit($limit, $offset);
}
Since you don't have an else part for your statement, the query is never limited for that first page.
I suggest you change that code to
if ($limit!=='0') {
$query->limit($limit, $offset);
}
or even just
$query->limit($limit, $offset);
since $limit should theoretically never be null or 0 because you've set it to 3. $offset, unless set, should be 0 so you could replace null with it in your model's function,
When there are more than 3 uri segments passed in the url, selected page of pagination will not be displayed correctly, it will highlight the first page all the time.
Pagination is working, but the selected page is not diplayed correctly.
To solve this, solution:
go to Pagination.php file which is located at system->libraries->Pagination.php
just simply set
var $uri_segment = 4;// or 5 or 6;
It will work.
You can just add this to the config array so the pagination knows where to find the current page:
$config['uri_segment'] = 4; // Your appropriate uri segment: 5 or 6
Try to code your controller like below:
public function index($page=''){
//...
$page = ($page!='')? $page : 0;
$config["cur_page"] = $page;
//...
}

Categories