<?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/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
>

<channel>
	<title>ITauthor &#187; PHP</title>
	<atom:link href="http://www.itauthor.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.itauthor.com</link>
	<description>Stuff about technical writing and software</description>
	<lastBuildDate>Thu, 29 Jul 2010 07:27:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<!-- podcast_generator="Blubrry PowerPress/1.0.8" mode="advanced" entry="normal" -->
	<itunes:summary>Talking about technical writing, software and technology in general. The ITauthor Podcast is an advert-free, irregularly published show by technical writers for technical writers or anyone interested in software documentation or IT generally.</itunes:summary>
	<itunes:author>Alistair Christie - ITauthor.com</itunes:author>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://www.itauthor.com/images/ITauthor-PhotoLogo-300px.jpg" />
	<itunes:owner>
		<itunes:name>Alistair Christie - ITauthor.com</itunes:name>
		<itunes:email>comments@itauthor.com</itunes:email>
	</itunes:owner>
	<managingEditor>comments@itauthor.com (Alistair Christie - ITauthor.com)</managingEditor>
	<copyright>2006-2009</copyright>
	<itunes:subtitle>Talking about technical writing, software and technology in general.</itunes:subtitle>
	<itunes:keywords>itauthor, alistair christie, technology, writing, documentation </itunes:keywords>
	<image>
		<title>ITauthor &#187; PHP</title>
		<url>http://www.itauthor.com/images/ITauthor-PhotoLogo-144px.jpg</url>
		<link>http://www.itauthor.com/category/php/</link>
	</image>
	<itunes:category text="Technology">
		<itunes:category text="Software How-To" />
		<itunes:category text="Tech News" />
		<itunes:category text="Podcasting" />
	</itunes:category>
		<item>
		<title>PHP: swapping round columns &amp; rows in field/record data</title>
		<link>http://www.itauthor.com/2010/07/20/php-swapping-round-columns-rows-in-fieldrecord-data/</link>
		<comments>http://www.itauthor.com/2010/07/20/php-swapping-round-columns-rows-in-fieldrecord-data/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 18:09:22 +0000</pubDate>
		<dc:creator>ac</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.itauthor.com/2010/07/20/php-swapping-round-columns-rows-in-fieldrecord-data/</guid>
		<description><![CDATA[I'm blogging this just because I couldn't find a solution via Google so I had to figure this out myself and it might save you some time if you're trying to do the same thing. I have some PHP script that grabs some data out of a MySQL table. However, rather than display it in [...]]]></description>
			<content:encoded><![CDATA[<p>I'm blogging this just because I couldn't find a solution via Google so I had to figure this out myself and it might save you some time if you're trying to do the same thing.</p>  <p>I have some PHP script that grabs some data out of a MySQL table. However, rather than display it in the normal way:</p>  <table border="0" cellspacing="0" cellpadding="2" width="433"><tbody>     <tr>       <td valign="top" width="80"><strong>resultField1</strong></td>        <td valign="top" width="80"><strong>resultField2</strong></td>        <td valign="top" width="80"><strong>resultField3</strong></td>        <td valign="top" width="80"><strong>resultField4</strong></td>        <td valign="top" width="111"><strong>resultField5</strong></td>     </tr>      <tr>       <td valign="top" width="80">2010-12-24</td>        <td valign="top" width="80">55</td>        <td valign="top" width="80">red</td>        <td valign="top" width="80">1</td>        <td valign="top" width="111">Miles Davis</td>     </tr>      <tr>       <td valign="top" width="80">2011-03-30</td>        <td valign="top" width="80">65</td>        <td valign="top" width="80">yellow</td>        <td valign="top" width="80">0</td>        <td valign="top" width="111">Kevin Coyne</td>     </tr>      <tr>       <td valign="top" width="80">2011-06-16</td>        <td valign="top" width="80">82</td>        <td valign="top" width="80">purple</td>        <td valign="top" width="80">1</td>        <td valign="top" width="111">Kate Bush</td>     </tr>   </tbody></table>  <p>I want to swap round the rows and columns so that I display it like this:</p>  <table border="0" cellspacing="0" cellpadding="2" width="433"><tbody>     <tr>       <td valign="top" width="80"><strong>resultField1</strong></td>        <td valign="top" width="80">2010-12-24</td>        <td valign="top" width="81">2011-03-30</td>        <td valign="top" width="79">2011-06-16</td>     </tr>      <tr>       <td valign="top" width="80"><strong>resultField2</strong></td>        <td valign="top" width="80">55</td>        <td valign="top" width="81">65</td>        <td valign="top" width="79">82</td>     </tr>      <tr>       <td valign="top" width="80"><strong>resultField3</strong></td>        <td valign="top" width="80">red</td>        <td valign="top" width="81">yellow</td>        <td valign="top" width="79">purple</td>     </tr>      <tr>       <td valign="top" width="80"><strong>resultField4</strong></td>        <td valign="top" width="80">1</td>        <td valign="top" width="81">0</td>        <td valign="top" width="79">1</td>     </tr>      <tr>       <td valign="top" width="80"><strong>resultField5</strong></td>        <td valign="top" width="80">Miles Davis</td>        <td valign="top" width="81">Kevin Coyne</td>        <td valign="top" width="79">Kate Bush</td>     </tr>   </tbody></table>  <p>The thing to be aware of is that when you use mysql_fetch_assoc to get stuff out of a database, for example: </p> <code>while($resultArray = mysql_fetch_assoc($selectQuery)) {   <br />&#160;&#160;&#160; <i>Do stuff here ...</i>    <br />}</code>  <p>   <br />what you get back is an associative array (that's a hash for folks like me who learnt this stuff in Perl before arriving at PHP). So in the above example $resultArray is an associative array - that is, a set of key/value pairs where the key is the field name and the value is the value in that field for that record. Each iteration through the while loop you get a different record.</p>  <p>So, in effect, it's like an array of associative arrays.</p>  <p>However, what you want in order to be able to swap round fields and columns is an associative array of arrays - that is, a set of key/value pairs where each key is a field name and each value is an array of the values for that field in each record in the data set. So what you need to do is:</p>  <ul>   <li>Iterate through each record in the data set (i.e. each row in your results) .</li>    <li>For each field in the record, assign the field value to an array, a reference to which is the value of the key/value pair in the outer associative array.</li> </ul>  <p>If it sounds complicated, then I'm glad I'm not the only one. Actually, it's probably easier to understand by looking at the code:</p>  <p><a title="http://www.itauthor.com/wp-content/uploads/2010/07/assocArrayOfArrays-example-PrettyPrinting.html" href="http://www.itauthor.com/wp-content/uploads/2010/07/assocArrayOfArrays-example-PrettyPrinting.html">assocArrayOfArrays-example-PrettyPrinting.html</a> - This is a syntax-highlighted representation of the PHP.    <br /><a title="http://www.itauthor.com/wp-content/uploads/2010/07/assocArrayOfArrays-example.php.txt" href="http://www.itauthor.com/wp-content/uploads/2010/07/assocArrayOfArrays-example.php.txt">assocArrayOfArrays-example.php.txt</a> - This is the actual PHP saved as a .txt file so that you can see and download it.</p>  <p>And here's the PHP in action:   <br /><a title="http://www.itauthor.com/wp-content/uploads/2010/07/assocArrayOfArrays-example.php" href="http://www.itauthor.com/wp-content/uploads/2010/07/assocArrayOfArrays-example.php">assocArrayOfArrays-example.php</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.itauthor.com/2010/07/20/php-swapping-round-columns-rows-in-fieldrecord-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP (or Perl) one line if/then/else statements</title>
		<link>http://www.itauthor.com/2009/08/08/php-or-perl-one-line-ifthenelse-statements/</link>
		<comments>http://www.itauthor.com/2009/08/08/php-or-perl-one-line-ifthenelse-statements/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 06:53:35 +0000</pubDate>
		<dc:creator>ac</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.itauthor.com/2009/08/08/php-or-perl-one-line-ifthenelse-statements/</guid>
		<description><![CDATA[If you're toggling something between two states in PHP or Perl it's often handy to use an if/then/else one liner. In pseudocode this goes like this: &#60;if this evaluates to TRUE&#62; then &#60;parse this&#62; else &#60;parse this&#62; All you need to do is replace the &#34;then&#34; with a question mark and the &#34;else&#34; with a [...]]]></description>
			<content:encoded><![CDATA[<p>If you're toggling something between two states in PHP or Perl it's often handy to use an if/then/else one liner.</p>  <p>In pseudocode this goes like this:</p>  <p><em>&lt;<strong>if </strong>this evaluates to TRUE&gt; </em><strong>then </strong><em>&lt;parse this&gt; </em><strong>else </strong><em>&lt;parse this&gt;</em></p>  <p>All you need to do is replace the &quot;then&quot; with a question mark and the &quot;else&quot; with a colon:</p>  <p><em>&lt;<strong>if </strong>this evaluates to TRUE&gt; </em><strong>? </strong><em>&lt;parse this&gt; </em><strong>:</strong><strong> </strong><em>&lt;parse this&gt;</em></p>  <p>For example:</p>  <p><span style="color: #c0c0c0"><em>print</em>&#160; </span>$trueOrFalse ? &quot;you're telling the truth&quot; : &quot;you're lying&quot;;</p>  <p>Ignore the print command, it's not part of the if/then/else statement, it's just here to do something with the outcome of that statement.</p>  <p>The expression immediately to the left of the question mark is evaluated. The expression between the question mark and the colon is parsed if the expression evaluates to TRUE, otherwise the expression immediately to the right of the colon is parsed. So in the above example, either &quot;you're telling the truth&quot; or &quot;you're lying&quot; is printed, depending on whether $trueOrFalse is ... you guessed it ... TRUE or FALSE.</p>  <p>But perhaps a more common situation is toggling the value assigned to a variable. For example, toggling between TRUE and FALSE:</p>  <p><span style="color: #c0c0c0"><em>$trueOrFalse = </em></span>$trueOrFalse ? FALSE : TRUE;</p>  <p>Here's a practical example of the use of if/then/else one liners. There's two in this chunk of PHP. The scroll box list below the code is the kind of thing this PHP produces.</p>  <p>&lt;div style=&quot;overflow:auto; height:100px; width:300px; border:3px groove #DDD; padding:0&quot;&gt;    <br />&lt;?php     <br />&#160;&#160;&#160; $alternateLine = FALSE;     <br />&#160;&#160;&#160; while($presidentsArray) {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; print &quot;&lt;div style=\&quot;background-color:&quot;;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; print $alternateLine ? &quot;#F5F8F9&quot; : &quot;white&quot;;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; print &quot;; padding-bottom: 1px\&quot;&gt; &amp;nbsp; &amp;nbsp; &lt;a href=\&quot;someURL\&quot; title=\&quot;This link goes nowhere\&quot;&gt;&quot; .     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $presidentsArray['name'] . &quot;&lt;/a&gt;&lt;/div&gt;&quot;;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $alternateLine = $alternateLine ? FALSE : TRUE;     <br />&#160;&#160;&#160; }     <br />?&gt;     <br />&lt;/div&gt;</p>  <div style="border-bottom: #dddddd 3px groove; border-left: #dddddd 3px groove; padding-bottom: 0pt; padding-left: 0pt; width: 300px; padding-right: 0pt; height: 100px; overflow: auto; border-top: #dddddd 3px groove; border-right: #dddddd 3px groove; padding-top: 0pt">   <div style="padding-bottom: 1px; background-color: white">&#160;&#160;&#160; <a title="This link goes nowhere" href="someURL">George Washington</a></div>    <div style="padding-bottom: 1px; background-color: #f5f8f9">&#160;&#160;&#160; <a title="This link goes nowhere" href="someURL">John Adams</a></div>    <div style="padding-bottom: 1px; background-color: white">&#160;&#160;&#160; <a title="This link goes nowhere" href="someURL">Thomas Jefferson</a></div>    <div style="padding-bottom: 1px; background-color: #f5f8f9">&#160;&#160;&#160; <a title="This link goes nowhere" href="someURL">James Madison</a></div>    <div style="padding-bottom: 1px; background-color: white">&#160;&#160;&#160; <a title="This link goes nowhere" href="someURL">James Monroe</a></div>    <div style="padding-bottom: 1px; background-color: #f5f8f9">&#160;&#160;&#160; <a title="This link goes nowhere" href="someURL">John Quincy Adams</a></div>    <div style="padding-bottom: 1px; background-color: white">&#160;&#160;&#160; <a title="This link goes nowhere" href="someURL">Andrew Jackson</a></div>    <div style="padding-bottom: 1px; background-color: #f5f8f9">&#160;&#160;&#160; <a title="This link goes nowhere" href="someURL">Martin Van Buren</a></div>    <div style="padding-bottom: 1px; background-color: white">&#160;&#160;&#160; <a title="This link goes nowhere" href="someURL">William Henry Harrison</a></div>    <div style="padding-bottom: 1px; background-color: #f5f8f9">&#160;&#160;&#160; <a title="This link goes nowhere" href="someURL">John Tyler</a></div> </div>]]></content:encoded>
			<wfw:commentRss>http://www.itauthor.com/2009/08/08/php-or-perl-one-line-ifthenelse-statements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
