shiv223
shiv223

Reputation: 45

Pagination links not working

I am trying to show my table in pagination.Links are coming but when i click on a 2 or 3 or anything it goes to /view_expenses/view&per_page= this. and i ll get a 404 error.why its not showing the rest data?

This is my controller

class View_expenses extends CI_Controller
 {
    public function __construct()
    {
        parent::__construct();
        $data['title']= 'View Expenses';
        $this->load->view('header_view',$data);
        $this->load->model('emp_expenses_model');
        $this->load->library('form_validation');
        $this->load->helper('url');
        $this->load->library('pagination');                 
            }


    public function index()
    {


            }
    function view($offset=0){
        $limit=5;
        //$this->uri->segment(3);
        $this->load->model('emp_expenses_model');
        //$result['contents']=$this->emp_expenses_model->getRows($limit,$offset);
        $result['countRows']=$this->emp_expenses_model->countRows();
        $this->load->library('pagination');
        $this->load->library('table');
        $config=array(
                        'base_url' =>site_url ('/view_expenses/view'),
                        'total_rows' => $result['countRows'],
                        'per_page' => $limit,
                        'uri_segment' => 3,
                        'num_links' => 1,
         );  
         //var_dump($config); 
        $this->db->limit(5);
        $this->pagination->initialize($config);
        $this->load->model('emp_expenses_model');
        $this->data['view_expenses'] = $this->emp_expenses_model->get_all();
        //$this->data['pagination'] = $this->pagination->create_links();
        //var_dump($this->data['pagination']);die("jk");
        $this->data['title'] = 'Payroll System';
        $this->data['message'] = $this->session->flashdata('message');
            $this->load->library('pagination');
        $this->load->view('view_expenses', $this->data);
        /*$this->load->view('add_list', $this->data);*/

    }   

This is my model

function getRows($limit,$offset)
    {
    $query=$this->db->select('expenses_id,id,dropdown,modeofpayment,amount')
           ->from('emp_expenses')
           ->limit($limit,$offset);
    $result=$query->get()->result_array();
    return $result;
    //var_dump($result);
    }
    function countRows()
    {
    //$query="select count(*) as count from emp_expenses";
    $result = $this->db->count_all_results('emp_expenses');
    //$result=$this->db->query($query);
    return $result;
    //var_dump('countRows');
}

This is My View

   <table cellspacing="0" cellpadding="2" border="0" id="tbl"  style="width:100%">
<tr style="background-color:#045c97">
 <?php echo $this->pagination->create_links()?>
  <td class="heading">Employee ID</td>
  <td class="heading">Drop Down</td>
  <td class="heading">Mode OF Payment</td>
  <td class="heading">Amount</td>
  <td class="heading">Edit</td>
  <td class="heading">Delete</td>
</tr>
<?php
foreach ($view_expenses as $m){
  $list_id = $m['id'];
  //print_r($list_id);die('adad');
  //$m['username']='';
?>
  <tr style="text-align:center;">
      <td><?php echo $m['id'] ?></td>
    <td><?php echo $m['dropdown'] ?></td>
            <td><?php echo $m['modeofpayment'] ?></td>
             <td><?php echo $m['amount'] ?></td>
    <td><a href="<?php echo site_url('view_expenses/edit_expenses/'.$list_id) ?>"class="btn btn-primary btn-mini">Edit</a></td>
    <td>
      <?php 
        echo anchor('view_expenses/delete_expenses/'.$list_id, 'Delete', array('onClick' => "return confirm('Are you sure you want to delete?')"));
      ?>
    </td>

Upvotes: 0

Views: 91

Answers (2)

grepsedawk
grepsedawk

Reputation: 3411

Your query is written completely wrong, you must put a question mark before everything else in the URL.

 foo://example.com:8042/over/there?name=ferret&color=brown#nose
 \_/   \______________/\_________/ \______________________/ \__/
  |           |            |            |                    |
scheme     authority       path        query              fragment

Notice, the "Question-Mark", or query operator must come before any queries, but if there are more than one they should be seperated by the "&".

Try this:

/view_expenses/view?per_page=

As to the links not showing you what you want, your code is not correct in any way. Here is a tutorial in which I found easy to understand: http://www.phpfreaks.com/tutorial/basic-pagination

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157839

you need to use ? not & to separate a query string from the rest of request

Upvotes: 1

Related Questions