标签:php limit pagination codeigniter offset
如您所见,我正在查询数据库中的问题列表.
我的模型返回一个问题计数(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′(这是正确的).
但分页输出显示24个问题,跳过我数据库中的第一行/问题.
起初我认为这可能是因为错误的偏移,但在第一页中设置为0.
如果我不使用分页,我的控制器会输出所有25个问题.因此,似乎有一些我正在做的分页限制/偏移可能是罪魁祸首.
任何想法在这里可能是错的?
谢谢你的帮助.
模型(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;
");
型号(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;应该返回$query-> num_rows();.
另外,尝试将LIMIT $limit OFFSET $offset更改为LIMIT $offset,$limit.
标签:php,limit,pagination,codeigniter,offset 来源: https://codeday.me/bug/20190710/1418709.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。