php - Codeigniter - 使用查询字符串进行路由

标签 php mysql codeigniter

我是 Codeigniter 的新手。我开发了一个简单的搜索表单,单击它就会显示结果。

我在表单中使用了 get 方法,因此当我搜索时,URL 类似于domain/Search/search_func?keyword=test&submit=Search。我想将 URL 更改为类似domain/Search/test 的内容。 这里 Search 是我的 Controller ,search_func 是我的方法,test 是获取参数。 我尝试过使用 `$route['搜索/search_func?(:any)'] = '搜索/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">

Controller

    <?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;
    }
}   
?>

最佳答案

<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';

此处的搜索是您的 Controller (:any),将映射到您的 get 参数,并将由搜索 Controller 的 search_func 处理。

关于php - Codeigniter - 使用查询字符串进行路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52514596/

相关文章:

php - laravel 5 collection pull 方法不打印任何内容

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

PHP strip 标签不工作

mysql - 简单的 MySQL 重复删除

php - 监听 mySQL 数据库中的更改(使用 PHP)

php - 在 Codeigniter 中上传多个文件

php - 代码点火器 3 : Can't catch database error using try catch block

mysql - 在 Yii 中连接 2 个数据库以同时处理测试数据和真实数据

javascript - 使用javascript中的ckeditor获取存储在db中的html标签的数据

codeigniter - 限制 codeigniter 管理面板的 ip 地址