Welcome Guest, Not a member yet? Register   Sign In
[Solved] Codeigniter Pagination Will Not Go Back Page
#1

(This post was last modified: 10-28-2015, 01:37 AM by wolfgang1983.)

I have got a List controller where I can sort the table thead links all working fine.

The problem is when I click on a pagination link and go to that page I click on it will not let me click and go back to a different page.

$this->uri->segmet(5); is where I think issue is because it is the page number. Every thing else works fine it just will not let me go back to a different page when uri->segment(5) active.


PHP Code:
<?php

class Lists extends MY_Controller {

    public function 
__construct() {
        
parent::__construct();
        
$this->load->library('paginations');
        
$this->load->model('layouts/model_layouts');
    }

    public function 
index() {
        
$data['title'] = 'Layout List';

        if (
$this->uri->segment(3)) {
            
$sort $this->uri->segment(3);
        } else {
            
$sort '/name';
        }

        if (
$this->uri->segment(4)) {
            
$order $this->uri->segment(4);
        } else {
            
$order '/asc';
        }

        
$url '';

        if (
$this->uri->segment(3)) {
            
$url .= '/'$this->uri->segment(3);
        }

        if (
$this->uri->segment(4)) {
            
$url .= '/'$this->uri->segment(4);
        }

 
               // Problem Here

        
if ($this->uri->segment(5)) {
            
$url .= '/'$this->uri->segment(5);
        } 

        
$data['breadcrumbs'] = array();

        
$data['breadcrumbs'][] = array(
            
'text' => '<i class="fa fa-home"></i> Home',
            
'href' => site_url('common/dashboard')
        );

        
$data['breadcrumbs'][] = array(
            
'text' => 'Layout List',
            
'href' => site_url('layout/lists' $url)
        );

        
$data['add'] = site_url('layout/add') . $url;

 
               $data['layouts'] = array();

        
$this->load->library('pagination');

        if (
$this->uri->segment(3) == 'name') {
            
$start = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
        } else {
            
$start = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        }

        if (
$this->uri->segment(3) == 'status') {
            
$start = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
        } else {
            
$start = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        }

        
$config['base_url'] = base_url('layout/lists' $url);
        
$config['total_rows'] = $this->model_layouts->count_total();
        
$config['per_page'] = $this->config->item('admin_limit');

        
$this->pagination->initialize($config);

        
$data['pagination'] = $this->pagination->create_links();

        
$results $this->model_layouts->get_layouts($config['per_page'], $start); 

        foreach (
$results as $result) {
            
$data['layouts'][] = array(
                
'layout_id' => $result['layout_id'],
                
'name' => $result['name'],
                
'status' => ($result['status'] ? '<div class="label label-success">Enabled</div>' '<div class="label label-warning">Disabled</div>'),
                
'edit' => ($this->uri->segment(3)) ? site_url('layout/edit' $url .'/'$result['layout_id']) : site_url('layout/edit/' $result['layout_id']),
                
'delete' => ($this->uri->segment(3)) ? site_url('layout/delete' $url .'/'$result['layout_id']) : site_url('layout/delete/' $result['layout_id']),
            );
        }

        
$success_flash_data $this->session->flashdata('success');

        if (isset(
$success_flash_data)) {
            
$data['success'] = $success_flash_data;

            unset(
$success_flash_data);
        } else {
            
$data['success'] = '';
        }

        
// Link for sort name must be left here

        
$url '';

        if (
$order == 'asc') {
            
$url .= '/desc';
        } else {
            
$url .= '/asc';
        }

        if (
$this->input->get('page')) {
            
$url .= $this->input->get('page');
        }

        
$data['sort_name'] = site_url('layout/lists/name') . $url;
        
$data['sort_status'] = site_url('layout/lists/status') . $url;

        
$url '';

        if (
$this->uri->segment(3)) {
            
$url .= '/'$this->uri->segment(3);
        }

        if (
$this->uri->segment(4)) {
            
$url .= '/'$this->uri->segment(4);
        }

        
$text_pagination 'Showing Rows %d to %d out of %d (%d Pages)';

        
//$data['results'] = sprintf($text_pagination, ($this->model_layouts->count_total()) ? (($page - 1) * $this->config->item('admin_limit')) + 1 : 0, ((($page - 1) * $this->config->item('admin_limit')) > ($this->model_layouts->count_total() - $this->config->item('admin_limit'))) ? $this->model_layouts->count_total() : ((($page - 1) * $this->config->item('admin_limit')) + $this->config->item('admin_limit')), $this->model_layouts->count_total(), ceil($this->model_layouts->count_total() / $this->config->item('admin_limit')));

        
$data['sort'] = $sort;
        
$data['order'] = $order;

        
$data['admin_page'] = 'template/layout/' strtolower(__CLASS__) . '_view';
        
$this->load->view('template/common/template'$data);
    }


Any suggestion and example world be great

I have been reading this http://www.codeigniter.com/user_guide/li...ation.html

I also have attached the controller

Attached Files
.php   Lists.php (Size: 4.01 KB / Downloads: 186)
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

(This post was last modified: 10-27-2015, 03:42 AM by sv3tli0.)

I can advice you to check what links are generated and if* there is something wrong, to search for the pattern which is causing it.
I see that you have many customisations at your code and I the problem should be some where inside ..
Best VPS Hosting : Digital Ocean
Reply
#3

I'm not sure whether I can find the problem, but I see a few places to start looking:
- Why is the constructor loading a library named 'paginations'? I'd imagine that if the library didn't exist, you'd get an error right away, but if it does exist, you don't appear to be using it in the controller.
- You check your URI segments multiple times and handle them inconsistently throughout your index method. For example, $sort and $order are prefixed with a slash if the related URI segment does not exist, otherwise they are not prefixed with a slash.
- Since you don't appear to be handling re-ordering of the URI segments, the $url variable should be built with all of the segments populated, even when they are not populated in the current URI, so it would probably be best to use the $sort and $order variables rather than checking segments 3 and 4 again.
- After loading the pagination library, you check whether segment 3 == 'name' and set the $start variable accordingly with an if/else statement. Then you check segment 3 again (this time whether it's == 'status') and set the $start variable in another if/else statement. The if ($this->uri->segment(3) == 'status') block makes the if ($this->uri->segment(3) == 'name') block completely useless, so you should either remove the first if/else block, or fix the logic to reflect your intent (side note: this is one of the reasons people recommend commenting your code, so a developer can see the intent of the code from your comments and understand what the code should be doing).
- After your weird dance with the session's flashdata method, you reset your $url variable (why?), then check whether $order == 'asc', even though your default value for $order is '/asc', and you add '/desc' to the $url if $order is 'asc' (but you would add '/asc' if $order is '/asc'), so whatever the intent here, you're likely to see some odd behavior.
- Next you add the value of the 'page' $_GET variable to $url if it exists, even though I didn't see any other use of this variable in the controller, and it doesn't do anything to ensure the data is safe or otherwise appropriate to append to $url
- After resetting $url again, you rebuild most of the original $url, then don't do anything with it.

My recommendation would be to set a fixed use for each URI segment and set $config['uri_segment'] before initializing the pagination library so the page number is set to a fixed segment, then set the arguments on the index() method with default values, for example:

PHP Code:
public function index($sort 'name'$order 'asc'$start 0)
{
    
$config['uri_segment'] = 5

If you want to be able to omit the $sort and/or $order in the URL, then make the page segment 3:
PHP Code:
public function index($start 0$sort 'name'$order 'asc')
{
    
$config['uri_segment'] = 3

Then, you won't have to juggle them around (since you need a $sort for $order to do anything useful, right?).

If the page number absolutely has to be last, and the other items have to be optional, it just goes back to being more consistent with the handling of the URI segments. I would recommend just calling $this->uri->segment() once for each segment, then validating the values against the valid values, then setting $sort, $order, and $start accordingly.

In your current code, $start appears to only be the value of segment(5) (if segment(3) == 'status' and segment(5) is truthy), the value of segment(3) (if segment(3) is not 'status' but is still truthy) or 0. However, your code is perfectly capable of generating a URI in which the $start value should be in segment(4) (if segment(4) does not contain an $order value but segment(3) contains a $sort value). In fact, when you go to /layout/lists, it seems that your pagination would generate a series of links in the form /layout/lists/$start, but your code would assign the page number to $sort and potentially generate some links on the new page which cause even more problems.
Reply
#4

(This post was last modified: 10-28-2015, 01:37 AM by wolfgang1983.)

Thank you for advice now all working.

PHP Code:
<?php


class Lists extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('layouts/model_layouts');
    }

    public function index($sort_by 'name'$sort_order 'asc'$offset 0) {
        $data['title'] = 'Layout List';

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => '<i class="fa fa-home"></i> Home',
            'href' => site_url('common/dashboard')
        );

        $data['breadcrumbs'][] = array(
            'text' => 'Layout List',
            'href' => site_url('layout/lists')
        );

        $data['add'] = site_url('layout/add');

        $data['layouts'] = array();

        $data['fields'] = array(
            'name' => 'Layout Name',
            'status' => '<div class="text-center">Layout Status</div>',
            'Edit Layout' => '<div class="text-center">Edit Layout</div>'
        );

        $url $this->uri->segment(1) .'/'$this->uri->segment(2) .'/'$this->uri->segment(3) .'/'$this->uri->segment(4);

        if ($url == 'layout/lists' .'/'$sort_by .'/'$sort_order) {
            
            $config
['base_url'] = base_url('layout/lists') .'/'$sort_by .'/'$sort_order;    
            $config
['uri_segment'] = 5;        
            $offset 
$this->uri->segment(5);
            
            $results 
$this->model_layouts->search($this->config->item('admin_limit'), $offset$sort_by$sort_order); 

            foreach($results as $result) {
                $data['results'][] = array(
                    'layout_id' => $result['layout_id'],
                    'name' => $result['name'],
                    'status' => ($result['status']) ? 'Enabled' 'Disabled',
                    'edit' => ($offset) ? anchor('layout/edit' .'/'$sort_by .'/'$sort_order .'/'$offset .'/?layout_id='$result["layout_id"], '<i class="fa fa-pencil fa-2x"></i>', array('class' => 'btn btn-primary')) : anchor('layout/edit' .'/'$sort_by .'/'$sort_order .'/?layout_id='$result["layout_id"], '<i class="fa fa-pencil fa-2x"></i>', array('class' => 'btn btn-primary'))
                );
            }

        } else {

            $config['base_url'] = base_url('layout/lists');
            $config['uri_segment'] = 3;

            $offset $this->uri->segment(3);

            $results $this->model_layouts->search($this->config->item('admin_limit'), $offset$sort_by$sort_order); 

            foreach($results as $result) {
                $data['results'][] = array(
                    'layout_id' => $result['layout_id'],
                    'name' => $result['name'],
                    'status' => ($result['status']) ? 'Enabled' 'Disabled',
                    'edit' => ($offset) ? anchor('layout/edit' .'/'$offset .'/?layout_id='$result["layout_id"], '<i class="fa fa-pencil fa-2x"></i>', array('class' => 'btn btn-primary')) : anchor('layout/edit' .'/?layout_id='$result["layout_id"], '<i class="fa fa-pencil fa-2x"></i>', array('class' => 'btn btn-primary'))
                );
            }
        }

        $config['total_rows'] = $this->model_layouts->count_total();
        $config['per_page'] = $this->config->item('admin_limit');
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_link'] = false;
        $config['last_link'] = false;
           $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
['pagination'] = $this->pagination->create_links();

        $data['sort_by'] = $sort_by;
        $data['sort_order'] = $sort_order;

        $success_flash_data $this->session->flashdata('success');

        if (isset($success_flash_data)) {
            $data['success'] = $success_flash_data;
            unset($success_flash_data);
        } else {
            $data['success'] = '';
        }

        $data['admin_page'] = 'template/layout/' strtolower(__CLASS__) . '_view';
        $this->load->view('template/common/template'$data);
    }


When I go and click on edit button and then press cancel button now it goes back to the correct page on pagination

On the edit controller I need to use code below

PHP Code:
if ($this->uri->segment(5) == TRUE) {
 
  $data['cancel'] = site_url('layout/lists') .'/'$this->uri->segment(3) .'/'$this->uri->segment(4) .'/'$this->uri->segment(5);
} elseif (
$this->uri->segment(3) == FALSE) {
 
  $data['cancel'] = site_url('layout/lists');
} else {
 
  $data['cancel'] = site_url('layout/lists') .'/'$this->uri->segment(3) .'/'$this->uri->segment(4);



Lists View Table Example


PHP Code:
<table class="table table-bordered table-hover">

<
thead>
<
tr>
<?
php if ($sort_by == 'name') {?>
<td>
<?php echo anchor("layout/lists/name/" . (($sort_order == 'asc' && $sort_by == 'name') ? 'desc' 'asc'),
'Name', array('class' => $sort_order));?>
</td>
<?php } else {?>
<td>
<?php echo anchor("layout/lists/name/" . (($sort_order == 'asc' && $sort_by == 'name') ? 'desc' 'asc'),
'Name');?>
</td>
<?php }?>

<?php if ($sort_by == 'status') {?>
<td class="text-center">
<?php echo anchor("layout/lists/status/" . (($sort_order == 'asc' && $sort_by == 'status') ? 'desc' 'asc'),
'Status', array('class' => $sort_order));?>
</td>
<?php } else { ?>
<td class="text-center">
<?php echo anchor("layout/lists/status/" . (($sort_order == 'asc' && $sort_by == 'status') ? 'desc' 'asc'),
'Status');?>
</td>
<?php }?>

<td class="text-center">Edit Layout</td>
<td class="text-center">Delete Layout</td>

</tr>
</thead>
    <tbody>
    <?php foreach ($results as $result): ?>
        <tr>
            <td><?php echo $result['name'];?></td>
            <td class="text-center"><?php echo $result['status'];?></td>
            <td class="text-center"><?php echo $result['edit'];?></td>
            <td class="text-center"><?php echo $result['delete'];?></td>
        </tr>
    <?php endforeach ?>
    </tbody>
</table> 


Attached Files
.php   Lists.php (Size: 4.12 KB / Downloads: 120)
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB