Codeigniter Query Help Returning JSON

How do I return the results of a query as JSON echo to screen?
John S.Web DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
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 DeveloperAuthor 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:

    var searchValue=$( "#search_quotes" ).val();
        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->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);


Open in new window

Thank you.
Ray PaseurCommented:
To this comment...
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.
Marco GasiFreelancerCommented:
The problem is here:

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Oliver RussellCommented:
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:


Open in new window

Next, you can use Post to send JSON notation. Data can displayed in JSON using display_override hook controller.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.