How to create MVC for codeigniter search?

I am very new to CodeIgniter, which is very difficult for me to recognize so far. Mostly I never work with frameworks, and this is my first time.

I understand MVC, but I really don’t know how to create a search, even simple one: I just want someone to send a word to the input and search it in my database (with Ajax or not) and give an answer. Can someone help me with some ideas on how I should act? I understand that in the view I put my divs, tabs and much more, and in the controller I will call my functions that will interact with my model. I struggle with how to integrate them into CI, although due to the fact that the view is actually populated through the controller, and I believe that I can not use the functions from it in the view.

Any help please?

+4
source share
3 answers

Start by creating a controller that will handle the search queries and display the search page, followed by the search term passed to the model to search for the database (and send it back to the controller). The controller will pass it to the view.

A small example:

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

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(); } } 

View to display the search form

 <?php echo form_open('search/execute_search'); echo form_input(array('name'=>'search')); echo form_submit('search_submit','Submit'); ?> 

View to display results

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

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

eg

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); } } ?> 
0
source

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> 
0
source

Source: https://habr.com/ru/post/1390451/


All Articles