0

I have a mysql table with image data in.

I want to retrieve the image data and push into a javascript array.

There are two fields that I want to put into the array and they are image_ref, and image_name I know I need a 2D array for this and I need to retrive the data from the db using ajax and JSON.

I am not sure how to complete this.

I know I need to call the php page with a JSON ajax call

js:

var imagesArray[];

$.getJSON("get_image_data.php")
    .done(function(data) { 
  /*THIS IS WHERE I NEED HELP---*/
});         

get_image_data.php page

include("connect.php"); 
$query = mysql_query("SELECT * FROM images WHERE live='1' ORDER BY insertDate DESC");
while($row=mysql_fetch_array($query){
     echo json_encode(array("a" => $row['image_ref'], "b" => $row['image_name'])); 
}

I have highlighted where I need help in getting the returned data into the stockImages array.

Can anyone show me the light?

thanks in advance

5
  • Why would you want to put them in a 2D array ? A 1D array of objects is clearer and JSON allows you to do that easily. Commented Sep 27, 2013 at 12:31
  • I need a 2 d array as there are two fields for each image so the array would be imagesArray[0][0] etc I need the image_name for showing the image on the page and the image_ref for referencing it back to the db for any changes i.e. if i delete the image the field 'live' needs to be changed and I would use the image_ref to do this as uploaded user images can have the same name Commented Sep 27, 2013 at 12:41
  • You do not need a 2D array since an object itself is some kind of array with string used as indexes. And your PHP code is wrong. You can't combine associative arrays and fetch_array(). I suggest you learn languages and read documentations before using them and asking questions. Commented Sep 27, 2013 at 12:58
  • How am I supposed to learn without teachers and getting advice from here is asking for teachers as I stated in my post, I need help Commented Sep 27, 2013 at 13:02
  • There are plenty of AJAX, MySQL and JSON tutorials on internet. Commented Sep 27, 2013 at 13:19

2 Answers 2

2

By calling json_encode() multiple times in your loop, you are creating lots of singular bits of JSON. You should aim at creating and transmitting one transfer object, like this:

include("connect.php"); 
$query = mysql_query("SELECT * FROM images WHERE live='1' ORDER BY insertDate DESC");
$images = array(); 
while($row=mysql_fetch_array($query){
    $images[] = array("a" => $row['image_ref'], "b" => $row['image_name']); 
}
echo json_encode($images);

This will give you one JSON array, through which you can loop in your client side Javascript.

4
  • When I run the code with your updated php I alert the returned data and it echos as data=[object,Object][object,Object] can you tell me why it is doing this Commented Sep 27, 2013 at 14:05
  • Yes, that's OK. You are getting a Javascript object as a return - remember that JSON means Javascript OBJECT notation. It might have been wrong to use the term "array" above. In order to properly inspect the data variable, use a tool like Firebug and console.log(data) instead of alert(data) Commented Sep 27, 2013 at 16:42
  • the console in chrome returns [Object, Object] 0: Object 1: Object length: 2 in firefox the console logs nothing. How do I use the data to create an image on the page because when I do this Commented Sep 28, 2013 at 7:52
  • ah i finally get it after a week of scratching my head. To utilise the json object i have to call 'array[0].b' or 'array[0].b' Commented Oct 4, 2013 at 8:30
0

Actually, it is PHP that is wrong, 'cause on JS you will have the array you need directly in "data".

You PHP code should look like:

$query = mysql_query("SELECT * FROM images WHERE live='1' ORDER BY insertDate DESC");
$result = array();
while($row=mysql_fetch_array($query){
    $result[] = array("a" => $row['image_ref'], "b" => $row['image_name']); 
}
echo(json_encode($result));
1
  • @Katran I have changed the php to match yours but I am getting an error from my javascript $.getJSON("get_image_data.php").done(function(data) { stockImagesArray=data; }); it errors saying -- TypeError: $.getJSON(...).done is not a function @... Commented Sep 27, 2013 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.