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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化