Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Iterating through json using PHP and jQuery - for displaying pinball machines on your website using the Pinball Map API
//jQuery
jQuery(document).ready(function() {
jQuery.getJSON("http://pinballmap.com/api/v1/locations/4845/machine_details.json", function(data) {
jQuery.each(data.machines, function(i, obj) {
var machineName = data.machines[i].name;
var link = data.machines[i].ipdb_link;
var year = data.machines[i].year;
var manufacturer = data.machines[i].manufacturer;
var list = jQuery(".machine_list");
jQuery("<h4 />", { html: "<a href=" + link + ">" + machineName + "</a> (" + manufacturer + (manufacturer? ", ": "") + year + ")"}).appendTo(list);
});
});
});
/** PHP **/
<?php
$url = "http://pinballmap.com/api/v1/locations/4845/machine_details.json";
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
for($i=0; $i<count($data['machines']); $i++) {
$link = $data['machines'][$i]["ipdb_link"];
$name = $data['machines'][$i]["name"];
$manufacturer = $data['machines'][$i]["manufacturer"];
$year = $data['machines'][$i]["year"];
echo "<h4><a href=" . $link . ">" . $name . "</a> (" . $manufacturer . ($manufacturer? ", " : "") . $year . ")</h4>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment