Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 * Copyright 2010-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License").
6
 * You may not use this file except in compliance with the License.
7
 * A copy of the License is located at
8
 *
9
 *  http://aws.amazon.com/apache2.0
10
 *
11
 * or in the "license" file accompanying this file. This file is distributed
12
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13
 * express or implied. See the License for the specific language governing
14
 * permissions and limitations under the License.
15
 */
16
 
17
/*
18
	PREREQUISITES:
19
	In order to run this sample, I'll assume a few things:
20
 
21
	* You already have a valid Amazon Web Services developer account, and are
22
	  signed up to use Amazon SimpleDB <http://aws.amazon.com/simpledb>.
23
 
24
	* You already understand the fundamentals of object-oriented PHP.
25
 
26
	* You've verified that your PHP environment passes the SDK Compatibility Test.
27
 
28
	* You've already added your credentials to your config.inc.php file, as per the
29
	  instructions in the Getting Started Guide.
30
 
31
	TO RUN:
32
	* Run this file on your web server by loading it in your browser. It will generate HTML output.
33
*/
34
 
35
 
36
/*%******************************************************************************************%*/
37
// SETUP
38
 
39
	// Enable full-blown error reporting. http://twitter.com/rasmus/status/7448448829
40
	error_reporting(-1);
41
 
42
	// Set HTML headers
43
	header("Content-type: text/html; charset=utf-8");
44
 
45
	// Include the SDK
46
	require_once '../sdk.class.php';
47
 
48
 
49
/*%******************************************************************************************%*/
50
// ADD DATA TO SIMPLEDB
51
 
52
	// Instantiate the AmazonSDB class
53
	$sdb = new AmazonSDB();
54
 
55
	// Store the name of the domain
56
	$domain = 'php-sdk-getting-started';
57
 
58
	// Create the domain
59
	$new_domain = $sdb->create_domain($domain);
60
 
61
	// Was the domain created successfully?
62
	if ($new_domain->isOK())
63
	{
64
		// Add a batch of item-key-values to your domain
65
		$add_attributes = $sdb->batch_put_attributes($domain, array(
66
			'Item_01' => array(
67
				'Category'    => 'Clothes',
68
				'Subcategory' => 'Sweater',
69
				'Name'        => 'Cathair Sweater',
70
				'Color'       => 'Siamese',
71
				'Size'        => array('Small', 'Medium', 'Large')
72
			),
73
			'Item_02' => array(
74
				'Category'    => 'Clothes',
75
				'Subcategory' => 'Pants',
76
				'Name'        => 'Designer Jeans',
77
				'Color'       => 'Paisley Acid Wash',
78
				'Size'        => array('30x32', '32x32', '32x34')
79
			),
80
			'Item_03' => array(
81
				'Category'    => 'Clothes',
82
				'Subcategory' => 'Pants',
83
				'Name'        => 'Sweatpants',
84
				'Color'       => array('Blue', 'Yellow', 'Pink'),
85
				'Size'        => 'Large',
86
				'Year'        => array('2006', '2007')
87
			),
88
			'Item_04' => array(
89
				'Category'    => 'Car Parts',
90
				'Subcategory' => 'Engine',
91
				'Name'        => 'Turbos',
92
				'Make'        => 'Audi',
93
				'Model'       => 'S4',
94
				'Year'        => array('2000', '2001', '2002')
95
			),
96
			'Item_05' => array(
97
				'Category'    => 'Car Parts',
98
				'Subcategory' => 'Emissions',
99
				'Name'        => 'O2 Sensor',
100
				'Make'        => 'Audi',
101
				'Model'       => 'S4',
102
				'Year'        => array('2000', '2001', '2002')
103
			),
104
		));
105
 
106
		// Were the attributes added successfully?
107
		if ($add_attributes->isOK())
108
		{
109
			// Add an additional size to Item_01
110
			$append_attributes = $sdb->put_attributes($domain, 'Item_01', array(
111
				'Size' => 'Extra Large'
112
			));
113
 
114
			// Were the new attributes appended successfully?
115
			if ($append_attributes->isOK())
116
			{
117
			 	// Use a SELECT expression to query the data.
118
				// Notice the use of backticks around the domain name.
119
				$results = $sdb->select("SELECT * FROM `{$domain}` WHERE Category = 'Clothes'");
120
 
121
				// Get all of the <Item> nodes in the response
122
				$items = $results->body->Item();
123
 
124
				// Re-structure the data so access is easier (see helper function below)
125
				$data = reorganize_data($items);
126
 
127
				// Generate <table> HTML from the data (see helper function below)
128
				$html = generate_html_table($data);
129
			}
130
		}
131
	}
