1

I have been playing around with JSON for a few days and I really think it's a pretty cool way to interchange data making use of it... I am building an app using jquery mobile where I am trying to populate Json data, so far I have tried this approach:

From a json file named movie-details.json I have this:

{"movies":[{"id":"1","name":"Dabangg2","picUrl":"http:\/\/www.naz8.com\/images\/Dabangg2.jpg"},{"id":"2","name":"Talassh","picUrl":"http:\/\/www.naz8.com\/images\/talassh.jpg"},{"id":"3","name":"JAB TAK HAI JAAN","picUrl":"http:\/\/www.naz8.com\/images\/jthj.jpg"},{"id":"4","name":"Khiladi 786","picUrl":"http:\/\/www.naz8.com\/images\/khiladi786.jpg"}]}

and I can get the data via the following to dynamically create a detailed listview:

<script type="text/javascript">

 $.getJSON("movie-details.json", function(movies){
   //Start off with an empty list every time to get the latest from server
   $('#movieList').empty();

   //add the movie items as list
   $.each(movies, function(i, movie){
     $('#movieList').append(generateMovieLink(movie));
   });

   //refresh the list view to show the latest changes
   $('#movieList').listview('refresh');

 });

  //creates a movie link list item
 function generateMovieLink(movie){

  //debugger;
  return '<li><a href="javascript:void(0)'
        + '" onclick="goToMovieDetailPage(\''
        + movie.name 
        + '\',\''
        + movie.picUrl +'\')">' 
        + movie.name 
        + '</a></li>';
 }

 function goToMovieDetailPage(movieName, moviePicUrl){

  //create the page html template
  var moviePage = $("<div data-role='page' data-url=dummyUrl><div data-role='header' data-add-back-btn='true'><h1>"
                  + movieName + "</h1></div><div data-role='content'><img border='0' src='" 
                  + moviePicUrl + "' width=204 height=288></img></div><div data-role='footer' data-position='fixed'><h4>" 
                  + movieName + "</h4></div></div>");

  //append the new page to the page container
  moviePage.appendTo( $.mobile.pageContainer );

  //go to the newly created page
  $.mobile.changePage( moviePage );
 }  

</script>

how can I populate the data from a php file, for example movie-details.php:

<?php
include('connection.php');
$var = array();
$sql = "SELECT * FROM movies";
$result = mysqli_query($con, $sql);

while($obj = mysqli_fetch_object($result)) {
$var[] = $obj;
}
echo '{"movies":'.json_encode($var).'}';
?>

What variables do I have to declare to fetch the json data from the objet in the php file?

For example:

// JUST AN IDEA... I AM CONFUSED..

var url="....";

$.getJSON(url,function(...){

$.each(movie.movies, function(i,...)

Your help is greatly appreciated...

4
  • 2
    movies is an object, the array is movies.movies Commented Jul 15, 2014 at 17:51
  • Thank Charlietfl for your comment... but I need a clearer example as per what should be declared for the fetching process to work: Commented Jul 15, 2014 at 17:56
  • $.each(movies.movies, function ... Commented Jul 15, 2014 at 18:03
  • Hi Charlietlf, Thank you so much... yes... I just modified the $.each(movies, function(...) with $.each(movies.movies, function(...) based on your previous answered and it works beatufully... THANK YOU AGAIN... Commented Jul 15, 2014 at 18:30

2 Answers 2

1

You should json encode the whole shebang.

echo json_encode(array("movies" => $var));

As for the jquery method, $.getJSON('<url>', ...) will work.

1
  • Chris... Thank you as well for passing me the code above although I already had echo '{"movies":'.json_encode($var).'}'; that works efficiently in the same way... Commented Jul 15, 2014 at 18:31
0

The easiest way for me is to just get the visual of the data as a tree from the console.

$.getJSON(url, function(data){  
    console.log(data) 
});

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.