CodeIgniter

时间:2015-06-04 14:36:38

标签: php codeigniter filter pagination

我是CodeIgniter的新手,但我想问一下分页和过滤。 我做了它们并且彼此之间的工作很好,但我有两个页面也有过滤器和分页。它们给了我一些错误。 - 当我点击过滤器时,网址如下: company / components / all / names_of_filters 所以过滤器在uri段(4)工作。 ================================================== =======================但是当我加载分页时它会显示给我 公司/组件/全部/分页,因此过滤器会因分页而崩溃。我宣布uri segment(5)加载了分页,但同样的事情。这个想法是过滤器正在从数据库变化,它是一个动态的部分。我怎样才能宣布它 $ conf [base_url] =(company / components / all /...../)并在这些破折号中添加一个连接更改名称过滤器的函数的函数?! -------------------------------------------------- -------------------------------------------------- ----- *有什么方法吗? * 最佳:)

2 个答案:

答案 0 :(得分:0)

放这个文件 帮助文件夹中的myhelper_helper.php

function configPagination($url,$total_rows,$segment,$per_page=10)
    {


        $CI = get_instance();
        $CI->load->library('pagination');
        $config['base_url']         = site_url($url);
        $config['total_rows']       = $total_rows;
        $config['per_page']         = $per_page;
        $config['uri_segment']      = $segment;
        $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['num_links']        = 3;
        $config['next_tag_open']    = "<li>";
        $config['next_tag_close']   = "</li>";
        $config['prev_tag_open']    = "<li>";
        $config['prev_tag_close']   = "</li>";

        $config['first_link']   = FALSE;
        $config['last_link']    = FALSE;
        $CI->pagination->initialize($config);

        return $CI->pagination->create_links();
    }

使用会话库进行分页的示例... 控制器: Users.php

public function __construct()

    {

        parent::__construct();

        $this->per_page = 20; // How many records per page...

        $this->load->helper('myhelper');
        $this->load->library('session');
        $this->load->model('users_model');


    }

function list($start=0)
{
if(isset($_POST['filter_status']))

{

$this->session->set_userdata('filter_status',$this->input->post('filter_status'));

}

$data['users'] =  $this->users_model->get_all($start,$this->per_page);
$total          = $this->users_model->count_all();
$data['pages']  = configPagination('users/list',$total,5,$this->per_page);
$data['start']    = $start;

$this->load->view('users_view',$data);


}

最后在您的模型Users_model.php

function __construct()
    {
        parent::__construct();
        $this->load->database();
        }



    function get_all($start,$limit) {

            if($this->session->userdata('filter_status')!='')
            {
                $this->db->where('status',$this->session->userdata('filter_status'));
            }

    $query = $this->db->get('users', $limit, $start);

            return $query;

function count_all()

        {
                if($this->session->userdata('filter_status')!='')
        {
            $this->db->where('status',$this->session->userdata('filter_status'));
        }


        $query = $this->db->get('users');
        return $query->num_rows();
        }

在您看来,这样的事情

    <form action="" method="post" id="filter_form">
    <div class="form-group">
     <label>Status: </label>
     <div class="controls">
    <?php $status = array(0=>"Inactive", 1=>"Inactive",-1=>"Banned");?>
    <select id="status-select" name="filter_status" class="form-contro">
    <option value="">All</option>
            <?php foreach ($status as $key=>$value) { 
            $sel = ($key==$this->session->userdata('filter_status'))?'selected="selected"':'';
                      ?>
          <option value="<?php echo $key;?>" <?php echo $sel;?>><?php echo $value ?></option>
                    <?php } ?>
                </select>
<input type="submit" Value="Filter" />
                 </div></form>

答案 1 :(得分:0)

控制器

    $this->load->library('pagination');
    $pagination_config['base_url'] = base_url('/webs/webs1/all/');
    $pagination_config['total_rows'] = $this->model_m->count_all();
    $pagination_config['per_page'] = 2;
    $pagination_offset = $this->uri->segment(5);
    $this->pagination->initialize($pagination_config);
    $data['pagination_links'] = $this->pagination->create_links();
    //end

    //fetch categories from the database
    $data['categories'] = $this->model_m->get_categories();     
    $categoryID = $this->uri->segment(4);
    if($categoryID == null){
        redirect('backend/subcategories/all/filterAll');
    }
    if(!isset($categoryID) || ( $categoryID == "filterAll" )) {
        $data['subcategories'] = $this->model_m->get_all_subcategories();
    } else {
        $data['subcategories'] = $this->model_m->get_by_category($categoryID);
    }
    $data['selected_category'] = $categoryID;

    $data['view'] = $this->load->view($view,$data,true);
    $this->load->view($viewlayout,$data);

<强> MODEL

public function get_by_category($id){
    $query = $this->db->get_where('items', array('prod_id' => $id));
    if ($query->num_rows() > 0) { return $query->result(); }
    return false;
}

public function get_all_subcategories($catID = NULL,$limit = NULL ,$offset = NULL ) {
    if(isset($catID) && is_int($catID)) {
        if(isset($limit) && isset($offset)){
            $result = $this->db->get_where('items',array('prod_id' => $catID),$limit,$offset);
            return $result->result();
        }
        $result = $this->db->get_where('items',array('prod_id' => $catID));
        return $result->result();
    }

    if(isset($limit) && isset($offset)){
        $result = $this->db->get('items',$limit,$offset);
        return $result->result();
    }
    $result = $this->db->get('items');
    return $result->result();
}

public function count_all() {
    return $this->db->get('items')->num_rows();
}