Taufiq Hidayah
Taufiq Hidayah

Reputation: 47

How to make a search webservice API with Codeigniter

I've made a Search API with native PHP in the past, but I don't know how to make it in Codeigniter. The code below is not showing the data. Could someone point me in the right direction?

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));
}

Upvotes: 1

Views: 1569

Answers (1)

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

Make use of 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));
}

Upvotes: 1

Related Questions