Skip to content

Instantly share code, notes, and snippets.

@RyanTG
Last active August 29, 2015 14:05
Show Gist options
  • Save RyanTG/598bfb06ecbe6e502c0f to your computer and use it in GitHub Desktop.
Save RyanTG/598bfb06ecbe6e502c0f to your computer and use it in GitHub Desktop.
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