dongyi3616
2019-08-15 03:46
浏览 115
已采纳

CodeIgniter Rest-Api:多个类似查询搜索

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhao7445 2019-08-15 04:53
    已采纳

    To get this query :

    Select * from Mahasiswa where nrp=$keyword OR nama=$keyword
    

    You could use this :

    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_where('nrp', $keyword);
                $this->db->or_where('nama', $keyword);
            }
            $this->db->get()->result_array();
        }
    }
    

    To get this query :

    Select * from Mahasiswa where nrp like $keyword OR nama like $keyword
    

    You could use this :

    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);
                $this->db->or_like('nama',$keyword);
            }
            $this->db->get()->result_array();
        }
    }
    

    And you have invalid get method.

    This :

    $keyword = $this->get('nrp');
    $keyword2 = $this->get('nama');
    

    Should be :

    $keyword = $this->input->get('nrp');
    $keyword2 = $this->input->get('nama');
    
    打赏 评论

相关推荐 更多相似问题