Reputation: 363
I want to take the returning JSON object and display what the PHP query returned, but when I use brackets to access the indices value it displays only one character at a time. Basically I want each index to contain each row from the query. This particular query has two results (the id and file name). I would like to get both the id and name into one index of the JSON array. Output displayed below:
7 5.jpg
8 7-mini.jpg
Here is my code...
<script type="text/javascript">
$(function(){
$.ajax({
url: '<?php echo base_url().'jqueryDB/jquery';?>',
type:'POST',
dataType: 'json',
success: function(output_string){
document.write(output_string[0]);
}
});
});
Right now this display '7', instead of '7 5.jpg'. How can I get it to store each row into one index?
Here's my PHP file:
$userID = $this->session->userdata('id');
$query = $this->db->query('SELECT * FROM upload WHERE userID = '.$userID);
$num_rows = $query->num_rows();
$val = $query->row_array();
$output_string = "";
foreach ($query->result() as $row){
$output_string .= $row->id;
$output_string .= " ".$row->name."<br />";
}
echo json_encode($output_string);
Upvotes: 5
Views: 36289
Reputation: 24661
First, you want to store the results of your query into an array, not a string. The results of calling json_encode
will take that array and turn it into a JSON-encoded string.
$output_array = array();
foreach($query->result() as $row) {
$output_array[] = array( 'id' => $row->id, 'name' => $row->name );
}
echo json_encode( $output_array );
Notice that the array you pass it can be arbitrarily deep. json_encode
will structure the object based off of the structure of the array. I would also suggest looking at the resultant string in your browser to see what the JSON looks like when encoding multidimensional arrays like this.
In your javascript, you must parse the string to get it to be a useful JavaScript object...
success: function(output_string){
var obj = JSON.parse( output_string );
// This will iterate over all of the values in the return object
for(var i in obj) {
document.write(obj[i].name + '<br>' + obj[i].id);
}
}
Feel free to structure your return object differently, I was just going off of what you laid out in your question.
jQuery also has facilities where, if you specify your MIME types correctly, the parameter to your success
callback will automatically be parsed into a JSON object for you. For more information see the jQuery documentation
Upvotes: 3
Reputation: 2057
I wrote most of this before @watcher's answer came up and it's probably the one you want but this was written already so I figured I post it. I'd modify @David Fells solution like this:
$output = array();
foreach ($query->result() AS $row) {
$output[$row->id] = array();
foreach ($row AS $prop=>$value) {
$output[$row->id][$prop] = $value;
}
}
$output_json = json_encode($output);
I generally use PDO objects for accessing my DBs and that returns an array instead of object. This should work for an array but I believe it will work with objects in PHP as well.
This should work with any query that returns a column named id and obviously the id portion could be replaced with $id where $id equals whatever id string/integer you'd like use.
...
Now when you want the array back in PHP (I'm not particularly familiar with javascript) you would need to use:
// The TRUE returns it as an array rather than object.
$output = json_decode($output_json, TRUE);
Much of this is pretty basic and I imagine you know it already. But the title made me think that the this last part - the json_decode part - was what you needed and maybe it will help someone else who finds this question.
Upvotes: 1
Reputation: 6798
...
$output = array();
foreach ($query->result() as $row) {
$output[] = $row->id . ' ' . $row->name;
}
echo json_encode($output);
Upvotes: 8