132
 
133
 
134
/*%******************************************************************************************%*/
135
// HELPER FUNCTIONS
136
 
137
	function reorganize_data($items)
138
	{
139
		// Collect rows and columns
140
		$rows = array();
141
		$columns = array();
142
 
143
		// Loop through each of the items
144
		foreach ($items as $item)
145
		{
146
			// Let's append to a new row
147
			$row = array();
148
			$row['id'] = (string) $item->Name;
149
 
150
			// Loop through the item's attributes
151
			foreach ($item->Attribute as $attribute)
152
			{
153
				// Store the column name
154
				$column_name = (string) $attribute->Name;
155
 
156
				// If it doesn't exist yet, create it.
157
				if (!isset($row[$column_name]))
158
				{
159
					$row[$column_name] = array();
160
				}
161
 
162
				// Append the new value to any existing values
163
				// (Remember: Entries can have multiple values)
164
				$row[$column_name][] = (string) $attribute->Value;
165
				natcasesort($row[$column_name]);
166
 
167
				// If we've not yet collected this column name, add it.
168
				if (!in_array($column_name, $columns, true))
169
				{
170
					$columns[] = $column_name;
171
				}
172
			}
173
 
174
			// Append the row we created to the list of rows
175
			$rows[] = $row;
176
		}
177
 
178
		// Return both
179
		return array(
180
			'columns' => $columns,
181
			'rows' => $rows,
182
		);
183
	}
184
 
185
	function generate_html_table($data)
186
	{
187
		// Retrieve row/column data
188
		$columns = $data['columns'];
189
		$rows = $data['rows'];
190
 
191
		// Generate shell of HTML table
192
		$output = '<table cellpadding="0" cellspacing="0" border="0">' . PHP_EOL;
193
		$output .= '<thead>';
194
		$output .= '<tr>';
195
		$output .= '<th></th>'; // Corner of the table headers
196
 
197
		// Add the table headers
198
		foreach ($columns as $column)
199
		{
200
			$output .= '<th>' . $column . '</th>';
201
		}
202
 
203
		// Finish the <thead> tag
204
		$output .= '</tr>';
205
		$output .= '</thead>' . PHP_EOL;
206
		$output .= '<tbody>';
207
 
208
		// Loop through the rows
209
		foreach ($rows as $row)
210
		{
211
			// Display the item name as a header
212
			$output .= '<tr>' . PHP_EOL;
213
			$output .= '<th>' . $row['id'] . '</th>';
214
 
215
			// Pull out the data, in column order
216
			foreach ($columns as $column)
217
			{
218
				// If we have a value, concatenate the values into a string. Otherwise, nothing.
219
				$output .= '<td>' . (isset($row[$column]) ? implode(', ', $row[$column]) : '') . '</td>';
220
			}
221
 
222
			$output .= '</tr>' . PHP_EOL;
223
		}
224
 
225
		// Close out our table
226
		$output .= '</tbody>';
227
		$output .= '</table>';
228
 
229
		return $output;
230
	}
231
 
232
 
233
?><!DOCTYPE html>
234
<html>
235
	<head>
236
		<meta http-equiv="Content-type" content="text/html; charset=utf-8">
237
		<title>sdb_create_domain_data</title>
238
		<style type="text/css" media="screen">
239
		body {
240
			margin: 0;
241
			padding: 0;
242
			font: 14px/1.5em "Helvetica Neue", "Lucida Grande", Verdana, Arial, sans;
243
			background-color: #fff;
244
			color: #333;
245
		}
246
		table {
247
			margin: 50px auto 0 auto;
248
			padding: 0;
249
			border-collapse: collapse;
250
		}
251
		table th {
252
			background-color: #eee;
253
		}
254
		table td,
255
		table th {
256
			padding: 5px 10px;
257
			border: 1px solid #eee;
258
		}
259
		table td {
260
			border: 1px solid #ccc;
261
		}
262
		</style>
263
	</head>
264
	<body>
265
 
266
		<!-- Display HTML table -->
267
		<?php echo $html; ?>
268
 
269
	</body>
270
</html>