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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?