Codeigniter分页(在第1页发行之后)

时间:2017-07-17 14:26:57

标签: php codeigniter pagination

我的Db中有大约1000条记录。 搜索查询在我的搜索结果页面的 Page 1 中显示分页结果,但是当我转到第2页,第3页等等时......它并没有向我显示任何内容。它给我一条消息说没有找到记录。 我错过了与相对URI有关的事情吗?或者是别的什么 ?

控制器 - college_search.php

public function index()
{

    $param = str_replace('-',' ', $this->uri->segment(2));
    $param = strip_tags($param);

    $param2 = str_replace('-',' ', $this->uri->segment(3));
    $param2 = strip_tags($param2);

    $data['msg'] = '';

    //Pagination starts
    $total_rows = $this->posted_colleges_model->count_searched_job_records($param,$param2);
    $config = pagination_configuration(base_url("search-college/".$this->uri->segment(2)), $total_rows, 50, 3, 5, true);
    $this->pagination->initialize($config);
    $page = ($this->uri->segment(2)) ? $this->uri->segment(3) : 0;
    $page_num = $page-1;
    $page_num = ($page_num<0)?'0':$page_num;
    $page = $page_num*$config["per_page"];
    $data["links"] = $this->pagination->create_links();
    $data['total_rows'] = $total_rows;
    $data['page'] = $current_records;
    $data['from_record'] = $page+1;
    $data['result'] = $obj_result;
    $data['param'] = $param;
    $data['param2'] = $param2;
    //Pagination ends

    $obj_result = $this->posted_colleges_model->get_searched_colleges($param,$param2, $config["per_page"], $page);

模型 - posted_colleges_model.php

    public function count_searched_job_records($param,$param2) {
        $Q = $this->db->query('CALL count_ft_searched_colleges("'.$param.'","'.$param2.'")');   
         if ($Q->num_rows > 0) {
            $return = $Q->row('total');
        } else {
            $return = 0;
        }
        $Q->next_result();
        $Q->free_result();
        return $return;

    }

public function get_searched_colleges($param,$param2='', $per_page, $page) {        
       $Q = $this->db->query('CALL ft_search_job2("'.$param.'", "'.$param2.'", '.$page.', '.$per_page.')');
        if ($Q->num_rows > 0) {
            $return = $Q->result();
        } else {
            $return = 0;
        }
        $Q->next_result();
        $Q->free_result();
        return $return;

procedure1 count_ft_searched_colleges

    BEGIN
    SELECT COUNT(pc.ID) as total
    FROM `pp_companies` pc
    WHERE pc.sts = 'active'

AND (
        pc.company_name like CONCAT("%",param,"%")
        OR pc.company_city like CONCAT("%",param,"%")
        OR pc.company_location like CONCAT("%",param,"%")
        OR pc.company_state like CONCAT("%",param,"%")
        OR pc.category1 like CONCAT("%",param,"%")
        OR pc.category2 like CONCAT("%",param,"%")
        OR pc.category3 like CONCAT("%",param,"%")

    )
AND pc.company_city like CONCAT("%",param2,"%");
END

procedure2 ft_search_job2

    BEGIN

    SELECT pc.ID, pc.company_name, pc.company_logo, pc.company_slug, pc.sts,pc.company_location,pc.company_city,pc.company_state,pc.category1,pc.category2,pc.category3
    FROM `pp_companies` pc 
    WHERE pc.sts = 'active' 
    AND (
        pc.company_name like CONCAT("%",param,"%")
        OR pc.company_city like CONCAT("%",param,"%")
        OR pc.company_location like CONCAT("%",param,"%")
        OR pc.company_state like CONCAT("%",param,"%")
        OR pc.category1 like CONCAT("%",param,"%")
        OR pc.category2 like CONCAT("%",param,"%")
        OR pc.category3 like CONCAT("%",param,"%")

        )
        AND (pc.company_city) like CONCAT("%",param2,"%")
ORDER BY pc.ID DESC
    LIMIT from_limit, to_limit;
END

查看 - college_search_view.php

    <div class="searchjoblist col-md-<?php echo $col;?>"> 
<div class="paginationWrap"> <?php echo ($result)?$links:'';?> </div>

  <!--Colleges List-->

  <div class="searchpage">
    <div class="toptitlebar">
      <div class="row">
        <div class="col-md-6"><b><?php echo $param;?> Colleges</b></div>
        <div class="col-md-6 text-right"><strong>Colleges <?php echo $from_record.' - '.$page;?> of <?php echo $total_rows;?></strong> </div>
      </div>
    </div>

    <ul class="searchlist">
    <!--Colleges Row-->

      <?php if($result):
    $CI =& get_instance();
                        foreach($result as $row):
                            $company_logo = ($row->company_logo)?$row->company_logo:'no_logo.jpg';
                            $is_already_applied = $CI->is_already_applied_for_job($this->session->userdata('user_id'), $row->ID);       
              ?>
      <li>
        <div class="row">
          <div class="col-md-2"><a href="<?php echo base_url('company/'.$row->company_slug);?>" class="thumbnail" title="<?php echo $row->job_title;?>"><img src="<?php echo base_url('public/uploads/employer/thumb/college-logos/'.$company_logo);?>" alt="<?php echo base_url('company/'.$row->company_slug);?>" /></a></div>
          <div class="col-md-10">
              <div class="col-md-7"> <a href="<?php echo base_url('company/'.$row->company_slug);?>"  class="jobtitle" title="View details about <?php echo $row->company_name;?>"><?php echo $row->company_name;?></a>
              <div class="location"><?php echo $row->company_location;?></div>

            </div>
            <div class="col-md-5">
              <?php
            if($is_already_applied=='yes'):
          ?>
              <a href="javascript:;" class="applybtngray">Already Applied</a>
              <?php else:?>
              <a href="<?php echo base_url('company/'.$row->company_slug);?>" class="applybtn">More Details</a>
              <?php endif;?>

            </div>
            <div class="clearfix"> </div>               
          </div>              
        </div>
         <p><?php echo word_limiter(strip_tags(str_replace('-',' ',$row->company_description)),22);?></p>
      </li>
      <?php endforeach; else: ?>
      <div class="err" align="center">
        <p><strong> <?php echo ($param=='')?'Please enter keywords above to display the relevant opened jobs.':'Sorry, no record found';?> </strong></p>
      </div>
      <?php endif;?>
    </ul>
  </div>

  <!--Pagination-->

  <div class="paginationWrap"> <?php echo ($result)?$links:'';?> </div>
</div>

routes.php文件

$route['search-college'] = 'college_search/index/$1';
$route['search-college/(:any)'] = 'college_search/index/$1';

1 个答案:

答案 0 :(得分:0)

<强>更新

尝试使用更标准的方式设置分页

$config['base_url'] = base_url('search-college');   
    //assuming example.com/search-college/[PAGE_NUMBER]

$config['total_rows'] = 200;
$config['uri_segment'] = 2;  
    //assuming example.com/search-college/[PAGE_NUMBER]

$config['num_links'] = 5;
$config['use_page_numbers'] = true;
$config['total_rows'] = $total_rows;
$config['per_page'] = 50;

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

原始答案

从上面发布的代码中,似乎有一些缺少逻辑来计算ft_search_job2过程的from_limit和to_limit。

为了让分页正常工作并使用过程(您应该尝试使用CodeIgniter查询生成器),计算偏移量如下:

from_limit = $page*$per_page

这是假设页面为0索引,而不是从1开始