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

时间:2019-06-27 11:12:47

标签: php mysql codeigniter pagination

我试图用以下代码在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页。是否需要对控制器或模型文件进行其他更正以正确显示分页?

2 个答案:

答案 0 :(得分:0)

请参见下面的代码

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

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

答案 1 :(得分:0)

请检查官方分页文件:  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();