Codeigniter - 如何将选定的页码传递给url并相应地显示

时间:2015-10-14 17:39:28

标签: php codeigniter pagination codeigniter-2

我第一次在codeigniter玩分页,所以遇到一些困难需要帮助或指导。

这是我正在使用的代码,它工作正常,但得到这样的分页编号:

Page 1: Getting 2 number in url (http://localhost/projectname/campaigns/2)
Page 2: Getting 6 number in url (http://localhost/projectname/campaigns/6)

我不想像上面这样的行为传递数字,但是希望这样:

Page 1: Set 1 number in url (http://localhost/projectname/campaigns/1)
Page 2: Set 2 number in url (http://localhost/projectname/campaigns/2)

知道如何处理codeigniter框架吗?

分页代码:

campaigns_list.php(控制器)

public function __construct(){
    parent::__construct();
    $this->load->model('Insta_campaigns_list_model');
    $this->load->library('pagination');
}

public function index($offset=0){
    IsNotLoggedIn(); // Check if a user is not logged in

    // For SEO
    $data['meta_title'] = 'Instagrammer Campaigns | YourCreatrs';
    $data['meta_descripton'] = '';
    $data['meta_keywords'] = '';

    // Pagination code for campaigns code start
    $config['total_rows'] = $this->Insta_campaigns_list_model->TotalCampaigns();

    // Check status value is number
    $status = $this->uri->segment(3);
    if(!ctype_digit($status)){
        $status = "1";
    }

    $config['base_url'] = base_url()."instagrammer/campaigns/".$status;
    $config['per_page'] = 3;
    $config['uri_segment'] = '4';

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

    $config['first_link'] = '« First';
    $config['first_tag_open'] = '<li class="prev page">';
    $config['first_tag_close'] = '</li>';

    $config['last_link'] = 'Last »';
    $config['last_tag_open'] = '<li class="next page">';
    $config['last_tag_close'] = '</li>';

    $config['next_link'] = 'Next →';
    $config['next_tag_open'] = '<li class="next page">';
    $config['next_tag_close'] = '</li>';

    $config['prev_link'] = '← Previous';
    $config['prev_tag_open'] = '<li class="prev page">';
    $config['prev_tag_close'] = '</li>';

    $config['cur_tag_open'] = '<li class="active"><a href="">';
    $config['cur_tag_close'] = '</a></li>';

    $config['num_tag_open'] = '<li class="page">';
    $config['num_tag_close'] = '</li>';

    $this->pagination->initialize($config);
    $query = $this->Insta_campaigns_list_model->GetCampaigns(3, $this->uri->segment(4));

    $data['Campaigns'] = null;

    if($query){
        $data['Campaigns'] =  $query;
    }
    // Pagination code for campaigns code end

    $this->load->view('include/instagrammer_header', $data);
    $this->load->view('instagrammer/campaigns_list', $data);
    $this->load->view('include/instagrammer_footer');
}

insta_campaigns_list_model.php(型号)

function GetCampaigns($limit=null, $offset=NULL){

    $AllUserData = $this->session->all_userdata();
    if(isset($AllUserData['LoginSession']['id']) && !empty($AllUserData['LoginSession']['id'])){
        $UserId = $AllUserData['LoginSession']['id'];
    }else{
        $UserId = "0";
    }

    if(isset($AllUserData['LoginSession']['UserType']) && !empty($AllUserData['LoginSession']['UserType'])){
        $UserType = $AllUserData['LoginSession']['UserType'];
    }else{
        $UserType = "";
    }

    // Check status value is number
    $status = $this->uri->segment(3);
    if(!ctype_digit($status)){
        $status = "2";
    }

    if($status == "2"){
        // Invites records
        $this->db->select('campaign.*, invite_instagrammer.campaign_id as campaign_ids_from_invite, invite_instagrammer.invite_id');
        $this->db->from('invite_instagrammer');
        $this->db->join('campaign', 'invite_instagrammer.campaign_id = campaign.campaign_id', 'left');
        $where = "instagrammer_id =$UserId AND invite_instagrammer.is_accepted =0";
        $this->db->where($where);
        $this->db->order_by("invite_instagrammer.campaign_id", "desc");
        $this->db->limit($limit, $offset);
        $query = $this->db->get();

        return $query->result();
    }else if($status == "1"){
        // Active records
        $this->db->select('campaign.*, invite_instagrammer.campaign_id as campaign_ids_from_invite, invite_instagrammer.invite_id');
        $this->db->from('invite_instagrammer');
        $this->db->join('campaign', 'invite_instagrammer.campaign_id = campaign.campaign_id', 'left');
        $where = "instagrammer_id =$UserId AND invite_instagrammer.is_accepted =1 AND campaign.end_date >= CURDATE() AND campaign.status =1";
        $this->db->where($where);
        $this->db->order_by("invite_instagrammer.campaign_id", "desc");
        $this->db->limit($limit, $offset);
        $query = $this->db->get();

        return $query->result();
    }else{
        // Closed records
        $this->db->select("*");
        $this->db->from('campaign');
        $where = "end_date < CURDATE() AND start_date != '0000-00-00 00:00:00' AND status =1";
        $this->db->where($where);
        $this->db->order_by("campaign_id", "desc");
        $this->db->limit($limit, $offset);
        $query = $this->db->get();

        return $query->result();
    }

}

function TotalCampaigns(){

    $AllUserData = $this->session->all_userdata();
    if(isset($AllUserData['LoginSession']['id']) && !empty($AllUserData['LoginSession']['id'])){
        $UserId = $AllUserData['LoginSession']['id'];
    }else{
        $UserId = "0";
    }

    if(isset($AllUserData['LoginSession']['UserType']) && !empty($AllUserData['LoginSession']['UserType'])){
        $UserType = $AllUserData['LoginSession']['UserType'];
    }else{
        $UserType = "";
    }

    // Check status value is number
    $status = $this->uri->segment(3);
    if(!ctype_digit($status)){
        $status = "2";
    }

    if($status == "2"){
        // Invites records
        $this->db->select('campaign.*, invite_instagrammer.campaign_id as campaign_ids_from_invite, invite_instagrammer.invite_id');
        $this->db->from('invite_instagrammer');
        $this->db->join('campaign', 'invite_instagrammer.campaign_id = campaign.campaign_id', 'left');
        $where = "instagrammer_id =$UserId AND invite_instagrammer.is_accepted =0";
        $this->db->where($where);

        return $this->db->count_all_results();
    }else if($status == "1"){
        // Active records
        $this->db->select('campaign.*, invite_instagrammer.campaign_id as campaign_ids_from_invite, invite_instagrammer.invite_id');
        $this->db->from('invite_instagrammer');
        $this->db->join('campaign', 'invite_instagrammer.campaign_id = campaign.campaign_id', 'left');
        $where = "instagrammer_id =$UserId AND invite_instagrammer.is_accepted =1 AND campaign.end_date >= CURDATE() AND campaign.status =1";
        $this->db->where($where);

        return $this->db->count_all_results();
    }else{
        // Closed records
        $this->db->select('*');
        $this->db->from('campaign');
        $where = "end_date < CURDATE() AND start_date != '0000-00-00 00:00:00' AND status =1";
        $this->db->where($where);

        return $this->db->count_all_results();
    }

}

campaigns.php(视图)

<?php
if(is_array($Campaigns) && count($Campaigns)):
    for($i=0; $i<count($Campaigns); $i++):
?>
<div class="col-md-4">
    <div  class="campaign-details">
        <div class="campaign-top-img">
            <?php if(file_exists($FileLocation)): ?>
                <img src="<?php echo base_url();?>public/images/campaign-images/thumbs/<?php echo $CampaignImage; ?>" alt="<?php echo $CampaignName; ?>">
            <?php else: ?>
                <img src="<?php echo base_url();?>public/images/no-image.png" alt="">
            <?php endif; ?>
            <div class="campaign-details-inner">
                <h3><?php echo $CampaignName; ?></h3>
                <small>27/09/2015</small> <span>$<?php echo $BudgetSpend; ?></span>
            </div>
        </div>
    </div>
</div>
<?php
    endfor;
else:
    ?>
    <div class="container-fluid no-results">
        <div class="row">
            <div class="col-md-12">
                <h2>Nothing Found</h2>
                <br />
                <span>Sorry, but nothing matched your search criteria. Please try again with some different<br /> keyword. </span>
            </div>
        </div>
    </div>
    <?php
endif;
?>
<?php if(count($Campaigns) != "0"): ?>
<div class="navigation-bar">
    <?php echo $this->pagination->create_links(); ?>
</div>
<?php endif; ?>

1 个答案:

答案 0 :(得分:0)

在控制器文件中只需添加:

$config['use_page_numbers'] = TRUE;

您将获得如下网址:

http://localhost/projectname/campaigns/2
http://localhost/projectname/campaigns/3