php 如何在 Codeigniter 中为搜索创建 MVC?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8821844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:34:43  来源:igfitidea点击:

how to create MVC for search in Codeigniter?

phpcodeignitersearch

提问by jpganz18

I am very new to CodeIgniter, which has been very hard for me to learn so far. I mostly never work with frameworks and this is my first time.

我对 CodeIgniter 很陌生,到目前为止我很难学习。我基本上从不使用框架,这是我的第一次。

I understand MVC but I really don't know how to create a search, even just a basic one: I just want someone to send a word in an input and search it in my database (with Ajax or not) and give the answer back. Could anyone help me with some ideas for how I should proceed? I understand that in the view I will put my divs, inputs and more, and in the controller I will call my functions that will interact with my model. I'm struggling with how to integrate them on CI though because the view is actually filled via the controller and I believe I can't use the functions of from it in the view.

我了解 MVC,但我真的不知道如何创建搜索,即使只是一个基本的搜索:我只想有人在输入中发送一个单词并在我的数据库中搜索它(是否使用 Ajax)并给出答案. 任何人都可以帮助我提出一些关于我应该如何进行的想法吗?我知道在视图中我将放置我的 div、输入等,并且在控制器中我将调用与我的模型交互的函数。我正在努力将它们集成到 CI 中,因为视图实际上是通过控制器填充的,我相信我无法在视图中使用它的功能。

Any help please?

请问有什么帮助吗?

回答by Roel

Start by creating a controller that will be handling the search requests and displaying the search page, followed by the search-term passed to the model for database lookup (and sending it back to the controller). The controller will pass it to the view.

首先创建一个控制器,它将处理搜索请求并显示搜索页面,然后将搜索项传递给模型以进行数据库查找(并将其发送回控制器)。控制器会将它传递给视图。

A little example;

一个小例子;

The Controller

控制器

class Search extends CI_Controller {

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

        $this->load->helper('form');

        $this->load->model('search_model');
    }

    public function index()
    {
        $this->load->view('search_form');
    }

    public function execute_search()
    {
        // Retrieve the posted search term.
        $search_term = $this->input->post('search');

        // Use a model to retrieve the results.
        $data['results'] = $this->search_model->get_results($search_term);

        // Pass the results to the view.
        $this->load->view('search_results',$data);
    }

}

The Model

该模型

class Search_model extends CI_Model {

    public function get_results($search_term='default')
    {
        // Use the Active Record class for safer queries.
        $this->db->select('*');
        $this->db->from('members');
        $this->db->like('username',$search_term);

        // Execute the query.
        $query = $this->db->get();

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

}

The view for displaying the search form

显示搜索表单的视图

<?php
    echo form_open('search/execute_search');

    echo form_input(array('name'=>'search'));

    echo form_submit('search_submit','Submit');


?>

The View for displaying results

显示结果的视图

<div>
    <?php
        // List up all results.
        foreach ($results as $val)
        {
            echo $val['username'];
        }
    ?>
</div>

回答by bbnn

In CodeIgniter, most likely you will use post method instead of get. So, in the search form, make sure that you are using post method.

在 CodeIgniter 中,您很可能会使用 post 方法而不是 get。因此,在搜索表单中,请确保您使用的是 post 方法。

for example

例如

View:

看法:

<form action="<?=site_url('search_controller/search_function_in_controller')?>" method="post">
  search: <input type="text" name="keyword" />
  <input type="submit" value="Submit" />
</form> 

Controller

控制器

<?php
class Search_controller extends CI_Controller {

    public function search_function_in_controller()
    {
            $keyword = $_POST['keyword']; // you can also use $this->input->post('keyword');
            $this->load->model('Search_model');
            $data['search_result'] = $this->search_model->search_user($keyword);
            $this->load->view('search_result', $data);
    }
}
?>

回答by Muhammad Zohaib

Model

模型

<?php
class Searchmodel extends CI_Model
{

    public function __construct() {
        parent::__construct();
    }
    function search($keyword)
    {
        $this->db->like('first_name',$keyword);
        $query  =   $this->db->get('user');
        return $query->result_array();
    }
}

?>

Controller

控制器

<?php
Class Search extends CI_Controller
{

    public function __construct() {
        parent::__construct();
        $this->load->model('searchmodel');

    }


    function search_keyword()
    {
        $keyword=$this->input->post('submit');
        $data['users']=$this->searchmodel->search($keyword);

        $this->load->view('user/view', $data);

    }
}
?>

View

看法

<form class="form-inline" role="form" action="<?php echo site_url().'/search/search_keyword';?>" method="post">
    <div class="form-group">
        <input type="text" class="form-control" name="search" placeholder="Search by firstname">
    </div>
    <button type="submit" class="btn btn-info" name="submit" >Search</button>
</form>