e698057f8bb6699949fa18ad9a7d41752c3024f8
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.0.1pre / docs / pages / dynamic-samples / category.php
1 <?php
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.
5
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.
9
10 $category_data = array(
11         animals => array(
12                 name => "Animals",
13                 description => "All your favorites from aardvarks to zebras.",
14                 items => array(
15                         array(
16                                 name => "Pets",
17                         ),
18                         array(
19                                 name => "Farm Animals",
20                         ),
21                         array(
22                                 name => "Wild Animals",
23                         )
24                 )
25         ),
26         colors => array(
27                 name => "Colors",
28                 description => "Fresh colors from the magic rainbow.",
29                 items => array(
30                         array(
31                                 name => "Blue",
32                         ),
33                         array(
34                                 name => "Green",
35                         ),
36                         array(
37                                 name => "Orange",
38                         ),
39                         array(
40                                 name => "Purple",
41                         ),
42                         array(
43                                 name => "Red",
44                         ),
45                         array(
46                                 name => "Yellow",
47                         ),
48                         array(
49                                 name => "Violet",
50                         )
51                 )
52         ),
53         vehicles => array(
54                 name => "Vehicles",
55                 description => "Everything from cars to planes.",
56                 items => array(
57                         array(
58                                 name => "Cars",
59                         ),
60                         array(
61                                 name => "Planes",
62                         ),
63                         array(
64                                 name => "Construction",
65                         )
66                 )
67         )
68 );
69
70 // Get the name of the category to display from
71 // the query params for the script.
72
73 $category_name = '';
74 if ( $_GET[ 'id' ] ) {
75         $category_name = $_GET[ 'id' ];
76 }
77
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.
81
82 $category_obj  = $category_data[ $category_name ];
83
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.
87
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 ) {
92                 echo 'null';
93         } else {
94                 echo '{"name":"' . $category_obj[ 'name' ]
95                         . '","description":"' . $category_obj[ 'description' ]
96                         . '","items":[';
97
98                 $arr = $category_obj[ 'items' ];
99                 $count = count($arr);
100                 for ( $i = 0; $i < $count; $i++ ) {
101                         if ( $i ) {
102                                 echo ",";
103                         }
104                         echo '{"name":"' . $arr[ $i ][ 'name' ] . '"}';
105                 }
106                 echo "]}";
107         }
108 } else {
109         // Data should be written out as HTML.
110         header("Content-type: text/html");
111 ?>
112 <!DOCTYPE HTML>
113 <html>
114 <head>
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>
121 </head>
122 <body>
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">
126 <?php
127                 if ( !$category_obj ) {
128 ?>
129                 <p>No matches found.</p>
130 <?php
131                 } else {
132 ?>
133                 <p><?php echo $catgory_object['description']; ?></p>
134                 <ul data-role="listview" data-inset="true">
135 <?php
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";
140                         }
141 ?>
142                 </ul>
143 <?php
144                 }
145 ?>
146         </div>
147 </div>
148 </body>
149 </html>
150 <?php }