Search in CodeIgniter
application/config/database.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'flowers',
'dbdriver' => 'mysqli',
................................................
/application/config/autoload.php
$autoload['libraries'] = array('database‘,’table’);
$autoload['helper‘] = array('url', 'file','form','html');
/application/config/routes.php
$route['default_controller'] = 'FlowerController';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
/application/models/FlowerModel.php
<?php
class FlowerModel extends CI_Model {
function searchFlower($search){
$this->db->like('nume', $search);
$this->db->or_like('culoare', $search);
$query = $this->db->get('flori');
return $query->result_array();
}
}
/application/controllers/FlowerController.php
<?php
class FlowerController extends CI_Controller {
function __Construct(){
parent::__Construct ();
$this->load->model('FlowerModel','f'); // load model
}
public function index() {
$this->load->view('search_form');
}
public function search(){
$search=$this->input->post('search');
$row=$this->f->searchFlower($search);
$data['r']=$row;
$this->load->view('search_result',$data);
}
}
/application/views/search_form.php
<?php
echo form_open('FlowerController/search');
$search = ['name' => 'search',
'id' => 'text1',
'value'=>'',
'maxlength' => '100',
'size' => '30',
];
?>
<?php echo form_label('Search ', 'text1');?>
<?php echo form_input($search);?>
<br/><br/>
<?php echo form_submit('submit', 'Search');?>
/application/views/search_result.php
<?php
//$this->load->library('table');
$this->table->set_heading(array('Id','Nume',
'Culoare', 'Marime', 'Pret'));
foreach($r as $row ){
$this->table->add_row($row);
}
echo $this->table->generate();

21. CodeIgniter search