Get JSON from PHP and display it in HTML
I have a SQL database linking to a PHP page which produces the following
JSON:
[{"ID":"1","SubjectName":"Subject 1"},{"ID":"2","SubjectName":"Subject 2"}]
I was wondering how to format and display this in a separate HTML page.
For example: (Using JQuery Mobile)
<ul data-role="listview" data-divider-theme="b" data-inset="true">
<li data-role="list-divider" role="heading">Subject1</li>
<li data-theme="c">1</li>
<li data-role="list-divider" role="heading">Subject2</li>
<li data-theme="c">2</li>
</ul>
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL at".$hostname/"<br />");
//Select Database
$selected = mysql_select_db($databasename,$dbhandle)
OR die("Could not select Database:".$databasename."<br />");
//SQL Query
$result = mysql_query("SELECT ID, SubjectName FROM Subject");
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
//while ($row = mysql_fetch_array($result)) {
// echo "ID:".$row{'ID'}." Subject:".$row{'SubjectName'}."<br />";
// }
mysql_close($dbhandle);
?>
I'm using PhoneGap, so I can only use Java to do this.
I have found some tutorials, but they use JSON from the URL, but this is
only displaying on the page.
No comments:
Post a Comment