We help IT Professionals succeed at work.

Codeigniter Query Help Returning JSON

John S.
John S. asked
on
609 Views
Last Modified: 2018-10-17
How do I return the results of a query as JSON echo to screen?
Comment
Watch Question

Marco GasiFreelancer
CERTIFIED EXPERT
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
Author of the Year 2014

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
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.