0

我的问题是我想从书籍列表中创建一个分页,我只是不知道如何使用活动记录或任何替代方法来做到这一点。这是我想要获取限制和偏移量以进行分页的查询。

   SELECT books.isbn,
              books.title,
              GROUP_CONCAT(CONCAT(author.fname,' ',author.lname)) AS FullName,
              books.date_published,
              books.price,
              books.stock,
              books.img,
              books.summary,
              books.status,
              bookscategory.id AS bookcategoryid,
              bookscategory.bookcategory FROM books 
            LEFT JOIN bookauthor
             ON books.isbn = bookauthor.isbn
            LEFT JOIN author 
            ON bookauthor.aid = author.id
            LEFT JOIN bookscategory 
            ON books.bookcategory = bookscategory.id
            WHERE books.stock > 0 GROUP BY books.isbn

提前致谢!

4

1 回答 1

0

尝试这样的事情,你需要相应地改变它,

你的控制器:

$limit                      = 20;
/*pagination start*/
$this->load->library('pagination');
$config['base_url']         = base_url().'controller_name/this_function_name/';
$config['total_rows']       = $this->your_model->total_count_books();
$config['per_page']         = 30;
$config['uri_segment']      = 3;
$config['next_link']        = 'Next';
$config['prev_link']        = 'Prev';
$config['cur_tag_open']     = '<span class="active_page">';
$config['cur_tag_close']    = '</span>';
$this->pagination->initialize($config);

$data           = array();
$data['books']  = $this->your_model->get_books($limit, $this->uri->segment( 3 ));

您的模型功能:

function total_count_books(){
    $sql    = "SELECT count( books.isbn ) as total FROM books 
        LEFT JOIN bookauthor
         ON books.isbn = bookauthor.isbn
        LEFT JOIN author 
        ON bookauthor.aid = author.id
        LEFT JOIN bookscategory 
        ON books.bookcategory = bookscategory.id
        WHERE books.stock > 0 GROUP BY books.isbn";
    $data   = $this->db->query( $sql )->row_array();
    return $data['total'];       
}

function get_books( $limit, $offset = 0 ){
    $sql    = "SELECT books.isbn,
          books.title,
          GROUP_CONCAT(CONCAT(author.fname,' ',author.lname)) AS FullName,
          books.date_published,
          books.price,
          books.stock,
          books.img,
          books.summary,
          books.status,
          bookscategory.id AS bookcategoryid,
          bookscategory.bookcategory FROM books 
        LEFT JOIN bookauthor
         ON books.isbn = bookauthor.isbn
        LEFT JOIN author 
        ON bookauthor.aid = author.id
        LEFT JOIN bookscategory 
        ON books.bookcategory = bookscategory.id
        WHERE books.stock > 0 GROUP BY books.isbn";
        if( !$offset ){
            $offset = 0;
        }
        $sql      .= " Limit {$offset}, {$limit}";
    return $data   = $this->db->query( $sql )->result_array(); #or ->result();
}
于 2013-10-23T07:48:17.593 回答