Codeigniter Query Help Returning JSON

John S.
John S. used Ask the Experts™
on
How do I return the results of a query as JSON echo to screen?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Marco GasiFreelancer
Top Expert 2010

Commented:
Assuming your model performs the query, it just returns the resulting set using json_encode to the controller. This one encapsulates the result into something like $data["json_result"] and pass it to the view wich then will echoes it. If you can post some code I'll probably cane give you a better help.
John S.Web Developer

Author

Commented:
Okay this is an example of the query I want to run:

SELECT * FROM (`userorderdetails`) WHERE `detail_userid` = '13' AND `detail_refnum` = '11' AND `detail_userid` = '13' OR `detail_jobnumber` = '11' AND `detail_userid` = '13' OR `detail_custref` = '11' AND `detail_userid` = '13' ORDER BY `detail_pickupdate` desc

Open in new window


and I need the results for this Jquery Autocomplete call:

$("button").click(function(){
    var searchValue=$( "#search_quotes" ).val();
    $.post("search_invoice_auto.php",
    {
        name: 'searchValue'        
    },
    function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    });
});

Open in new window


and here what my NON WORKING USERS model looks like:

	
        $id = $_SESSION['userId'];

        $this->load->database();
    	$this->db->where('detail_userid', $id);

        $search_string  = $_POST['searchValue'];  // IS THIS RIGHT? ARE WE GETTING THIS FROM AJAX?
    	
    	$this->db->where('detail_refnum', $search)->where('detail_userid', $id);
    	$this->db->or_where('detail_jobnumber', $search)->where('detail_userid', $id);
    	$this->db->or_where('detail_custref', $search)->where('detail_userid', $id);

        // NEED TO QUERY AND RETURN JSON RESULTS

Open in new window


Thank you.
Most Valuable Expert 2011
Top Expert 2016

Commented:
To this comment...
// IS THIS RIGHT? ARE WE GETTING THIS FROM AJAX?
PHP has a built-in function (and sometimes frameworks wrap this function in another function) that displays the contents of variables.  You can use var_dump($_POST) to see what you are getting from the jQuery AJAX $.post() request.

In Laravel, which seems to have supplanted CodeIgniter for all new development over the past few years, there is the handy dd() method.
Freelancer
Top Expert 2010
Commented:
The problem is here:
$.post("search_invoice_auto.php"

Open in new window

You can't in CodeIgniter call an external script this way. You have instead to call a controller method which will process tha ajax call. So in your controller you should create something like
public function search_invoice_auto(){
    $search_value = $_POST['name'];
//    call model function passing $search_value and get results
    $data['result'] = $this->model_name->do_search($search_value);
//    thewn pass data to the view
}

Open in new window


Hope this help.
For this you will have to download and install CURL library for codeigniter first. Codeigniter CURL can be downloaded from github repo and then can be loaded using following command:

$this->load->library('Curl');

Open in new window


Next, you can use Post to send JSON notation. Data can displayed in JSON using display_override hook controller.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial