使用codeigniter中的下一个和上一个按钮进行分页

Posted

技术标签:

【中文标题】使用codeigniter中的下一个和上一个按钮进行分页【英文标题】:pagination with next and previous button in codeigniter 【发布时间】:2019-11-09 09:39:22 【问题描述】:

我尝试使用以下代码在 CodeIgniter 中添加分页,它带有这种格式的 1 2 3 4 5 >。但我需要添加下一个和上一个按钮。 控制器:

public function chooseBale($program_id = '')

    $config                = [];
    $config["base_url"]    = base_url() . "ginner/choosebale/" . $program_id;
    $config["total_rows"]  = $this->GinnerModel->get_count();
    $config["per_page"]    = 10;
    $config["uri_segment"] = 2;
    $choice                = $config["total_rows"] / $config["per_page"];
    $config["num_links"]   = round($choice);
    $this->pagination->initialize($config);
    $page          = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
    $data["links"] = $this->pagination->create_links();

    $data['pg_title']   = 'Choose Bale';
    $data['breadcrumb'] = ['Home' => '', 'Sale' => 'nolink', 'New Process' => 'nolink', 'Choose Bale' => 'nolink'];
    $data['left_menu']  = "left-menu-ginner.php";
    $data['program']    = $this->CommonModel->getTableWhere(PROGRAM, 'id =' . $program_id);
    $data['program_id'] = $program_id;
    $data['bale_list']  = $this->GinnerModel->baleList($program_id, $config["per_page"], $page);
    $this->load->view('common/header', $data);
    $this->load->view('ginner/choose-bale');
    $this->load->view('common/footer');

我在模型文件中使用了以下查询来获取具有限制的记录。 型号:

public function baleList($program_id, $per_page, $page)

    $this->db->select('gp.id, gp.lot_no, SUM(gb.weight) AS weight, SUM(gb.staple) AS staple, SUM(gb.mic) AS mic, SUM(gb.strength) AS strength, SUM(gb.trash) AS trash, gb.color_grade');
    $this->db->from(GIN_BALES . ' gb');
    $this->db->join(GIN_PROCESS . ' gp', 'gp.id=gb.process_id');
    $this->db->where('gb.sold_status', 0);
    $this->db->where('gp.ginner_id', $this->prscr_id);
    $this->db->where('gp.program', $program_id);
    $this->db->group_by('gp.id');
    $this->db->limit($per_page, $page);
    $lot_details = $this->db->get()->result();
    $lot_details = array_column($lot_details, null, "id");
    $prs_id      = "'" . implode("','", array_keys($lot_details)) . "'";
    $bales_list  = [];
    $n           = 0;
    $this->db->select('id,process_id,press_no,weight,staple,mic,strength,trash,color_grade');
    $this->db->from(GIN_BALES);
    $this->db->where('sold_status', 0);
    $this->db->where("process_id IN (" . $prs_id . ")", null, false);
    $bales = $this->db->get()->result();
    foreach ($bales as $value) 
        if (array_key_exists($value->process_id, $lot_details)) 
            $lot_details[$value->process_id]->bales[] = $value;
        
        $lot_details[$n]->bales = $bales;
        $n++;
    
    return $lot_details;

查看:

<p><?php echo $links; ?></p>

我已经在查看页面代码的表格下添加了这个链接代码。在该箭头到达第一页之后,分页一直工作到 5 个部分。所以我无法查看分页的第 6 页。是否需要在控制器或模型文件中进行任何其他更正以正确显示分页?

【问题讨论】:

【参考方案1】:

见下面的代码,

我们通过配置库配置分页链接

    $this->load->library("pagination");
    $config = array();
    $config["base_url"] = base_url() . "blogs";
    $config["total_rows"] = $this->blog->getBlogCount();
    $config["per_page"] = 10;
    $config["uri_segment"] = 2;
    $config['full_tag_open'] = "<ul class='pagination'>";
    $config['full_tag_close'] = '</ul>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';

    $config['prev_link'] = 'Previous';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';

    $config['next_link'] = 'Next';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';

    $this->pagination->initialize($config);

【讨论】:

【参考方案2】:

检查官方分页文件: https://www.codeigniter.com/user_guide/libraries/pagination.html#customizing-the-previous-link 这是样本。

        $config['base_url'] = base_url() . 'paging/custom';
        $config['total_rows'] = $total_records;
        $config['per_page'] = $limit_per_page;
        $config["uri_segment"] = 3;

        // custom paging configuration
        $config['num_links'] = 2;
        $config['use_page_numbers'] = TRUE;
        $config['reuse_query_string'] = TRUE;

        $config['full_tag_open'] = '<div class="pagination">';
        $config['full_tag_close'] = '</div>';

        $config['first_link'] = 'First Page';
        $config['first_tag_open'] = '<span class="firstlink">';
        $config['first_tag_close'] = '</span>';

        $config['last_link'] = 'Last Page';
        $config['last_tag_open'] = '<span class="lastlink">';
        $config['last_tag_close'] = '</span>';

        $config['next_link'] = 'Next Page';
        $config['next_tag_open'] = '<span class="nextlink">';
        $config['next_tag_close'] = '</span>';

        $config['prev_link'] = 'Prev Page';
        $config['prev_tag_open'] = '<span class="prevlink">';
        $config['prev_tag_close'] = '</span>';

        $config['cur_tag_open'] = '<span class="curlink">';
        $config['cur_tag_close'] = '</span>';

        $config['num_tag_open'] = '<span class="numlink">';
        $config['num_tag_close'] = '</span>';

        $this->pagination->initialize($config);

        // build paging links
        $params["links"] = $this->pagination->create_links();     

【讨论】:

以上是关于使用codeigniter中的下一个和上一个按钮进行分页的主要内容,如果未能解决你的问题,请参考以下文章

滚动库中的下一个和上一个按钮中的错误

使用php的下一个和上一个按钮[重复]

引导选项卡:表单的下一个和上一个按钮

为啥我的下一个和上一个按钮在 IOS 上看起来很奇怪?

如何为滑块的分页按钮创建简单的下一个和上一个触发按钮?

如何显示同一类别的下一个和上一个按钮?