Codeigniter分页不显示查询结果的第一行

发布于 2024-11-30 01:40:38 字数 3392 浏览 2 评论 0原文

正如您在下面看到的,我正在查询数据库以获取问题列表。

我的模型返回问题计数 ( 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;
}

As you can see below I am querying the database for a list of questions.

My model returns a count of questions ( count_questions() ) as well as the questions themselves ( get_questions($args) ) which are then paginated.

    $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();

The odd behavior I am seeing is that count_questions() returns '25' (which is correct).

But the pagination output shows 24 questions, skipping the first row/question in my database.

At first I thought this could be because of wrong offset, but it is set to 0 in the first page.

If I do NOT use pagination, my controller outputs all 25 questions to my view. So there seems to be something I'm doing with pagination limit/offset that may be the culprit.

Any ideas what might be wrong here?

Thanks for helping.

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫罗兰の梦幻 2024-12-07 01:40:38

return $query->num_rows; 应该是 return $query->num_rows();

另外,尝试将 LIMIT $limit OFFSET $offset 更改为 LIMIT $offset,$limit

return $query->num_rows; should be return $query->num_rows();.

Also, try changing LIMIT $limit OFFSET $offset to LIMIT $offset,$limit.

带上头具痛哭 2024-12-07 01:40:38
$this->uri->segment(3, 0);

默认情况下,如果该段不存在,该函数将返回 FALSE(布尔值)。所以我觉得不对!?

($offset      === NULL) ? ($offset = 0) : ($offset = $offset);
$this->uri->segment(3, 0);

By default the function returns FALSE (boolean) if the segment does not exist. so i think isn't right!?

($offset      === NULL) ? ($offset = 0) : ($offset = $offset);
贩梦商人 2024-12-07 01:40:38

问题是,当默认页面执行时,它从第一行开始记录而不是零,这就是第一行丢失的原因。我应用了以下代码来解决这个问题

$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

请根据您的请求 URL 编辑分段

Problem is, when default page execute it start record from 1st row instead zero, that's why first row is missing. I applied following code to fix this problem

$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

Please edit segment as per your request URL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文