codeigniter pagination link is not working when i

2019-08-26 07:10发布

问题:

when i click on the link in view it shows no page found.can any one help me ?. also tell me why those links are not working?. The display of data is okay. But the link for go to other page of the table it is not working. When i click the "1,2,3" page not found. It is still in the same place.. i m guessing it will be post or get problem?.

this is my controller.

<?PHP
/**
* 
*/
class Search extends MY_Controller
{

    public  function index ()

    {   
        if  ($this->form_validation->run('search') == FALSE)

       {


            $this->load->model('City');
           $deals=$this->City->default_city();
           foreach ($deals as $key) 
           {

                   $city_id=$key->city_id;
                   $city_name=$key->city_name;
                   $city_status=$key->city_status;

           } 



        }

        else

       {


            $city_name=$this->input->post('city_name');
            $this->load->model('City');
            $city_id=$this->City->other_city($city_name);


            foreach ($city_id as $row)

            {
                    $city_id= $row->city_id;
                    $city_name=$row->city_name;
                    $city_status=$row->city_status;


            }
        }   

            $deal_name=$this->input->post('deal_name');
            $data['city_id']=$city_id;
            $data['city_name']=$city_name;
            $data['city_status']=$city_status;
            $this->load->model('Deals');
            $total_deals=$this->Deals->total_deals();
            $config=[
            'base_url' => base_url().'Search',
            'per_page' => 3,
            'total_rows' => $this->Deals->number_rows($deal_name,$city_id,$city_status),
            'uri_segment'=> 2,
            ];
            $this->pagination->initialize($config);

            $user_search=$this->Deals->user_search($deal_name,$city_id,$city_status,$config['per_page'],$this->uri->segment(2));
            $data['links']=$this->pagination->create_links();
            $data['deals']=$user_search;

            $data['total_deals']=$total_deals;
            $total_categories=$this->Deals->total_categories();
            $data['total_categories']=$total_categories;
            for($i=1;$i<=$total_categories;$i++)

            {
                $deals_by_categories[]=$this->Deals->deals_by_categories($i);
            }
            $category_name=$this->Deals->categories();
            //$category_name = json_decode(json_encode($category_name), true);
            $data['category_name']=$category_name;
            $data['deals_by_categories']=$deals_by_categories;
            $this->load->view('page-search',$data);








    }
}
?>

this is my model function

public function user_search($deal_name,$city_id,$city_status,$limit,$offset)  

    {  

           $query= $this->db->select('merchants.merchant_logo,deals.deal_title,deals.deal_description,deals.start_date,deals.end_date,deal_id')
           ->from('deals')
           ->join('merchants_branches', 'deals.branch_id=merchants_branches.branch_id', 'inner')
           ->join('merchants', 'merchants_branches.merchant_id=merchants.merchant_id', 'inner')
           ->join('cities','cities.city_id=merchants_branches.city_id','inner')
           ->where('merchants_branches.city_id',$city_id)
           ->where('cities.city_status',$city_status)
           ->where('deal_status', 1)
           ->like('deals.deal_name',$deal_name)
           ->limit($limit,$offset)
           ->get();
           return $query->result();

    }

     public function number_rows($deal_name,$city_id,$city_status)  

    {  

           $query= $this->db->select('merchants.merchant_logo,deals.deal_title,deals.deal_description,deals.start_date,deals.end_date,deal_id')
           ->from('deals')
           ->join('merchants_branches', 'deals.branch_id=merchants_branches.branch_id', 'inner')
           ->join('merchants', 'merchants_branches.merchant_id=merchants.merchant_id', 'inner')
           ->join('cities','cities.city_id=merchants_branches.city_id','inner')
           ->where('merchants_branches.city_id',$city_id)
           ->where('cities.city_status',$city_status)
           ->where('deal_status', 1)
           ->like('deals.deal_name',$deal_name)
           ->get();
           return $query->num_rows();

    }

this is my view

<?php 
                    if(count($deals)):
                    foreach ($deals as $deals) : 
                    date_default_timezone_set("Asia/Dubai");
                    $date=date('Y-m-d H:i:s');
                    $enddate=$deals->end_date;

                    $date = new DateTime($date);

                    $enddate = new DateTime($enddate);

// The diff-methods returns a new DateInterval-object...
                    $diff = $date->diff($enddate);
                    if($date<$enddate):
// Call the format method on the DateInterval-object


                 ?>

                        <a class="col-md-4 model-1" href="<?php echo base_url(); ?>DealDescription/<?php echo $deals->deal_id  ?>/<?php echo $city_status?>">
                            <div class="product-thumb model-2">
                                <header class="product-header">
                                    <img src="<?php echo base_url(); ?>img/logos/<?php echo $deals->merchant_logo ?>" alt="Image Alternative text" title="Food is Pride" />
                                </header>
                                <div class="product-inner">
                                    <h5 class="product-title"><?php echo $deals->deal_title ?></h5>
                                    <p class="product-desciption"><?php echo $deals->deal_description ?></p>
                                    <div class="product-meta"><span class="product-time"><i class="fa fa-clock-o"></i> <?php echo $diff->format('%a Day and %h hours');?></span>
                                        <ul class="product-price-list">
                                            <li><span class="product-price">$118</span>
                                            </li>
                                            <li><span class="product-old-price">$227</span>
                                            </li>
                                            <li><span class="product-save">Save 52%</span>
                                            </li>
                                        </ul>
                                    </div>
                                    <p class="product-location"><i class="fa fa-map-marker"></i> <?php echo $city_name;?></p>
                                </div>
                            </div>
                        </a>


                    <?php else: header('location:'.base_url().'SearchError'); endif; endforeach;?>
                         <?php else: header('location:'.base_url().'SearchError');

                    endif; 
                    ?>
                    <?php echo $links; ?>

回答1:

Due to the way CodeIgniter works, and the fact that you really can't have URI segments in your controller's index method, you need to move the search to a different method. This is pretty easy if you do something like:

public function index()
{
    $this->results();
}

public function results( $page = 1 )
{
     // your code
}

Notice now that you now actually have a page variable in URI segment 3. This is important, because CodeIgniter will be looking for that page based on the config:

$config['uri_segment'] = 3; // You can't use 2!

Also, in CodeIgniter it's important to use site_url, and not base_url for this pagination base_url:

$config['base_url'] = site_url('search/results'); // Do not uppercase "search"

Furthermore, if you want a natural user experience for pages, you should be using page numbers:

$config['use_page_numbers'] = TRUE;

Taking this approach, you still need to determine what your query offset is, because it's obviously not the page numbers:

$offset = ( $page * $config['per_page'] ) - $config['per_page'];

Also, make sure to read the user guide's page on pagination, as it's very helpful: https://www.codeigniter.com/userguide3/libraries/pagination.html

This should get you paginating in no time.



标签: codeigniter