Last active
August 29, 2015 14:05
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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