CodeIgniter中的自定义搜索功能和分页

时间:2018-10-23 07:10:37

标签: php codeigniter pagination codeigniter-3

因此,我创建了一个自定义表单,在其中使用GET方法以检索用户在URL中键入的值。这是我创建的表单:

<!-- Section: Form -->
<?php
    $action     =   'store/searchPosts';
    $attributes =   array(
        'method'    =>  'get',
        'class'     =>  'form-horizontal',
        'role'      =>  'form',
    );
    $hidden     =   array();
?>
<?= form_open(htmlspecialchars($action), $attributes, $hidden); ?>
    <!-- Featured -->
    <div class="form-group">
    <?php
        $label_text =   'Featured';
        $id         =   'featured';
        $attributes =   array();
    ?>
    <?= form_label($label_text, $id, $attributes); ?>
    <?php
        $name       =   'featured';
        $options    =   array(
            'yes'      =>  'Yes',
            'no'        =>  'No',
        );
        $selected   =   array('yes');
        $extra      =   array(
            'id'    =>  'featured',
            'class' =>  'form-control',
            'value' =>  set_value('featured'),
        );
    ?>
    <?= form_dropdown($name, $options, $selected, $extra); ?>
    </div>
    <!-- Free -->
    <div class="form-group">
    <?php
        $label_text =   'Free';
        $id         =   'free';
        $attributes =   array();
    ?>
    <?= form_label($label_text, $id, $attributes); ?>
    <?php
        $name       =   'free';
        $options    =   array(
            'yes'      =>  'Yes',
            'no'        =>  'No',
        );
        $selected   =   array('yes');
        $extra      =   array(
            'id'    =>  'free',
            'class' =>  'form-control',
            'value' =>  set_value('free'),
        );
    ?>
    <?= form_dropdown($name, $options, $selected, $extra); ?>
    </div>
    <!-- Title -->
    <div class="form-group">
    <?php
        $label_text =   'Title';
        $id         =   'title';
        $attributes =   array();
    ?>
    <?= form_label($label_text, $id, $attributes); ?>
    <?php
        $data   =   array(
            'id'    =>  'title',
            'name'  =>  'title',
        );
        $value  =   set_value('title');
        $extra  =   array(
            'class'         =>  'form-control',
            'placeholder'   =>  'Here goes the title',
        );
    ?>
    <?= form_input($data, $value, $extra); ?>
    </div>
    <!-- Author -->
    <div class="form-group">
    <?php
        $label_text =   'Author';
        $id         =   'user_id';
        $attributes =   array();
    ?>
    <?= form_label($label_text, $id, $attributes); ?>
    <?php
        $name       =   'user_id';
        $options    =   $this->userFilter;
        $selected   =   array('1');
        $extra      =   array(
            'id'    =>  'user_id',
            'class' =>  'form-control',
            'value' =>  set_value('user_id'),
        );
    ?>
    <?= form_dropdown($name, $options, $selected, $extra); ?>
    </div>
    <!-- Words Like -->
    <div class="form-group">
    <?php
        $label_text =   'Contains the words';
        $id         =   'keyword';
        $attributes =   array();
    ?>
    <?= form_label($label_text, $id, $attributes); ?>
    <?php
        $data   =   array(
            'id'    =>  'keyword',
            'name'  =>  'keyword',
        );
        $value  =   set_value('keyword');
        $extra  =   array(
            'class'         =>  'form-control',
            'placeholder'   =>  'Type here',
        );
    ?>
    <?= form_input($data, $value, $extra); ?>
    </div>
    <!-- Price Range -->
    <div class="form-group">
    <?php
        $label_text =   'Minimum Price';
        $id         =   'price_low';
        $attributes =   array();
    ?>
    <?= form_label($label_text, $id, $attributes); ?>
    <?php
        $data   =   array(
            'id'    =>  'price_low',
            'name'  =>  'price_low',
        );
        $value  =   set_value('price_low');
        $extra  =   array(
            'data-slider-id'    =>  'ex1Slider',
            'data-slider-min'   =>  '0',
            'data-slider-max'   =>  '1000',
            'data-slider-step'  =>  '1',
            'data-slider-value' =>  '0',
        );
    ?>
    <?= form_input($data, $value, $extra); ?>
    <?php
        $label_text =   'Maximum Price';
        $id         =   'price_high';
        $attributes =   array();
    ?>
    <?= form_label($label_text, $id, $attributes); ?>
    <?php
        $data   =   array(
            'id'    =>  'price_high',
            'name'  =>  'price_high',
        );
        $value  =   set_value('price_high');
        $extra  =   array(
            'data-slider-id'    =>  'ex1Slider',
            'data-slider-min'   =>  '0',
            'data-slider-max'   =>  '1000',
            'data-slider-step'  =>  '1',
            'data-slider-value' =>  '1000',
        );
    ?>
    <?= form_input($data, $value, $extra); ?>
    </div>
    <!-- Buttons -->
    <?php
    $data       =   array(
        'id'    =>  'mysubmit',
        'type'  =>  'submit',
    );
    $content    =   'Submit';
    $extra      =   array(
        'class' =>  'btn btn-primary',
    );
    ?>
    <?= form_button($data, $content, $extra); ?>
    <?php
    $data   =   'myreset';
    $value  =   'Reset';
    $extra  =   array(
        'class' =>  'btn btn-default',
    );
    ?>
    <?= form_reset($data, $value, $extra); ?>
