【问题标题】:PHP Codeigniter PaginationPHP Codeigniter 分页
【发布时间】:2017-01-17 04:14:30
【问题描述】:

您好,我被困在 codeigniter 分页上。我得到总共 6 行,每页显示 2 行。

我正在获取分页链接 {Page: 1,2,3},其中 1 不可点击。

点击第 2 页时,我的链接看起来像 localhost/demo/index.php/home/press/?per_page=0/2 但我在 sql 上遇到错误

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“/2,2”附近使用正确的语法

SELECT * FROM media where media_type = 'PRESS' AND id <> 0 ORDER BY id DESC LIMIT 0/2

同样的错误,当我点击链接时获得分页链接:3 我不知道如何解决这个问题。我正在分享我在控制器、模型和视图上使用的代码。

我的控制器代码:

function press() 
{
    $this->load->model('Home_model');
    $page= $this->input->get('per_page') ? $this->input->get('per_page') : 0;
    $this->load->library('Pagination');
    $data['page'] = $this->input->get('page');
    if($data['page'] == '') {
        $data['page'] = $config['per_page'] = '2'; // Per Page
    } else {
        $data['page'] = $config['per_page'] = $this->input->get('page');
    }
    $config['first_url']='0';
    $pageno = $this->input->get('per_page');
    if($pageno == ''){
        $pageno = '0';
    } 
    $url_to_paging = $this->config->item('base_url');
    $config['base_url'] = $url_to_paging.'home/press/?per_page='.$page;
    $return = $this->Home_model->pressdata($config['per_page'],$pageno, $data);

    $data['pressdata'] = $return['result']; // Get Two Row Data
    $config['total_rows'] = $return['count']; // Total Count 6

    $this->pagination->initialize($config);
    $this->load->view('press',$data);
}

型号:

function pressdata($pg_num, $offset, $content) 
{
    if($offset == ''){
        $offset = '0';
    }
    $sql = "SELECT * FROM  media where media_type = 'PRESS' AND id <> 0";
    if($pg_num!=0 || $pg_num!="")
    {
        $sql .= " ORDER BY id DESC LIMIT ".$offset.",".$pg_num;
    } 
    $query = $this->db->query($sql);
    $sql_couint = "SELECT * FROM  media where media_type = 'PRESS' AND id <> 0";
    $query1 = $this->db->query($sql_couint);
    $ret['result'] = $query->result();
    $ret['count']  = $query1->num_rows();
    return $ret;
} 

【问题讨论】:

标签: php codeigniter codeigniter-3


【解决方案1】:
function pressdata($pg_num, $offset, $content) 
{
    if($offset == ''){
        $offset = '0';
    }
    $sql = "SELECT * FROM  media where media_type = 'PRESS' AND id => 0";
    if($pg_num!=0 || $pg_num!="")
    {
        $sql .= " ORDER BY id DESC LIMIT ".$offset.",".$pg_num;
    } 
    $query = $this->db->query($sql);
    $sql_couint = "SELECT * FROM  media where media_type = 'PRESS' AND id => 0";
    $query1 = $this->db->query($sql_couint);
    $ret['result'] = $query->result();
    $ret['count']  = $query1->num_rows();
    return $ret;
} 

<-------id should be greater and equal to zero----->

【讨论】:

    【解决方案2】:

    我终于找到了解决这个问题的方法。 这是Controller、Model、View的最终代码。

    控制器代码:

      function press()
       {
        $this->load->library('pagination');
        $url_to_paging = $this->config->item('base_url'); // URL here (www.example.com)
        $config['base_url'] = $url_to_paging.'home/press/'; // www.example.com/home/press
        $config['per_page'] = '4'; // Per Page Data Show 4
        $data = array();
        $return = $this->Home_model->pressdata($config['per_page'],$this->uri->segment(3));
        $data['pressdata'] = $return['result'];
        $config['total_rows'] = $return['count'];
        $this->pagination->initialize($config);
        $this->load->view('press', $data);
        }
    

    型号代码:

    function pressdata($num, $offset) 
    {
        if($offset == '')
        {
            $offset = '0';
        }
        $sql = "SELECT * FROM media where media_type = 'PRESS' AND id <> 0 ";
        if($num!='' || $offset!='')
        {
            $sql .= " order by id desc limit ".$offset.",".$num."";
        }
        $query = $this->db->query($sql);
        $sql_count = "SELECT * FROM media  WHERE media_type = 'PRESS' AND id <> 0";
        $query1 = $this->db->query($sql_count);
        $ret['result'] = $query->result_array();
        $ret['count']  = $query1->num_rows();
        return $ret;
    }
    

    查看分页链接:

        if($this->pagination->create_links()) {  ?>
           <div style="clear:both; border-top: solid 1px #e9ecf1;"></div>
             <div style="float:right;">
               <span class="pagination" style="margin:0px; border:none;">
                    <?php echo $this->pagination->create_links(); ?>
               </span>
              </div>
        <?php } 
    

    【讨论】:

      【解决方案3】:

      这样试试

      $this->load->library('pagination');
      $this->load->model('Home_model');
      
      $config['base_url'] = base_url().'home/press/;
      $config['total_rows'] = 200;   // Total no of rows returned from the database table
      $config['uri_segment'] = 3;    // It is the page no,used as offset in model
      $offset = 0;
      
      if ($this->uri->segment($config['uri_segment']) != "") {
          $offset = $this->uri->segment($config['uri_segment']);
      }
      
      $config['per_page'] = 20;      // you can change it as you want
      $return = $this->Home_model->pressdata($config['per_page'],$offset, $data); 
      $this->pagination->initialize($config);
      

      在你的模型中你可以做这样的事情

      function pressdata($per_page, $offset) {
          if($per_page !== '' && $offset !== '') {
              $this->db->limit($per_page, $offset);
          }
          //rest of your query
      }  
      

      不要复制粘贴它,我只是想给你一个想法。修改它,根据您的需要实现它。让我知道它是否有效。浏览 CodeIgniter 的 pagination 库以获取更详细的文档。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-07
        • 2023-03-16
        相关资源
        最近更新 更多