<?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>Jeremy Smyth's Blog &#187; PHP</title>
	<atom:link href="http://jeremysmyth.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://jeremysmyth.com</link>
	<description></description>
	<lastBuildDate>Sun, 04 Jul 2010 07:10:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Regular Expressions and Numeric Comparisons</title>
		<link>http://jeremysmyth.com/2010/03/12/regular-expressions-and-numeric-comparisons/</link>
		<comments>http://jeremysmyth.com/2010/03/12/regular-expressions-and-numeric-comparisons/#comments</comments>
		<pubDate>Fri, 12 Mar 2010 19:43:32 +0000</pubDate>
		<dc:creator>Jeremy Smyth</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://jeremysmyth.com/?p=213</guid>
		<description><![CDATA[So let&#8217;s say you&#8217;re parsing through order numbers (as strings):


OD1004A
OD1004B
OD1108A
OE1108B
OE1108C
OE1109A
OE1148A
OE1149A
OE1151A


&#8230;and so on. And you want to find orders where the numeric portion is between 1100 and 1150, regardless of what the rest is.
Tempting as it is, this isn&#8217;t actually a regular expression problem, but requires a bit of numeric processing too.
We would need to parse [...]]]></description>
			<content:encoded><![CDATA[<p>So let&#8217;s say you&#8217;re parsing through order numbers (as strings):</p>
<pre>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;">OD1004A
OD1004B
OD1108A
OE1108B
OE1108C
OE1109A
OE1148A
OE1149A
OE1151A</div>
</div>
</pre>
<p>&#8230;and so on. And you want to find orders where the numeric portion is between 1100 and 1150, regardless of what the rest is.</p>
<p>Tempting as it is, this isn&#8217;t actually a regular expression problem, but requires a bit of numeric processing too.</p>
<p>We would need to parse the digits and then perform numeric comparison, e.g.:</p>
<pre>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="re0">$input</span> <span class="sy0">=</span> whatever<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co2"># gets something like &quot;OE1105A&quot;</span></div>
</div>
</pre>
<p>&#8230;then match with the following pattern:</p>
<pre>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="st0">&quot;/\w+(\d+)\w/&quot;</span><span class="sy0">,</span> <span class="re0">$input</span><span class="sy0">,</span> <span class="re0">$match</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
</pre>
<p>&#8230;to store the digits in memory, and then:</p>
<pre>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$match</span><span class="br0">&#91;</span>1<span class="br0">&#93;</span> <span class="sy0">&gt;=</span> 1100 and <span class="re0">$match</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> <span class="sy0">&lt;=</span> <span class="nu0">1150</span><span class="br0">&#41;</span><span class="br0">&#123;</span><span class="sy0">...</span></div>
</div>
</pre>
<p>to check to see if the number is in range.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeremysmyth.com/2010/03/12/regular-expressions-and-numeric-comparisons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: require and include</title>
		<link>http://jeremysmyth.com/2009/10/30/php-require-and-include/</link>
		<comments>http://jeremysmyth.com/2009/10/30/php-require-and-include/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 06:36:43 +0000</pubDate>
		<dc:creator>Jeremy Smyth</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jeremysmyth.com/?p=211</guid>
		<description><![CDATA[Another simple feature that&#8217;s good to pick up on!
When you want to run the same functions from several pages, but don&#8217;t want to copy/paste (because that&#8217;s BAD), you do this.
Usually, you&#8217;d use require or include to include the code from one PHP script in another.
If you hadn&#8217;t done this before, or designed it in, you&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>Another simple feature that&#8217;s good to pick up on!</p>
<p>When you want to run the same functions from several pages, but don&#8217;t want to copy/paste (because that&#8217;s BAD), you do this.</p>
<p>Usually, you&#8217;d use require or include to include the code from one PHP script in another.</p>
<p>If you hadn&#8217;t done this before, or designed it in, you&#8217;ll probably need to extract the functions from your script(s), place them in a central file, and include that in all scripts that need it (including, of course, the one they were originally in!). Remember that includeing a file in another causes its embedded HTML to be included too, so you&#8217;ll typically include PHP files that have functions only, to achieve the desired result.</p>
<p>Then you just call the function, as if it were written in the script in the first place <img src='http://jeremysmyth.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeremysmyth.com/2009/10/30/php-require-and-include/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Representing Hierarchical data in PHP</title>
		<link>http://jeremysmyth.com/2009/08/28/representing-hierarchical-data-in-php/</link>
		<comments>http://jeremysmyth.com/2009/08/28/representing-hierarchical-data-in-php/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 17:41:42 +0000</pubDate>
		<dc:creator>Jeremy Smyth</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://jeremysmyth.com/?p=156</guid>
		<description><![CDATA[I&#8217;ve come across a lot of people having a problem when representing data from their databases; hierarchical data provides us with a particular problem when displaying the relationships and containers required.
An example of hierarchical data
As an example, let&#8217;s say you have forums within categories. The category &#8220;Web Programming&#8221; may have forums like &#8220;PHP&#8221;, &#8220;JavaScript&#8221; and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve come across a lot of people having a problem when representing data from their databases; hierarchical data provides us with a particular problem when displaying the relationships and containers required.</p>
<h2>An example of hierarchical data</h2>
<p>As an example, let&#8217;s say you have forums within categories. The category &#8220;Web Programming&#8221; may have forums like &#8220;PHP&#8221;, &#8220;JavaScript&#8221; and so on, so we want to list each forum <em>under</em> each category. The problem here is that you&#8217;ll have:
<ul>
<li>a table for categories
<li>one for forums (with a foreign key for categories)
<li>one for posts, with a foreign key for forums
<li>and probably one for comments.
</ul>
<p>This fits quite neatly into a hierarchy: each comment belongs to a post, which belongs to a forum, which belongs to a category.</p>
<h2>Using multiple resultsets</h2>
<p>If you want to display the Categories with Forums under them in your page, one way to do this is by setting up multiple resultsets, each focusing on one level of the hierarchy. To achieve this, you&#8217;d need to get a resultset for the categories first, and then iterate over this list with another loop for your forum list.</p>
<p>The following code is an example of iterating using <em>nested queries</em>:</p>
<pre>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="re0">$cat_rs</span> <span class="sy0">=</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&quot;select id, name from categories&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">while</span><span class="br0">&#40;</span><span class="re0">$cat_row</span> <span class="sy0">=</span> <a href="http://www.php.net/mysql_fetch_array"><span class="kw3">mysql_fetch_array</span></a><span class="br0">&#40;</span><span class="re0">$cat_rs</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp;<span class="co1">// print category name from $cat_row[1]</span>
&nbsp; &nbsp; &nbsp;<span class="re0">$forum_rs</span> <span class="sy0">=</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&quot;select name... &quot;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">.</span> <span class="st0">&quot;from forums &quot;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">.</span> <span class="st0">&quot;where cat_id = '&quot;</span> <span class="sy0">.</span> <span class="re0">$cat_row</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">.</span><span class="st0">&quot;'&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp;<span class="kw1">while</span><span class="br0">&#40;</span><span class="re0">$forum_row</span> <span class="sy0">=</span> <a href="http://www.php.net/mysql_fetch_array"><span class="kw3">mysql_fetch_array</span></a><span class="br0">&#40;</span><span class="re0">$forum_rs</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//print forum stuff</span>
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>Although this matches the hierarchical nature of the data, and is pretty straightforward and intuitive to understand, it is somewhat inefficient; it issues a select statement for each category, plus one to list the categories in the first place. If there are many categories, the page might take some time to load as all queries are issued and processed.</p>
<h2>Using a SQL <tt>JOIN</tt>: a single resultset</h2>
<p>Another way to solve the problem would be to retrieve all relevant data in a <em>single resultset</em>, and use PHP to iterate over the rows, deciding on when to print each individual section.</p>
<p>A <tt>join</tt> will give you a single recordset with the category in one column, and the forum in another, giving you many rows for a single category.</p>
<pre>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="re0">$join_rs</span> <span class="sy0">=</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&quot;select c.id, c.name, f.name,... &quot;</span> 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">.</span> <span class="st0">&quot;from categories c inner join forums f &quot;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">.</span> <span class="st0">&quot;on c.id = f.cat_id &quot;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">.</span> <span class="st0">&quot;order by c.id&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// this line is crucial</span>
<span class="re0">$current_category</span> <span class="sy0">=</span> <span class="st0">&quot;&quot;</span><span class="sy0">;</span>
<span class="kw1">while</span><span class="br0">&#40;</span><span class="re0">$join_row</span> <span class="sy0">=</span> <a href="http://www.php.net/mysql_fetch_array"><span class="kw3">mysql_fetch_array</span></a><span class="br0">&#40;</span><span class="re0">$join_rs</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$join_row</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span> <span class="sy0">!=</span> <span class="re0">$current_category</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//set up new category headings here</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$current_category</span> <span class="sy0">=</span> <span class="re0">$join_row</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//print the forum stuff in the current category</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>The PHP is a bit more complex, because you have to close divs or tables correctly, within the loop. The <tt>order by</tt> line in the above example is vital, because without it, the categories may be dotted throughout the resultset. As we want all forum data related to a single category to appear together, we want rows belonging to that category to be adjacent in the resultset. The <tt>order by</tt> clause achieves this.</p>
<p>Despite the complexity, there are still benefits to this approach. The earlier example was simple and intuitive, using nested queries for each parent element. On the other hand, in this example we&#8217;ve only issued one <tt>select</tt> statement, so the page is likely to load more quickly, given that the number of round trips to MySQL will be substantially fewer.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeremysmyth.com/2009/08/28/representing-hierarchical-data-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Handling Parameter Arrays from Forms</title>
		<link>http://jeremysmyth.com/2009/08/25/php-handling-parameter-arrays-from-forms/</link>
		<comments>http://jeremysmyth.com/2009/08/25/php-handling-parameter-arrays-from-forms/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 10:56:15 +0000</pubDate>
		<dc:creator>Jeremy Smyth</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jeremysmyth.com/?p=114</guid>
		<description><![CDATA[Sometimes, when processing a form, you need to retrieve a number of values for a single variable. This is often a cause for confusion when beginning PHP. The particular problem faced: how do we process multiple values coming from a form to a databse, when using PHP?
Imagine we had a drop-down box containing a number [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, when processing a form, you need to retrieve a number of values for a single variable. This is often a cause for confusion when beginning PHP. The particular problem faced: how do we process multiple values coming from a form to a databse, when using PHP?</p>
<p>Imagine we had a drop-down box containing a number of elements, and we want the user to select one or more. We&#8217;d use the <tt>select</tt> element in HTML.</p>
<p>
<pre>
<div class="codesnip-container" >
<div class="html4strict codesnip" style="font-family:monospace;"><span class="sc2">&lt;<a href="http://december.com/html/4/element/select.html"><span class="kw2">select</span></a> <span class="kw3">name</span><span class="sy0">=</span><span class="st0">&quot;taglist[]&quot;</span> <span class="kw3">multiple</span><span class="sy0">=</span><span class="st0">&quot;multiple&quot;</span>&gt;</span></div>
</div>
</pre>
<p><p>Note the square brackets: for parameter arrays &#8212; parameters that allow multiple values &#8212; PHP requires that you name a GET or POST variable accordingly, so the <tt>select</tt> above needs to be named <tt>taglist[]</tt> in order to tell PHP that it will contain an array rather than a single value.</p>
<p>In order to process this, we then have to iterate over that array.</p>
<p>To iterate over the array in PHP code, we use something like this:</p>
<pre>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$_POST</span><span class="br0">&#91;</span><span class="st0">&quot;taglist[]&quot;</span><span class="br0">&#93;</span> <span class="kw1">as</span> <span class="re0">$s</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&quot;INSERT INTO articlestagged (article_id, tag) &quot;</span> <span class="sy0">.</span>
&nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&quot;VALUES ('<span class="es4">$id</span>','<span class="es4">$s</span>')&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; or <a href="http://www.php.net/die"><span class="kw3">die</span></a><span class="br0">&#40;</span><span class="st0">&quot;Insert Error: &quot;</span><span class="sy0">.</span><a href="http://www.php.net/mysql_error"><span class="kw3">mysql_error</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>Obviously this example relates to adding new entries to a database, but the same would apply if we were querying a database, displaying multiple images, or indeed anywhere else we needed to operate on multiple concurrent values.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeremysmyth.com/2009/08/25/php-handling-parameter-arrays-from-forms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
