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

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加