6

I am creating a web-service backend for a mobile app I am developing. (I am an experience Obj-C developer, not a web-designer!) Essentially I would like to use Codeigniter and Phil Sturgeon's RESTful API Server https://github.com/philsturgeon/codeigniter-restserver however, I'm having some trouble getting it all setup and working.

I have MySQL database that is setup with data in it. And I need some help writing a CodeIgniter PHP model and controller that returns JSON data of what is inside that database. All of the tutorials and forum post's i have found deal with hardcoded data in the controler, not a MySQL database. I would ideally like to have the URL formatted like this http://api.mysite.com/v1/search?id=1&name=foo&city=bar , I could potentially have 50+ parameters to pass in the url.

Using Phil's code, I have come up with this as my controller:

public function index_get()
{
    if (!$this->get('id'))
    {
        $this->response(NULL, 400);
    }

    $data = $this->grid_m->get_id($this->get('id'));
    if ($data)
    {
        $this->response($data, 200);
    }
    else
    {
        $this->response(NULL, 404);
    }
}

That only gets me one search term: id?=# .. I need to know how to get multiple search terms

Here is my Codeigniter model:

<?php

class Grid_m extends CI_Model
{

function get_all()
{
    $query = $this->db->get('grid');
    if ($query->num_rows() > 0)
    {
        return $query->result();
    }
    return FALSE;;
}

This just returns EVERYTHING in my MySQL database regardless of what id or url term I pass it in the URL.

I'm a big noob when it comes to developing my own custom API so any suggestions on how to fix my controller and database model would be a huge help!

Thanks for the help!

-brian

Brian
  • 723
  • 1
  • 8
  • 26

3 Answers3

1

This is old question but if somebody go here and still need help, try these code:

In your controller:

public function index_get()  
{  
    $where = '';  

    if ($this->get('id'))  // search by id when id passed by
    {              
        $where .= 'id = '.$this->get('id');  
    }  

    if ($this->get('name'))  // add search by name when name passed by        
    {  
        $where .= (empty($where)? '' : ' or ')."name like '%".$this->get('name')."%'";  
    }  

    if ($this->get('city'))  // add search by city when city passed by  
    {  
        $where .= (empty($where)? '' : ' or ')."city like '%".$this->get('city')."%'";  
    }  

    // you can add as many as search terms

   if ( ! empty($where))  
   {  
       $data = $this->grid_m->get_searched($where);  

       if ($data)  
       {  
           $this->response($data, 200);  
       }  
       else  
       {  
           $this->response(NULL, 404);  
       }  
    }  
    else  
    {  
        $this->response(NULL, 404);  
     }  
}

In your model, create get_searched function:

class Grid_m extends CI_Model  
{  
   function get_searched($where = NULL)  
   {  
      if ( ! is_null($where))  
      {  
          $this->db->where($where);  
          $query = $this->db->get('grid');  

          if ($query->num_rows() > 0)  
          {  
              return $query->result();  
          }  
       }  
       return FALSE;  
    }  
}  
Arif RH
  • 155
  • 8
0

User Codeigniter's Active record to build proper query, you can build any type of query using methods of active record, refer following example I have just added one condition in it you can add more conditions as per your need.

 <?php
class Grid_m extends CI_Model
{    
  function get_all()
  {
     $this->db->select('col1, col2, col3')->where('id', 5);
     $query = $this->db->get('grid');

    if ($query->num_rows() > 0)
    {
        return $query->result();
    }
    return FALSE;;
  }

}
Pankaj Khairnar
  • 3,028
  • 3
  • 25
  • 34
  • but he is doesn't know other ways to query – Pankaj Khairnar Nov 25 '12 at 08:01
  • This is great and helped me accomplish one part of my problems, but how might I pass more parameters from my URL? ie: search?type=Restaurant&location=San%20Francicso – Brian Nov 25 '12 at 22:34
  • Codeigniter has default behavior of handling urls, you have to form your url like http://example.com/index.php/controller_name/method_name/prameter1/parameter2/parameter... if you don't want to use this behavior by enabling get parameters refer the url http://userguides.ellislab.com/codeigniter/general/urls.html – Pankaj Khairnar Nov 26 '12 at 02:06
0

Please check you query

$query = $this->db->get_where('grid', array('id' => $id));
Saleem
  • 1,059
  • 14
  • 27