如果选择选项选择所有类别,则显示类别中的所有页面

时间:2015-05-06 11:37:34

标签: codeigniter

我正在使用codeigniter创建一个搜索表单,并在我的 $ categories 的选择表单上创建一个名为搜索所有类别的选择选项

我想知道当我点击选择选项搜索所有类别然后按提交它将显示所有页面时,如何在我的选择表单上这样做。

我可以通过$category_id = $this->input->post('category_search');模型函数获取类别ID

Currenly我一次只能选择一个类别并按提交,只会显示该类别的页面。

单一类别搜索的模型功能:

public function get_category_page_search_results() {

    // This input gets the category id selected.
    $category_id = $this->input->post('category_search'); 

    $this->db->from('page_to_category p2c', 'LEFT');
    $this->db->join('page p', 'p.page_id = p2c.page_id', 'LEFT');
    $this->db->join('page_description pd', 'pd.page_id = p2c.page_id', 'LEFT');
    $this->db->where('p2c.category_id', $category_id);
    $query = $this->db->get();
    return $query->result_array();
}

搜索控制器

<?php

class Search extends MX_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $data['title'] = "Search";

        $search_filter = $this->input->get_post('search_filter');

        if (empty($search_filter)) {
            $data['search_criteria'] = "Search";
        } else {
            $data['search_criteria'] = "Search" .'-'. $this->input->get_post('search_filter');
        }

        $data['searched'] = $this->input->get_post('search_filter');

        $this->load->model('admin/tool/model_tool_image');

        $data['categories'] = array();
        $data['pages'] = array();

        $category_results = $this->get_all_categories_for_search();

        foreach ($category_results as $result) {
            $data['categories'][] = array(
                'category_id' => $result['category_id'],
                'name' => $result['name']
            );
        }

        if(!empty($_POST['input-search-category'])) {

            $page_results = $this->get_category_page_search_results();

            foreach ($page_results as $page) {
                $data['pages'][] = array(
                    'page_id' => $page['page_id'],
                    'image' => $this->model_tool_image->resize($page['image'], 100, 100),
                    'name' => $page['name'],
                    'description' => $page['description']
                );
            }
        }

        if (!empty($_POST['search'])) {

            $results2 = $this->get_search_results();

            foreach ($results2 as $result) {
                $data['pages'][] = array(
                    'page_id' => $result['page_id'],
                    'image' => $this->model_tool_image->resize($result['image'], 100, 100),
                    'name' => $result['name'],
                    'description' => $result['description']
                );
            }
        }       

        $this->form_validation->set_rules('search_filter', '');
        $this->form_validation->set_rules('category_search', '');

        if ($this->form_validation->run() == FALSE) {

            $data['column_left'] = Modules::run('catalog/common/column_left/index');
            $data['column_right'] = Modules::run('catalog/common/column_right/index');
            $data['content_top'] = Modules::run('catalog/common/content_top/index');
            $data['content_bottom'] = Modules::run('catalog/common/content_bottom/index');
            $data['footer'] = Modules::run('catalog/common/footer/index');
            $data['header'] = Modules::run('catalog/common/header/index');  
            $this->load->view('theme/default/template/pages/search_view', $data);

        } else {

            $data['column_left'] = Modules::run('catalog/common/column_left/index');
            $data['column_right'] = Modules::run('catalog/common/column_right/index');
            $data['content_top'] = Modules::run('catalog/common/content_top/index');
            $data['content_bottom'] = Modules::run('catalog/common/content_bottom/index');
            $data['footer'] = Modules::run('catalog/common/footer/index');
            $data['header'] = Modules::run('catalog/common/header/index');  
            $this->load->view('theme/default/template/pages/search_view', $data);
        }
    }

    // @To do create search Model

    public function get_search_results() {
        $match = $this->input->post('search_filter');
        $this->db->from('page', 'LEFT');
        $this->db->join('page_description', 'page_description.page_id = page.page_id');
        $this->db->like('name', $match);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function get_all_categories_for_search() {
        $this->db->from('category', 'LEFT');
        $this->db->join('category_description', 'category_description.category_id = category.category_id', 'LEFT');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function get_category_page_search_results() {
        $category_id = $this->input->post('category_search');
        $this->db->from('page_to_category p2c', 'LEFT');
        $this->db->join('page p', 'p.page_id = p2c.page_id', 'LEFT');
        $this->db->join('page_description pd', 'pd.page_id = p2c.page_id', 'LEFT');
        $this->db->where('p2c.category_id', $category_id);
        $query = $this->db->get();
        return $query->result_array();
    }
}

搜索视图

<?php echo $header; ?>
<div class="container">
    <div class="row">
        <?php echo $column_left; ?>
        <?php if ($column_left && $column_right) { ?>
        <?php $class = 'col-sm-6'; ?>
        <?php } elseif ($column_left || $column_right) { ?>
        <?php $class = 'col-sm-9'; ?>
        <?php } else { ?>
        <?php $class = 'col-sm-12'; ?>
        <?php } ?>
        <div id="content" class="<?php echo $class; ?>">
            <?php echo $content_top; ?>
            <form action="<?php echo base_url('pages/search');?>" method="post" role="search" enctype="multipart/form-data">
            <div class="row">
                <div class="col-sm-12">
                    <h1><?php echo $search_criteria;?></h1>
                    <hr>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label>Search For Page</label>
                        <input type="text" name="search_filter" placeholder="Search For Page" value="" class="form-control"/>
                    </div>
                    <div class="form-group">
                        <input type="submit" name="search" value="Search" role="button" class="btn btn-primary"/>
                    </div>                  
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label>Search By Category</label>
                        <select name="category_search" class="form-control">
                            <?php if ($categories) { ?>
                            <option value="">Search All Categories</option>
                            <?php foreach($categories as $category) {?>
                                <option value="<?php echo $category['category_id'];?>"><?php echo $category['name'];?></option>
                            <?php }?>
                            <?php } else { ?>
                                <option>No Categories Created</option>
                            <?php }?>
                        </select>
                    </div>
                    <div class="form-group">
                    <input type="submit" name="input-search-category" value="Category Search" role="button" class="btn btn-primary"/>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-12">
                    <hr>
                </div>
            </div>
            </form>
            <div class="row">
                <?php if ($pages) {?>
                <?php foreach ($pages as $page) {?>
                    <div class="col-sm-6 col-md-4">
                        <div class="thumbnail">
                            <a href=""><img src="<?php echo $page['image'];?>"></a>
                            <div class="caption">
                            <h3><?php echo $page['name']; ?></h3>
                            <p><?php echo $page['description']; ?></p>
                            <p>
                            <a href="" class="btn btn-primary" role="button">Read More</a>      
                            </p>
                            </div>
                        </div>
                    </div>
                <?php }?>
                <?php } else { ?>
                <div class="col-sm-12">
                    <div class="jumbotron">
                          <h2>Pages meeting the search criteria</h2>
                          <p>You have not entered the search fieild yet or there was no result found</p>
                    </div>
                </div>
                <?php }?>
            </div>
            <?php echo $content_bottom; ?>
        </div>
        <?php echo $column_right; ?>
    </div>
</div>
<?php echo $footer; ?>

快照

enter image description here

1 个答案:

答案 0 :(得分:1)

if($category_id > 0){
    $this->db->where('p2c.category_id', $category_id);
}

不应该这样做吗?