[英]Codeigniter pagination not showing first row of query result
正如您在下面看到的,我正在查詢數據庫以獲取問題列表。
我的 model 返回問題計數( count_questions()
)以及問題本身( get_questions($args)
),然后分頁。
$limit = '10';
$count = $this->forum_model->count_questions();
$offset = $this->uri->segment(3, 0);
$this->load->library('pagination');
$config['base_url'] = base_url() . 'forum/all/';
$config['total_rows'] = $count;
$config['per_page'] = $limit;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['q'] = $this->forum_model->get_questions(NULL, $limit, $offset);
$data['pag_links'] = $this->pagination->create_links();
我看到的奇怪行為是count_questions()
返回“25”(這是正確的)。
但是分頁 output 顯示 24 個問題,跳過了我數據庫中的第一行/問題。
起初我以為這可能是因為偏移量錯誤,但在第一頁它被設置為 0。
如果我不使用分頁,我的 controller 會將所有 25 個問題輸出到我的視圖中。 所以似乎我正在對分頁限制/偏移量做一些事情,這可能是罪魁禍首。
任何想法這里可能有什么問題?
感謝您的幫助。
Model (get_questions)
function get_questions($forum_qa_id = NULL, $limit = NULL, $offset = NULL)
{
($forum_qa_id === NULL) ? ($forum_qa_id = "'%'") : ($forum_qa_id = $forum_qa_id);
($limit === NULL) ? ($limit = 1) : ($limit = $limit);
($offset === NULL) ? ($offset = 0) : ($offset = $offset);
$query = $this->db->query("
SELECT forum_qa.*,
user_profiles.*,
c.*,
n.pid,
v.*,
Ifnull(n.ans_count, 0) AS ans_count
FROM forum_qa
JOIN user_profiles
ON user_id = forum_qa_author_id
LEFT JOIN (SELECT *
FROM votes) AS v
ON forum_qa_id = v.forum_qa_id_fk
LEFT JOIN (SELECT forum_cm_id,
forum_cm_author_id,
forum_qa_id_fk,
forum_cm_text,
forum_cm_timestamp,
forum_cm_flag,
first_name AS forum_cm_first_name,
last_name AS forum_cm_last_name,
facebook_id AS forum_cm_fb_id,
picture AS forum_cm_picture,
moderator AS forum_cm_moderator
FROM forum_cm
JOIN user_profiles
ON user_id = forum_cm_author_id) AS c
ON forum_qa_id = c.forum_qa_id_fk
LEFT JOIN (SELECT forum_qa_parent_id AS pid,
COUNT(*) AS ans_count
FROM forum_qa
WHERE forum_qa_parent_id IS NOT NULL
GROUP BY forum_qa_parent_id) AS n
ON forum_qa_id = n.pid
WHERE forum_qa_id LIKE $forum_qa_id
AND forum_qa_parent_id IS NULL
ORDER BY forum_qa_timestamp DESC
LIMIT $limit
OFFSET $offset;
");
Model (count_questions)
function count_questions()
{
$query = $this->db->query("
SELECT *
FROM forum_qa
WHERE forum_qa_type = 1;
");
return $query->num_rows;
}
return $query->num_rows;
應該return $query->num_rows();
.
另外,嘗試將LIMIT $limit OFFSET $offset
更改為LIMIT $offset,$limit
。
問題是,當默認頁面執行時,它從第一行而不是零開始記錄,這就是第一行丟失的原因。 我應用了以下代碼來解決這個問題
$page = ($this->uri->segment(3))? $this->uri->segment(3): 0;
請根據您的要求編輯段 URL
$this->uri->segment(3, 0);
默認情況下,如果段不存在,function 返回 FALSE(布爾值)。 所以我覺得不對??
($offset === NULL) ? ($offset = 0) : ($offset = $offset);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.