PHP Hacks Free Open Book

PHP Hacks

Origami Paper Planes
Paper Airplane Origami Boats. Learn hot to flod this crafts
Previous Page
Next Page

Hack 45. Suck Data from Excel Uploads

Using the XML from Excel 2003, you can read data directly from spreadsheets that customers upload to your site.

Your customers' data can come from many different sources. Making it easy for them to get their data into your system can mean the difference between getting their business and having them go somewhere else for their data needs (and taking their money with them). Supporting data import from common data sources such as Excel can be a very compelling feature for customers.

This hack shows you how to save Excel spreadsheets in the new XML format supported by Excel and Microsoft Office 2003 and how to read that format and display the data back to the user. Figure 5-12 illustrates the flow between the browser (shown here as the computer) and the import system. The first page is index.php, which presents the Browse button. The user then selects an Excel XML file, which is submitted to the import.php page; that page returns an HTML rendering of the data in the file.

Figure 5-12. The flow of the Excel XML import


5.13.1. The Code

index.php (shown in Example 5-34) is responsible for getting the Excel data into your PHP scripts.

Example 5-34. The PHP for getting Excel data into your scripts
<html>
<body>
	<form enctype="multipart/form-data" action="import.php" method="post">
	 Excel XML file:
		<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />

		<input type="file" name="file" /><br/>
	<input type="submit" value="Upload" />
</form>
</body>
</html>

Save the code in Example 5-35 as import.php. It handles the data import.

Example 5-35. PHP that handles Excel data import
<html>
<body>
<?php
$data = array();
if ( $_FILES['file']['tmp_name'] )
{
	
	$dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
	$rows = $dom->getElementsByTagName( 'Row' );
	foreach ($rows as $row)
	{
		$cells = $row->getElementsByTagName( 'Cell' );
		$datarow = array();
		foreach ($cells as $cell)
		{
			$datarow []= $cell->nodeValue;
		}
		$data []= $datarow;
			
	}
}
?>
<table>
<?php foreach( $data as $row ) { ?>
<tr>
<?php foreach( $row as $item ) { ?>
<td><?php echo( $item ); ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
</body>
</html>

The import.php page, which is at the heart of this hack, starts by opening up the uploaded file using the XML DOM reader. Then it iterates through each Row element, and within each, it works through the Cell elements. In each Cell, the script finds the actual data, which is stored into an array called $datarow. That stored data is then output as HTML using standard PHP text templating techniques (at the end of the script).

5.13.2. Running the Hack

Running this hack begins with creating an Excel spreadsheet. As is my practice, I went to the U.S. Census Bureau (http://www.census.gov) to score some data. In this case, I've used median family income in various brackets in the 50 states. (By the way, for a two-person family, the state with the highest median income was Alaska; go figure!) Anyway, the data looks like Figure 5-13.

Figure 5-13. The original Excel spreadsheet


Now I have to convert it to the XML spreadsheet format using Save As, as shown in Figure 5-14.

It's interesting to note here that I can keep the spreadsheet in XML format indefinitely; there is no loss of fidelity or precision between the binary version and the XML version. If you have a customer with Office 2003 and you are giving him a spreadsheet to start with, just give him the XML version! That way he can keep working in it and never know the difference.

Now, with XML in hand, we can upload the pages to the site; using the browser, surf to index.php. The page is shown in Figure 5-15.

Figure 5-14. Saving the spreadsheet in XML format


Figure 5-15. Uploading the spreadsheet to the server


Now click on the Browse button and find the XML spreadsheet. Then click on the Upload button, and that will submit the XML data to the import.php page. This page uses the XML DOM and XPath support in PHP to parse up the XML and extract the table data. Then it uses some simple PHP to format the data as an HTML table. The result for the example census data I showed in Figure 5-13 is shown in Figure 5-16.

Obviously, if you want to input the data into the database, you should change the code from formatting HTML to running SQL INSERT statements.


Figure 5-16. The spreadsheet data in HTML format


These types of user convenience features can be extremely compelling. Instead of spending hours typing their data into HTML forms and working with a clunky web interface, your customers can simply use tools they are familiar with, such as Excel.

5.13.3. See Also

Previous Page
Next Page
Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y][Z]

Origami Paper AirPlane
Paper Airplane Origami Boats

     Main Menu
PHP Hacks
Table of Contents
Copyright
Credits
Preface
Chapter 1.  Installation and Basics
Chapter 2.  Web Design
Chapter 3.  DHTML
Chapter 4.  Graphics
Chapter 5.  Databases and XML
Section 5.1.  Hacks 3450: Introduction
Hack 34. Design Better SQL Schemas
Hack 35. Create Bulletproof Database Access
Hack 36. Create Dynamic Database Access Objects
Hack 37. Generate CRUD Database Code
Hack 38. Read XML on the Cheap with Regular Expressions
Hack 39. Export Database Schema as XML
Hack 40. Create a Simple XML Query Handler for Database Access
Hack 41. Generate Database SQL
Hack 42. Generate Database Select Code
Hack 43. Convert CSV to PHP
Hack 44. Scrape Web Pages for Data
Hack 45. Suck Data from Excel Uploads
Hack 46. Load Your Database from Excel
Hack 47. Search Microsoft Word Documents
Hack 48. Create RTF Documents Dynamically
Hack 49. Create Excel Spreadsheets Dynamically
Hack 50. Create a Message Queue
Chapter 6.  Application Design
Chapter 7.  Patterns
Chapter 8.  Testing
Chapter 9.  Alternative UIs
Chapter 10.  Fun Stuff
Colophon
Index


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplane
Paper Airplane - Paper Airplane - Paper Airplane - Paper Airplane - Paper Airplane - Paper Airplane - Paper Airplane - Paper Airplane -