2 // This is a demo script that takes a single 'id' query param argument and
3 // returns its associated data as HTML, or, if called via XmlHttpRequest,
4 // returns its data as JSON.
6 // In the real-world, this category data would be looked
7 // up on the fly from some database. For this sample, we
8 // are just using some static in-memory data.
10 $category_data = array(
13 description => "All your favorites from aardvarks to zebras.",
19 name => "Farm Animals",
22 name => "Wild Animals",
28 description => "Fresh colors from the magic rainbow.",
55 description => "Everything from cars to planes.",
64 name => "Construction",
70 // Get the name of the category to display from
71 // the query params for the script.
74 if ( $_GET[ 'id' ] ) {
75 $category_name = $_GET[ 'id' ];
78 // Now get the category data, by name, from our in-memory
79 // dictionary. This is the part where a script normally fetches
80 // the data from a database.
82 $category_obj = $category_data[ $category_name ];
84 // Now figure out how the script is being called. If it's being
85 // called via XmlHttpRequest, then send the data back as JSON.
86 // If not, then send it back as a list in an HTML document.
88 if( $_SERVER[ "HTTP_X_REQUESTED_WITH" ] && $_SERVER[ "HTTP_X_REQUESTED_WITH" ] ==="XMLHttpRequest" ) {
89 // Data should be written out as JSON.
90 header("Content-type: application/json");
91 if ( !$category_obj ) {
94 echo '{"name":"' . $category_obj[ 'name' ]
95 . '","description":"' . $category_obj[ 'description' ]
98 $arr = $category_obj[ 'items' ];
100 for ( $i = 0; $i < $count; $i++ ) {
104 echo '{"name":"' . $arr[ $i ][ 'name' ] . '"}';
109 // Data should be written out as HTML.
110 header("Content-type: text/html");
115 <meta charset="utf-8">
116 <meta name="viewport" content="width=device-width">
117 <title>Vehicles</title>
118 <link rel="stylesheet" href="../../../css/themes/default/">
119 <script src="../../../js/jquery.js"></script>
120 <script src="../../../js/"></script>
123 <div data-role="page" data-add-back-btn="true">
124 <div data-role="header"><h1><?php if ( $category_obj ) { echo $category_obj['name']; } else { echo "No Match"; } ?></h1></div>
125 <div data-role="content">
127 if ( !$category_obj ) {
129 <p>No matches found.</p>
133 <p><?php echo $catgory_object['description']; ?></p>
134 <ul data-role="listview" data-inset="true">
136 $arr = $category_obj[ 'items' ];
137 $count = count($arr);
138 for ( $i = 0; $i < $count; $i++ ) {
139 echo "\t\t\t<li>" . $arr[ $i ][ 'name' ] . "</li>\n";