search a value in multiple columns - User01027 - 04-05-2019
Hi, Sorry if it is very basic question but I have been struggling with it. I am able to find a user by it's id, but I want to search user by id,name,email,salary or address. So when there is input in search form, my code should search that input value in all columns of table and show result if there is any match. Here is my code which works for search with id.
form in view :
<form method="post" action="<?php echo base_url(); ?>users/find">
<input type="text" placeholder="ID" name="id">
<input type="submit" value="Search">
</form>
method in controller users:
public function find() {
$fd = $this->input->post();
$this->load->model('HomeModel');
$data['user'] = $this->HomeModel->test($fd);
$this->load->view('found', $data);
}
method in model:
public function test($id) {
$query = $this->db->get_where('users',$id);
return $query->row_array();
}
view to display data :
<?php extract($user); ?>
in table .....
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $age; ?></td>
<td><?php echo $salary; ?> </td>
<td><?php echo $address; ?> </td>
Thanks.
RE: search a value in multiple columns - jreklund - 04-06-2019
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; }
RE: search a value in multiple columns - User01027 - 04-06-2019
It worked. Thank you very much jreklund.
RE: search a value in multiple columns - Mohammad Shafi - 08-04-2021
(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; }
RE: search a value in multiple columns - Mohammad Shafi - 08-04-2021
Thank you very much appreciate alot.
RE: search a value in multiple columns - anuragk - 07-23-2024
(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; }
Thankyou
This was helpful
|