Welcome Guest, Not a member yet? Register   Sign In
search a value in multiple columns
#4

(04-06-2019, 12:38 AM)jreklund Wrote: Here are some examples.

PHP Code:
<form method="post" action="<?php echo base_url(); ?>users/find">
 <
input type="text" placeholder="Search for user..." name="search">
 <
input type="submit" value="Search">
</
form>

public function 
find() {
 
$search $this->input->post('search');
 
$this->load->model('HomeModel');
 
$data['user'] = $this->HomeModel->test($search);
 
$this->load->view('found'$data);
}

public function 
test($search) {
 
// This is an absolute match
 
$q $this->db
 
->where('id',$search)
 ->
or_where('name',$search)
 ->
or_where('email',$search)
 ->
or_where('salary',$search)
 ->
or_where('address',$search)
 ->
get('users');

 
// https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-similar-data
 // If relative matches are what you are seeking.
 
$q $this->db
 
->like('id',$search)
 ->
or_like('name',$search)
 ->
or_like('email',$search)
 ->
or_like('salary',$search)
 ->
or_like('address',$search)
 ->
get('users');

 
// If you only want to see the user ones
 // Add the following above.
 
$this->db->group_by('id');

 
// This will only give you the first match
 
return $q->row_array();

 
// This will give you every match
 
$results = array();
 foreach (
$q->result_array() as $row)
 {
 
$results[] = $row;
 }

 return 
$results;

Reply


Messages In This Thread
search a value in multiple columns - by User01027 - 04-05-2019, 10:54 PM
RE: search a value in multiple columns - by Mohammad Shafi - 08-04-2021, 04:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB