codeigniter-routing和查询字符串

sg2wtvxw  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(262)

我是codeigniter的新手。我开发了一个简单的搜索表单,点击它就会显示结果。
我在表单中使用了get方法,因此在搜索时,url类似于domain/search/search\u func?keyword=test&submit=search。我想把url改成domain/search/test之类的。这里search是我的控制器,search函数是我的方法,test是get参数。我试过使用“$route['search/search_func?(:any)”]='search/search_func';”但运气不好。。有人能帮忙吗?

html格式

<form name="search_form" action="/Search/search_func">
Search: <input type="text" placeholder="Enter keywords, city, country, etc." name="keyword">
<input type="submit" name="submit" value="Search">

控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Search extends CI_Controller {
    public function index()
    {
        $this->load->view('search');
    }
    public function search_func()
    {
        $this->load->model('Search_model');
        $result['rows'] = $this->Search_model->search_result();
        $this->load->view('search_results',$result);
    }
}
?>

型号

<?php

class Search_model extends CI_Model
{
    public function search_result(){
        $this->load->database();
        $keyword  = $this->input->get('keyword'); 
        //$query = $this->db->get("employee");
        $query = $this->db->query("select * from employee where city like '%".$keyword."%' or state like'%".$keyword."%' or country like '%".$keyword."%'");
        $results = $query->result();
        return $results;
    }
}   
?>
nr7wwzry

nr7wwzry1#

<form name="search_form" action="/search/">
Search: <input type="text" placeholder="Enter keywords, city, country, etc." name="keyword">
<input type="submit" name="submit" value="Search">

$route['Search/(:any)'] = 'Search/search_func/$1';

这里搜索是您的控制器(:any)将Map到您的get参数,并将由search controller的search函数处理。

oalqel3c

oalqel3c2#

你的路线已经定了

$route['search/(:any)'] = 'search/search_func/$1';

控制器必须有一个参数。所以你的控制器功能

public function search_func($keyword)
{
$this->load->model('Search_model');
        $result['rows'] = $this->Search_model->search_result($keyword);
        $this->load->view('search_results',$result);
}

关于你的模型函数

public function search_result($keyword){
        $this->load->database();
        $query = $this->db->query("select * from employee where city like '%".$keyword."%' or state like'%".$keyword."%' or country like '%".$keyword."%'");
        $results = $query->result();
        return $results;
    }

相关问题