File: category.php

package info (click to toggle)
jquery-mobile 1.2.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,068 kB
  • ctags: 243
  • sloc: php: 113; makefile: 54
file content (150 lines) | stat: -rw-r--r-- 3,395 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
// This is a demo script that takes a single 'id' query param argument and
// returns its associated data as HTML, or, if called via XmlHttpRequest,
// returns its data as JSON.

// In the real-world, this category data would be looked
// up on the fly from some database. For this sample, we
// are just using some static in-memory data.

$category_data = array(
	animals => array(
		name => "Animals",
		description => "All your favorites from aardvarks to zebras.",
		items => array(
			array(
				name => "Pets",
			),
			array(
				name => "Farm Animals",
			),
			array(
				name => "Wild Animals",
			)
		)
	),
	colors => array(
		name => "Colors",
		description => "Fresh colors from the magic rainbow.",
		items => array(
			array(
				name => "Blue",
			),
			array(
				name => "Green",
			),
			array(
				name => "Orange",
			),
			array(
				name => "Purple",
			),
			array(
				name => "Red",
			),
			array(
				name => "Yellow",
			),
			array(
				name => "Violet",
			)
		)
	),
	vehicles => array(
		name => "Vehicles",
		description => "Everything from cars to planes.",
		items => array(
			array(
				name => "Cars",
			),
			array(
				name => "Planes",
			),
			array(
				name => "Construction",
			)
		)
	)
);

// Get the name of the category to display from
// the query params for the script.

$category_name = '';
if ( $_GET[ 'id' ] ) {
	$category_name = $_GET[ 'id' ];
}

// Now get the category data, by name, from our in-memory
// dictionary. This is the part where a script normally fetches
// the data from a database.

$category_obj  = $category_data[ $category_name ];

// Now figure out how the script is being called. If it's being
// called via XmlHttpRequest, then send the data back as JSON.
// If not, then send it back as a list in an HTML document.

if( $_SERVER[ "HTTP_X_REQUESTED_WITH" ] && $_SERVER[ "HTTP_X_REQUESTED_WITH" ] ==="XMLHttpRequest" ) {
	// Data should be written out as JSON.
	header("Content-type: application/json");
	if ( !$category_obj ) {
		echo 'null';
	} else {
		echo '{"name":"' . $category_obj[ 'name' ]
			. '","description":"' . $category_obj[ 'description' ]
			. '","items":[';

		$arr = $category_obj[ 'items' ];
		$count = count($arr);
		for ( $i = 0; $i < $count; $i++ ) {
			if ( $i ) {
				echo ",";
			}
			echo '{"name":"' . $arr[ $i ][ 'name' ] . '"}';
		}
		echo "]}";
	}
} else {
	// Data should be written out as HTML.
	header("Content-type: text/html");
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Vehicles</title>
<link rel="stylesheet"  href="../../../css/themes/default/">
<script src="../../../js/jquery.js"></script>
<script src="../../../js/jquery.mobile-1.2.0.js"></script>
</head>
<body>
<div data-role="page" data-add-back-btn="true">
	<div data-role="header"><h1><?php if ( $category_obj ) { echo $category_obj['name']; } else { echo "No Match"; } ?></h1></div>
	<div data-role="content">
<?php
		if ( !$category_obj ) {
?>
		<p>No matches found.</p>
<?php
		} else {
?>
		<p><?php echo $catgory_object['description']; ?></p>
		<ul data-role="listview" data-inset="true">
<?php
			$arr = $category_obj[ 'items' ];
			$count = count($arr);
			for ( $i = 0; $i < $count; $i++ ) {
				echo "\t\t\t<li>" . $arr[ $i ][ 'name' ] . "</li>\n";
			}
?>
		</ul>
<?php
		}
?>
	</div>
</div>
</body>
</html>
<?php }