Welcome Guest, Not a member yet? Register   Sign In
Codeigniter website and REST api from same models
#3

you can use a library but this is so incredibly simple with codeigniter
so here is a sample method, its getting an order based on an id and then sending it as JSON  


PHP Code:
// load your model in this case getorders
// validate the $id 
// then call something like:

function jsonOneOrder($id) {


 
       
       
if ( ! $orderdetail $this->getorders->getOne$id ) ) {

 
             // if the order does not come back go to error page
 
             $this->error_msg .= 'Error getting orders' ;
 
              $this->_showError() ; }

     // we have $orderdetail output as JSON 
 
   $this->output
    
->set_content_type('application/json')
    ->set_output(json_encode($orderdetail));

  

the other very cool thing -- $orderdetail can be an array returned from the model  -- but it also can be an object returned by the model.

so then for the receiving server -- decode JSON -- and you can use $orderdetail->name, $orderdetail->id, etc etc
Reply


Messages In This Thread
RE: Codeigniter website and REST api from same models - by cartalot - 03-11-2015, 01:11 PM



Theme © iAndrew 2016 - Forum software by © MyBB