【问题标题】:How to make a search webservice API with Codeigniter如何使用 Codeigniter 制作搜索 Web 服务 API
【发布时间】:2017-08-24 14:14:08
【问题描述】:

我过去用原生 PHP 制作了一个搜索 API,但我不知道如何在 Codeigniter 中制作它。下面的代码没有显示数据。有人能指出我正确的方向吗?

public function peta(){
    $data = array();

    // $query = "select * from tb_peta";
    $cari = isset($_REQUEST['cari']) ? $_REQUEST['cari'] : '';

    if ($cari != null) {
        $result = $this->db->query("SELECT * FROM tb_peta WHERE nama LIKE "."'%".$cari."%'");
    } else {
        $result = $this->db->query( "SELECT * FROM tb_peta");
    }

    // $q = $this->db->query($query);
    if ($q -> num_rows() >0) {
        $data ['success'] = 1;
        $data ['message'] = 'data ada';
        $data ['data']=$q->result();
    } else {
        $data['result']='0';
        $data['message'] ='kosong';
    }

    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 
        $data[] = $row;
    }

    echo json_encode(array('planet' => $json_response));
}

【问题讨论】:

    标签: php json codeigniter api


    【解决方案1】:

    利用codeigniter query builder

    public function peta(){
        $data = array();
    
        $cari = isset($_REQUEST['cari']) ? $_REQUEST['cari'] : '';
    
        if ($cari != null) {
            $this->db->like('name',$cari,'both');
            $result = $this->db->get("tb_peta");
        } else {
            $result = $this->db->get("tb_peta");
        }
    
        if ($result->num_rows() >0) {
            $data ['success'] = 1;
            $data ['message'] = 'data ada';
            $data ['data']=$result->result_array();
        } else {
            $data['result']='0';
            $data['message'] ='kosong';
        }
    
    
        echo json_encode(array('planet' => $data));
    }
    

    【讨论】:

    猜你喜欢
    • 2017-01-02
    • 2012-11-24
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    相关资源
    最近更新 更多