I'm want build some multiple Searching function in rest-api using Codeigniter, but the problem data not show when I'm test in Postman .
I'm using this code to build Searching function :
Result I want 1 :
Select * from Mahasiswa where nrp=$keyword OR nama=$keyword
Result I want 2 :
Select * from Mahasiswa where nrp like $keyword OR nama like $keyword
Controller:
public function search_get()
{
$keyword = $this->get('nrp');
$keyword = $this->get('nama');
$msgEmpty = ['status' => false, 'message' => 'Data Not Found'];
if ($keyword === null) {
$mahasiswa = $this->mahasiswa->showMahasiswa();
} else {
$mahasiswa = $this->mahasiswa->showMahasiswa($keyword);
}
if ($mahasiswa) {
$this->set_response([
'status' => true,
'data' => $mahasiswa,
], 200);
} else {
$this->set_response($msgEmpty, 404);
}
}
Model
public function showMahasiswa($keyword = null)
{
if ($keyword === null) {
return $this->db->get('mahasiswa')->result_array();
} else {
$this->db->select('*');
$this->db->from('mahasiswa');
if($keyword){
$this->db->or_like('nrp',$keyword);
}
if($keyword){
$this->db->or_like('nama',$keyword);
}
$this->db->get();
// return $this->db->get_where('mahasiswa', ['id' => $id])->result_array();
}
}
I've already used multiple where condition like this:
Example 1 :
public function showMahasiswa($keyword = null)
{
if ($keyword === null) {
return $this->db->get('mahasiswa')->result_array();
} else {
$where = "nrp=$keyword OR nama=$keyword";
return $this->db->get_where('mahasiswa', $where)->result_array();
// return $this->db->get_where('mahasiswa', ['id' => $id])->result_array();
}
}
Example 2 :
public function showMahasiswa($keyword = null)
{
if ($keyword === null) {
return $this->db->get('mahasiswa')->result_array();
} else {
$multipleKeyword =['nrp'=> $keyword,'nama'=> $keyword];
$this->db->where($multipleKeyword);
$this->db->get('mahasiswa')->result_array();
// return $this->db->get_where('mahasiswa', ['id' => $id])->result_array();
}
}
But all nothing work.
Can you help me with this case ? And Suggest me what the best query for searching in rest-api using CodeIgniter
Thanks