<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHPBeginner.net</title>
	<atom:link href="http://phpbeginner.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpbeginner.net</link>
	<description>Where to start learning PHP</description>
	<lastBuildDate>Sun, 10 Jan 2010 18:36:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using SimpleXML to read feeds</title>
		<link>http://phpbeginner.net/2010/01/10/using-simplexml-to-read-feeds/</link>
		<comments>http://phpbeginner.net/2010/01/10/using-simplexml-to-read-feeds/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 18:36:00 +0000</pubDate>
		<dc:creator>Robert Gonzalez</dc:creator>
				<category><![CDATA[Instruction]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[SimpleXML]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://phpbeginner.net/?p=40</guid>
		<description><![CDATA[A question was asked today on the Professional PHP Developer&#8217;s Google group  related to SimpleXML and using it to read feeds. From time to time developers have a need to consume web service feeds, like RSS feeds, and use them in some capacity. Whether it be reading for display or for aggregation, the basic [...]]]></description>
			<content:encoded><![CDATA[<p>A question was asked today on the <a href="http://groups.google.com/group/professional-php" title="You should the group... we are a great bunch of PHP nerds">Professional PHP Developer&#8217;s Google group </a> related to SimpleXML and using it to read feeds. From time to time developers have a need to consume web service feeds, like RSS feeds, and use them in some capacity. Whether it be reading for display or for aggregation, the basic concept is A) get the feed source; B) parse it; and C) do what you need to with it.</p>
<p><a href="http://www.php.net/manual/en/book.simplexml.php" title="PHP: Learn it, Love it, Live it">The SimpleXML PHP extension</a> provides a very easy to use way to do all of this. </p>
<p><strong>First things first</strong><br />
The first thing we are going to need is a feed source. In today&#8217;s question, that source is the BBC News. The feed can be found at <a href="http://newsapi.bbc.co.uk/feeds/search/news+sport/test" title="Our sample feed source">http://newsapi.bbc.co.uk/feeds/search/news+sport/test</a> and a quick visit in your web browser should show you the contents of the feed we will be parsing.</p>
<p>But what fun would it be to just look at another application&#8217;s rendering of our feed. We should have a look using something we build, right? <img src='http://phpbeginner.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Let&#8217;s get an idea of how the feed is structured first.</p>
<blockquote><p><strong>QUICK NOTE HERE:</strong> If you are not quite yet familiar with Object Oriented Programming this may seem a bit odd to you. Since this is a beginner web site I am not at all going to go into the concepts of OOP here. Just know that some of the stuff we are going to be doing here are going to be OOP related. Don&#8217;t get scared, I&#8217;ll be right by your side. <img src='http://phpbeginner.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p></blockquote>
<p><strong>Feed me</strong><br />
Ok, let&#8217;s get our hands dirty on some code, shall we? Just to get a feel for what we are going to be working, let&#8217;s have a look at the feed source as a SimpleXML object by taking the following code and running it:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Set the content type header to be plain jane text so we don't have to riddle
 * our output with &quot;pre&quot; tags
 */</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: text/plain'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Load up our source feed as a SimpleXML object
 */</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://newsapi.bbc.co.uk/feeds/search/news+sport/test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Let's look at it now, so we can see the structure of the feed
 */</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I have clipped it for length but the basics of the output of this, as I run it (because feeds are time based, so your output will more than likely be different than mine) is [<a href="/samples/xml/bbc-news-sports-raw.php" title="View the output of this code">View the current output of this script</a>]:</p>
<pre>SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 2.0
        )

    [channel] => SimpleXMLElement Object
        (
            [ttl] => 15
            [title] => BBC News and Sport Search: test
            [link] => http://newsapi.bbc.co.uk/feeds/search/news+sport/test
            [description] => BBC News and Sport Search: test
            [language] => en-gb
            [lastBuildDate] => Sun, 10 Jan 2010 17:26:19 GMT
            [item] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [title] => Battle to beat freeze continues
                            [link] => http://news.bbc.co.uk/go/rss/news/int/search/news%2Bsport/test/-/2/hi/uk_news/8450690.stm
                            [guid] => http://news.bbc.co.uk/2/hi/uk_news/8450690.stm
                            [pubDate] => Sun, 10 Jan 2010 17:18:24 GMT
                            [description] => The government pledges to do all it can to keep roads and schools open as the severe wintry weather eases its grip on the UK.
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [title] => Gers sweat on key duo's fitness
                            [link] => http://news.bbc.co.uk/go/rss/news/int/search/news%2Bsport/test/-/sport2/hi/football/teams/r/rangers/8450831.stm
                            [guid] => http://news.bbc.co.uk/sport2/hi/football/teams/r/rangers/8450831.stm
                            [pubDate] => Sun, 10 Jan 2010 16:47:04 GMT
                            [description] => Rangers assistant manager Ally McCoist is hoping Steven Davis and Kris Boyd will be able to shrug off injuries sustained in the 3-3 draw at Hamilton.
                        )
                )

        )

)
</pre>
<p>Ok, settle down. Let&#8217;s not freak out over all that data. Looking at the structure we can see that the root element has a property called &#8220;channel&#8221; that the feed data lives in. The &#8220;channel&#8221; property has a collection of properties but the ones we are going to be most interested in are &#8220;title&#8221;, &#8220;lastBuildDate&#8221; and &#8220;item&#8221;. </p>
<p>Looking at these properties we can guess fairly well what they do. Title is the feed title, lastBuildDate is the last time to feed was built and item is going to be the collection of feed entries.</p>
<p>The item collection likewise has a collection of properties in each item, and the properties we are going to be most interested in are &#8220;title&#8221;, &#8220;link&#8221;, &#8220;pubDate&#8221; and &#8220;description&#8221;. Again, looking at these properties you can intuitively guess what each one does: title is the item title, link is the URL of the item, pubDate is the date the item was published and description is the text of the item.</p>
<p><strong>Putting it all together</strong><br />
Knowing all of this we can now put together a very simple page that will consume this feed and render it. To do that, run this script in your browser [<a href="/samples/xml/bbc-news-sports.php" title="View the output of this code">View the output of this script</a>]:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Get the data source feed as a SimpleXML object
 */</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://newsapi.bbc.co.uk/feeds/search/news+sport/test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;BBC News Sports Feed&lt;/title&gt;
	&lt;style type=&quot;text/css&quot;&gt;
		.feed-item {
			border: solid 1px #008;
			margin: 1em 0;
		}
&nbsp;
		.row {
			background-color: #fff;
		}
&nbsp;
		.row-on {
			background-color: #dedede;
		}
	&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* Make a title for this page taken from the feed title */</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;h1&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/h1&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* For user experience, let them know the last time this feed updated */</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;p&gt;&lt;small&gt;Last updated <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lastBuildDate</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/small&gt;&lt;/p&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Now loop through the channel items to get the item information. Because
 * SimpleXML implements the countable and iterator interfaces we can loop
 * over just about any SimpleXML element recursively.
 * 
 * For styling, we are going to alternate row color
 */</span>
<span style="color: #666666; font-style: italic;">// Set the row coloring flag</span>
<span style="color: #000088;">$color</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Loop the feed items</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* Handle row color switching */</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;div class=&quot;feed-item row<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>-on<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* Make a title for this item, linking back to its original source*/</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;h2&gt;&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/a&gt;&lt;/h2&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* Show the body of the item with the publication date */</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;p&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
	&lt;p&gt;&lt;strong&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pubDate</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/strong&gt;&lt;/p&gt;
&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* Reset the row colorizer as needed */</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$color</span> <span style="color: #339933;">=</span> <span style="color: #339933;">!</span><span style="color: #000088;">$color</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>And there you have it, a nice little parsed output of an RSS feed using SimpleXML. See, that wasn&#8217;t so bad, was it?</p>
]]></content:encoded>
			<wfw:commentRss>http://phpbeginner.net/2010/01/10/using-simplexml-to-read-feeds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple PHP Excel exports</title>
		<link>http://phpbeginner.net/2009/11/08/simple-php-excel-exports/</link>
		<comments>http://phpbeginner.net/2009/11/08/simple-php-excel-exports/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 22:43:43 +0000</pubDate>
		<dc:creator>Robert Gonzalez</dc:creator>
				<category><![CDATA[Instruction]]></category>

		<guid isPermaLink="false">http://phpbeginner.net/?p=29</guid>
		<description><![CDATA[From time to time developers may need to be able to export data to an Excel spreadsheet. And there are lots of tools that allow developers to do just that, in many different, robust ways.
But what if you just need to put something together quickly that allows your users to be able to take a [...]]]></description>
			<content:encoded><![CDATA[<p>From time to time developers may need to be able to export data to an Excel spreadsheet. And there are lots of tools that allow developers to do just that, in many different, robust ways.</p>
<p>But what if you just need to put something together quickly that allows your users to be able to take a data set &#8211; for example a database query result &#8211; and download that in the form of an Excel spreadsheet? It is easier to do than you might think.</p>
<p>Let&#8217;s say your boss just called you into his office and said that he would like you to put an Excel spreadsheet together that will let him see all of the employees in your company. And let&#8217;s say that you have an intranet page that shows all of your employees on the screen in a table. Since there is a good chance that your boss isn&#8217;t the only person that might find an Excel spreadsheet of the employee list useful, you decide you&#8217;re going to take the employee list from the intranet and allow your entire company to download a copy of it in Excel format.</p>
<p>Before we get started, we will need a data set to work with. For this example we are going to keep our employee list very simple:<br />
<code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$employees</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Hans'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Auf'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1D'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1145'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Yung'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Ling'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'3C'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1220'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Mike'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Rowe'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'4E'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1232'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Joe'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Shmoe'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'2B'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1159'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Mary'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Smith'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1A'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1111'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p></code></p>
<p>Now, to add a little more complexity to our simple example (because we can <img src='http://phpbeginner.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ) we are going to make a listing of column headers for our employee table. This will also serve as our spreadsheet header row as well:<br />
<code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$columns</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'First Name'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Last Name'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'room'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Office Address'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'ext'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Phone Number'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p></code></p>
<p>If you look closely you can see that we mapped the columns array keys to the query result column names. The reason we are doing this is because as we present our HTML table of this data we can control the column order and column heading text with this array. Let&#8217;s have a look at that:<br />
<code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Build a mock data set as though it were a database query result
 */</span>
<span style="color: #000088;">$employees</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Hans'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Auf'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1D'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1145'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Yung'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Ling'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'3C'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1220'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Mike'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Rowe'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'4E'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1232'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Joe'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Shmoe'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'2B'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1159'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Mary'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Smith'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1A'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1111'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Build a custom header array for column headings
 * 
 * NOTE[0]: You could have done this in the query by aliasing column names, but 
 * doing it this way gives us a little something more to learn. ;)
 * 
 * NOTE[1]: If you miss it the first time around, come back and look at this to
 * see how you can control the column display order with this array.
 */</span>
<span style="color: #000088;">$columns</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'First Name'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Last Name'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'room'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Office Address'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'ext'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Phone Number'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Company employee list&lt;/title&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;h1&gt;Our company employee list&lt;/h1&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">// If there are employees list them ?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$employees</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!-- Let's build our employee table --&gt;
&lt;table border=&quot;1&quot;&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">// Let's build the header row first ?&gt; </span>
	<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$columns</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$column</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
		<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$column</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
	<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">// Now let's build the body ?&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$employees</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$employee</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
	&lt;tr&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* Let's make this a little dynamic... Yes, this is a lot of */</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* looping, but it is an exercise in what you can do         */</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$columns</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$alias</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$column</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
			&lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$employee</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$alias</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
	&lt;/tr&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
&lt;/table&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
&lt;div 
	style=&quot;
		background: #fcf; 
		color: #900; 
		border: solid 1px #900; 
		font-weight: bold; 
		padding: 10px 20px;&quot;
&gt;There are no employees on the list&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
&lt;/html&gt;
&lt;/body&gt;</pre></div></div>

<p></code></p>
<p>So now we need a mechanism to allow our users to download this data as an Excel spreadsheet. We could use pretty much anything that lets the page know we want to export (like a simple link) but for this exercise we are going to use a form. Why? No reason really, other than it is sometimes easier to tell a user to &#8220;push the button&#8221; instead of &#8220;click on the link that says &#8230;&#8221;.</p>
<p>As part of this we are also going to tie in a process that checks for the form button based export request, and if it is set, build the necessary data to allow the download. We do that like this:<br />
<code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Build a mock data set as though it were a database query result
 */</span>
<span style="color: #000088;">$employees</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Hans'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Auf'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1D'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1145'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Yung'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Ling'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'3C'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1220'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Mike'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Rowe'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'4E'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1232'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Joe'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Shmoe'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'2B'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1159'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Mary'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Smith'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'room'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1A'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1111'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Build a custom header array for column headings
 * 
 * NOTE[0]: You could have done this in the query by aliasing column names, but 
 * doing it this way gives us a little something more to learn. ;)
 * 
 * NOTE[1]: If you miss it the first time around, come back and look at this to
 * see how you can control the column display order with this array.
 */</span>
<span style="color: #000088;">$columns</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'fname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'First Name'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'lname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Last Name'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'room'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Office Address'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'ext'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Phone Number'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * If the export request was sent, handle that here
 */</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'export'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Set the field delimiter as a tab for Excel</span>
	<span style="color: #000088;">$delimiter</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Set the End Of Line character to force a new row</span>
	<span style="color: #000088;">$eol</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Start the spreadsheet by creating the header row based on our columns</span>
	<span style="color: #000088;">$sheet</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$delimiter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$columns</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/** 
	 * Create the body of the spreadsheet by way of a loop, looping the columns
	 * array inside of each employee iteration so that column order matches.
	 */</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$employees</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$employee</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Create a row string for building each row</span>
		<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Loop through the columns to make columnar data that matches the header</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$columns</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$row</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$employee</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$delimiter</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Pluck of the last tab and append a new line to the row</span>
		<span style="color: #000088;">$sheet</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">,</span> <span style="color: #000088;">$delimiter</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$eol</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Create the download file name - adding the date to make it a little unique</span>
	<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'employee-list-'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Ymd'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.xls'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Send the appropriate response headers </span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: application/x-msdownload'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: application/vnd.ms-excel'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Disposition: attachment; filename=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$filename</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009933; font-style: italic;">/** 
	 * Normally we would set our no-cache header here, but...
	 * Internet Explorer cannot handle SSL download saves if no-cache is set 
	 * anywhere. For most this would not be an issue, but if you are allowing
	 * exports to Excel over SSL then this will bite you in the butt when your 
	 * users are using IE
	 */</span>
	<span style="color: #666666; font-style: italic;">//header(&quot;Pragma: no-cache&quot;);</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: 0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Output the spreadsheet as a download</span>
	<span style="color: #b1b100;">print</span> <span style="color: #000088;">$sheet</span><span style="color: #339933;">;</span> <span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Company employee list&lt;/title&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;h1&gt;Our company employee list&lt;/h1&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">// If there are employees list them ?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$employees</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;form method=&quot;post&quot; action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; id=&quot;export-form&quot;&gt;
	&lt;!-- This will just be a simple button to trigger the export action --&gt;
	&lt;p&gt;
		&lt;input 
			type=&quot;submit&quot; 
			name=&quot;export&quot; 
			id=&quot;ef-export&quot; 
			value=&quot;Save this list as Excel&quot; 
		/&gt;
	&lt;/p&gt;
&lt;/form&gt;
&nbsp;
&lt;!-- Let's build our employee table --&gt;
&lt;table border=&quot;1&quot;&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">// Let's build the header row first ?&gt; </span>
	<span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$columns</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$column</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
		<span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$column</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
	<span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">// Now let's build the body ?&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$employees</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$employee</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
	&lt;tr&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* Let's make this a little dynamic... Yes, this is a lot of */</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* looping, but it is an exercise in what you can do         */</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$columns</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$alias</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$column</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
			&lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$employee</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$alias</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
	&lt;/tr&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
&lt;/table&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
&lt;div 
	style=&quot;
		background: #fcf; 
		color: #900; 
		border: solid 1px #900; 
		font-weight: bold; 
		padding: 10px 20px;&quot;
&gt;There are no employees on the list&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
&lt;/html&gt;
&lt;/body&gt;</pre></div></div>

<p></code></p>
<p>And there you have it. A simple, dynamic Excel exporter that will take your current data and dump it into a downloadable spreadsheet for your users.</p>
]]></content:encoded>
			<wfw:commentRss>http://phpbeginner.net/2009/11/08/simple-php-excel-exports/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maintaining processed form data</title>
		<link>http://phpbeginner.net/2009/10/30/maintaining-processed-form-data/</link>
		<comments>http://phpbeginner.net/2009/10/30/maintaining-processed-form-data/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 05:50:59 +0000</pubDate>
		<dc:creator>Robert Gonzalez</dc:creator>
				<category><![CDATA[Instruction]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://phpbeginner.net/?p=12</guid>
		<description><![CDATA[One of the questions that I see come up quite often from new developers is &#8220;How do I make it so that when someone enters something in a form and there is an error that the stuff they entered stays in the form?&#8221;. It is a perfectly logical question with an equally simple  solution [...]]]></description>
			<content:encoded><![CDATA[<p>One of the questions that I see come up quite often from new developers is &#8220;How do I make it so that when someone enters something in a form and there is an error that the stuff they entered stays in the form?&#8221;. It is a perfectly logical question with an equally simple  solution that many new developers might not immediately pick up on.</p>
<p>Keeping data entered in forms fresh boils down to keeping the posted form data inside of already declared variables so that, regardless of whether there is even a posted form, you can still display your form variables. But before we get into that, let&#8217;s create a simple HTML outline.</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>(Un)altered state<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p></code></p>
<p>So let&#8217;s say we want to take a simple form to collect a users name, gender, their favorite operating system and a brief explanation of why it is their favorite operating system. No, this is really not a practical example, other than the fact that we are going to be playing with a few different form elements, but it is acceptable for now.</p>
<p>So let&#8217;s build the form we are going to be using.<br />
<code></p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>(Un)altered state<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Tell us a little about yourself<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;collection-form&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Name: <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cf-name&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Gender: <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;radio&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;gender&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cf-gender-m&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;m&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>M <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;radio&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;gender&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cf-gender-f&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;f&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>F<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Favorite OS: <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;os&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cf-os&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span>&gt;</span>Please select one...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Tell us why:<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">textarea</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;why&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cf-why&quot;</span> <span style="color: #000066;">cols</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">rows</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;5&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">textarea</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cf-submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Let us know&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p></code></p>
<p>You&#8217;ll notice that in the markup we just posted that there is no PHP. We need to fix that, and at the same time add some dynamic features to our form like building our list items. But first we need to consider what type of data that we will be working with here. The &#8220;name&#8221; and &#8220;why&#8221; fields are pretty simple strings. But the &#8220;gender&#8221; and &#8220;OS&#8221; fields are a little more complex because they contain combination data. So for these we are going to be using arrays (and making the assumption that the data source for these arrays is a database result). To set up our &#8220;database results&#8221; we are going to be using the following arrays:</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$genderrows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'m'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'value'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'M'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'f'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'value'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'F'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$osrows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Micro$ost Winblows'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Proprietary Mac OSX'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Ubuntu, the best OS ever'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Some other crappy linux'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Unix'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'What\'s an operating system?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p></code></p>
<p>To make sure we can render our posted form values (or empty values if there is no posted form) we need to initialize some variables for use in the script. This is where the magic behind maintaining form state really comes into play. By doing this we are virtually guaranteed to have what was entered into the form in our possession for use in the form once again if we need it. And this is how we do it:</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$name</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>   ? <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$gender</span> <span style="color: #339933;">=</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'gender'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'gender'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$os</span>     <span style="color: #339933;">=</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'os'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>     ? <span style="color: #cc66cc;">0</span>    <span style="color: #339933;">:</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'os'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 0 because our list option values are numbers</span>
<span style="color: #000088;">$why</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'why'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>    ? <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'why'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p></code></p>
<p>If you look closely, what we managed to do here is capture all of the posted form data if there is any and, if there is no posted form data, default the variables to something (in our case, null) so we can still access those variables in our form. What this means is that we can now echo these variables as the values of our form elements regardless of whether we have posted form data.</p>
<p>And herein lies the solution to our challenge. By doing this we now have the data that was entered into the form. Which means if we choose to render the form as it was entered, all of the data entered into the form will still be in the form.</p>
<p>Our finished product looks like the below code, which you can copy and paste into a PHP file of your own, with any name, and run it from your local development environment (which is where all of your testing should take place <img src='http://phpbeginner.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ):<br />
<code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Start setting some default values for populating the form fields.
 * 
 * Name and why fields will be strings. Gender and OS fields will be an array
 * replicating a database query result. Regardless, their posted form values
 * will always be strings.
 */</span>
<span style="color: #000088;">$name</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>   ? <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$gender</span> <span style="color: #339933;">=</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'gender'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'gender'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$os</span>     <span style="color: #339933;">=</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'os'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>     ? <span style="color: #cc66cc;">0</span>    <span style="color: #339933;">:</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'os'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 0 because our list option values are numbers</span>
<span style="color: #000088;">$why</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'why'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>    ? <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'why'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/**
 * Wait, what? Why all those question marks? That is the ternary operator in 
 * PHP and it acts as a sort of simple short hand if/else statement. Read more
 * on the ternary operator at:
 * http://php.net/ternary#language.operators.comparison.ternary
 */</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Now we need to make some arrays for our more complex data types
 */</span>
<span style="color: #000088;">$genderrows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'m'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'value'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'M'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'f'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'value'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'F'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$osrows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Micro$ost Winblows'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Proprietary Mac OSX'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Ubuntu, the best OS ever'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Some other crappy linux'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Unix'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'What\'s an operating system?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;(Un)altered state&lt;/title&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;h1&gt;Tell us a little about yourself&lt;/h1&gt;
&lt;form method=&quot;post&quot; action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; id=&quot;collection-form&quot;&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* Echo the $name var as the default value of this field */</span>       <span style="color: #000000; font-weight: bold;">?&gt;</span> 
	&lt;!-- Name --&gt;
	&lt;p&gt;Name: &lt;input type=&quot;text&quot; id=&quot;cf-name&quot; name=&quot;name&quot; size=&quot;50&quot; value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$name</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;&lt;/p&gt;
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* While looping the genders list, see if there is a match to */</span>   <span style="color: #000000; font-weight: bold;">?&gt;</span> 
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* the entered data and select it if there is */</span>                   <span style="color: #000000; font-weight: bold;">?&gt;</span> 
	&lt;!-- Gender --&gt;
	&lt;p&gt;Gender: <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$genderrows</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$genderrow</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
		&lt;input 
			type=&quot;radio&quot; 
			name=&quot;gender&quot; 
			id=&quot;cf-gender-<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$genderrow</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'label'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; 
			value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$genderrow</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'label'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$gender</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$genderrow</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'label'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>checked=&quot;checked&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
		 /&gt; 
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$genderrow</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* Same as the genders list, matching while looping */</span>             <span style="color: #000000; font-weight: bold;">?&gt;</span> 
	&lt;!-- Operating System --&gt;
	&lt;p&gt;Favorite OS: &lt;select name=&quot;os&quot; id=&quot;cf-os&quot;&gt;
		&lt;option value=&quot;0&quot;&gt;Please select one...&lt;/option&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$osrows</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$osrow</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
		&lt;option 
			value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$osrow</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$osrow</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$os</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>selected=&quot;selected&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
		&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$osrow</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'label'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/option&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/select&gt;&lt;/p&gt;
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* Echo the $why var as the default value of this textarea */</span>       <span style="color: #000000; font-weight: bold;">?&gt;</span> 
	&lt;!-- Explanation --&gt;
	&lt;p&gt;Tell us why:&lt;br /&gt;&lt;textarea name=&quot;why&quot; id=&quot;cf-why&quot; cols=&quot;50&quot; rows=&quot;5&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$why</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/textarea&gt;&lt;/p&gt;
&nbsp;
	&lt;!-- Button --&gt;
	&lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;submit&quot; id=&quot;cf-submit&quot; value=&quot;Let us know&quot; /&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;!-- Quick view of what was posted --&gt;
&lt;!-- View source to it
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
--&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p></code></p>
<p>And there you have it. A completed form that maintains what was entered by the user. Try it for yourself and see what it does. See if you can spot how it does it. And start to think of ways to use this logic in your own code to allow for a more robust user experience for your users.</p>
<p><strong>Note: Do not use this code as is in your production environment. There is no validation or filtration on any of the posted form values. This leaves you wide open to malicious attacks by would be bad guys. This tutorial was only for the sake of learning a simple method to maintain form state. Please treat it as that&#8230; simple.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://phpbeginner.net/2009/10/30/maintaining-processed-form-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP for anyone</title>
		<link>http://phpbeginner.net/2008/08/13/hello-world/</link>
		<comments>http://phpbeginner.net/2008/08/13/hello-world/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 18:02:45 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Information]]></category>

		<guid isPermaLink="false">http://phpbeginner.net/?p=1</guid>
		<description><![CDATA[Welcome to PHPBeginner.net, home of the n00B PHP developer manual and instruction guide! The goal of this site is to provide entry level instruction to developers that are looking to get into PHP development. No assumption of current programming knowledge is made, but we do assume you know your way around (X)HTML. We believe anyone, [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to PHPBeginner.net, home of the n00B PHP developer manual and instruction guide! The goal of this site is to provide entry level instruction to developers that are looking to get into PHP development. No assumption of current programming knowledge is made, but we do assume you know your way around (X)HTML. We believe anyone, and we mean anyone, should be able to start right along with us.</p>
<p>If you are completely new to web development or programming in general we recommend you start at the beginning and work your way through. If you are already a programmer and are looking at picking up PHP to add to your mad skillset, start where you will. It should be fairly easy to get up to speed with how we do things.</p>
<p>We hope you enjoy this site and the content we bring you. If you have any questions, please visit the contact page to let us know what is on your mind.</p>
]]></content:encoded>
			<wfw:commentRss>http://phpbeginner.net/2008/08/13/hello-world/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