<?= form_close(); ?>

现在表单进入我在商店控制器中找到的方法searchPost:

public function searchPosts(){

        // Get Inputs
        $featured       =   $this->input->get('featured');
        $free               =   $this->input->get('free');
        $price_high =   $this->input->get('price_high');
        $price_low  =   $this->input->get('price_low');
        $title          =   $this->input->get('title');
        $userID         =   $this->input->get('user_id');
        $keyword        =   $this->input->get('keyword');

        $minimum = ( int ) str_replace( '.', '', $price_low );
        $maximum = ( int ) str_replace( '.', '', $price_high );

        // Pagination
        $config['base_url'] = base_url('store/searchPosts');
        $config['total_rows'] = $this->PublicPagination_model->getStoreSearchCount($featured, $free, $minimum, $maximum, $title, $userID, $keyword);
        $config['per_page'] = 6;
        $config['uri_segment'] = 3;
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['attributes'] = array('class' => 'page-link');
        $config['first_link'] = 'First';
        $config['last_link'] = 'Last';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['prev_link'] = '&laquo';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&raquo';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="page-item active"><a href="#" class="page-link">';
        $config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        $data['posts'] = $this->PublicPagination_model->getStoreSearhCountLimit($featured, $free, $minimum, $maximum, $title, $userID, $keyword, $config["per_page"], $page);

        // Get data
        // $data['posts']   =   $this->PublicStore_model->searchQuery($featured, $free, $minimum, $maximum, $title, $userID, $keyword);

        // Get featured
        $data['featured']   =   $this->PublicStore_model->getFeaturedPosts();

        // Get comments
        $data['comments']   =   $this->PublicStore_model->getSidebarComments();

        // Get categories
        $data['categories'] =   $this->PublicStore_model->getSidebarCategories();

        // Get tags
        $data['tags']       =   $this->PublicStore_model->getSidebarTagCloud();

        // Meta data
        $data['title']  =   $this->settings->title.' | Search Results';

        // Load template
        $this->template->load('public', 'default', 'store/search', $data);

    }

现在我正在努力的是显示正确的数据并使分页在视图中可用,在这里,我将使用两种用于“获取”数据和分页的模型方法: / p>

public function getStoreSearchCount($featured, $free, $minimum, $maximum, $title, $userID, $keyword){
    $this->db->select('*, ci_posts.create_date as date, COUNT(*) as num_row');
        $this->db->from($this->posts);
        $this->db->join('ci_relationship', 'ci_relationship.post_id = ci_posts.post_id', 'INNER');
        $this->db->order_by('ci_relationship.post_id', 'DESC');
        $this->db->group_by('ci_relationship.post_id');
    $this->db->like('title', $title);
    $this->db->or_like('body', $keyword);
        $this->db->where('ci_relationship.status', $this->published);
    $this->db->where('ci_relationship.type =', $this->productType);
    $this->db->or_where('ci_relationship.featured', $featured);
    $this->db->or_where('ci_relationship.free', $free);
    $this->db->or_where('ci_relationship.price >', $minimum);
    $this->db->or_where('ci_relationship.price <', $maximum);
    $this->db->or_where('ci_relationship.user_id', $userID);

    $query = $this->db->get();
    $result = $query->result();

    return $result[0]->num_row;
  }

  public function getStoreSearhCountLimit($featured, $free, $minimum, $maximum, $title, $userID, $keyword, $limit, $start){
        $this->db->select('*, ci_posts.create_date as date, COUNT(*) as num_row');
        $this->db->from($this->posts);
        $this->db->join('ci_relationship', 'ci_relationship.post_id = ci_posts.post_id', 'INNER');
        $this->db->order_by('ci_relationship.post_id', 'DESC');
        $this->db->group_by('ci_relationship.post_id');
    $this->db->like('title', $title);
    $this->db->or_like('body', $keyword);
        $this->db->where('ci_relationship.status', $this->published);
    $this->db->where('ci_relationship.type =', $this->productType);
    $this->db->or_where('ci_relationship.featured', $featured);
    $this->db->or_where('ci_relationship.free', $free);
    $this->db->or_where('ci_relationship.price >', $minimum);
    $this->db->or_where('ci_relationship.price <', $maximum);
    $this->db->or_where('ci_relationship.user_id', $userID);

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

        return $result = $query->result();
  }

我正试图从两个数据库表中获取数据。

谢谢。

0 个答案:

没有答案