如何使用Codeigniter去除URL中未使用的$ _GET参数?

时间:2016-10-07 17:38:05

标签: php codeigniter

我正在使用Codeigniter设计搜索功能和一些数据。

我的搜索模型如下:

class Search_Model extends CI_Model {

        public function get_results($search_terms = false) {

            // Build query
            foreach ($search_terms as $field => $value) {
                $this->db->like("{$field}", $value);
            }

            // Execute query
            $query = $this->db->get('exams');

            // Return results
            return $query->result_array();
        }
}

搜索控制器方法如下所示:

public function results() {

            $search_terms = array(
                'first_name' => $this->input->get('first_name', TRUE),
                'last_name' => $this->input->get('last_name', TRUE),
                'exam_name' => $this->input->get("exam_name", TRUE)
            );

            $data['title'] = "Search Results";
            $data['exams'] = $this->search_model->get_results($search_terms);

            $this->load->view('templates/header.php', $data);
            $this->load->view('exams/index.php', $data);
            $this->load->view('templates/footer.php');
}

搜索工作正常,但如果我只搜索"名字":

我想要清理这样的网址
search/results?first_name=tim&last_name=&exam_name=

如何删除URL中未使用的额外参数?

4 个答案:

答案 0 :(得分:4)

最快的方法是在将$ searh_terms数组传递给模型之前对其进行过滤。

$search_terms = array(
    'first_name' => $this->input->get('first_name', TRUE),
    'last_name' => $this->input->get('last_name', TRUE),
    'exam_name' => $this->input->get("exam_name", TRUE)
);

// Remove empty search values
foreach ($search_terms as $key => $value) {
    if (strlen($value) == 0) {
         unset($search_terms[$key]);
    }
}

答案 1 :(得分:0)

在标题中包含jquery。然后添加这个脚本。

<script>
$(document).ready(function(){
    $('#myform').submit(function(event){
        event.preventDefault();
        var url = $(this).attr('action')+'?';
        var first = false;
        var second = false;
        if($('input[name="first_name"]').val()){
            url += 'first_name='+$('input[name="first_name"]').val();
            first = true;
        }
        if($('input[name="last_name"]').val()){
            if(first){
                url += '&';
            }
            url += 'last_name='+$('input[name="last_name"]').val();
            second = true;
        }
        if($('input[name="exam_name"]').val()){
            if(second){
                url += '&';
            }
            url += 'exam_name='+$('input[name="exam_name"]').val();
        }
        window.location.replace(url);
    });
});
</script>

(我还没有运行这个脚本,所以如果你遇到任何语法错误或其他问题,请告诉我。) 最后将结果函数更改为,

public function results() {

            $search_terms = array();

            if(isset($_GET['first_name'])){
                $search_terms['first_name'] = $this->input->get('first_name', TRUE);
            }
            if(isset($_GET['last_name'])){
                $search_terms['last_name'] = $this->input->get('last_name', TRUE);
            }
            if(isset($_GET['exam_name'])){
                $search_terms['exam_name'] = $this->input->get('exam_name', TRUE);
            }

            $data['title'] = "Search Results";
            $data['exams'] = $this->search_model->get_results($search_terms);

            $this->load->view('templates/header.php', $data);
            $this->load->view('exams/index.php', $data);
            $this->load->view('templates/footer.php');
}

答案 2 :(得分:-1)

你可以试试这个:

$mode = array();
    if(isset($_GET['first_name'])):
        $mode[] = 'first_name='.$_GET['first_name']; 
    endif; 

    if(isset($_GET['last_name'])):
        $mode[] = 'last_name='.$_GET['last_name'];
    endif; 

    if(isset($_GET['exam_name'])):    
        $mode[] = 'exam_name='.$_GET['exam_name'];
    endif;

    //etc...

    //Then reformulate link 
    if(!empty($mode)){
        $link = '?' . implode('&',$mode);
    }else{
        $link = '';
    }

  redirect("form.php".$link);

答案 3 :(得分:-1)

只过滤您发送的内容......

foreach ($search_terms as $field => $value) {
            if ($value <> ""){ //or the test you like to use...
                $this->db->like("{$field}", $value);
            }
        }