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 に答える