<?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>Design, Development, and Consulting &#187; PHP</title>
	<atom:link href="http://netlumination.com/blog/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://netlumination.com</link>
	<description>A web design, development, and consulting firm based in Portland, Oregon helping small businesseses and individuals.</description>
	<lastBuildDate>Wed, 01 Sep 2010 23:27:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A recursive PHP function to turn digits into words</title>
		<link>http://netlumination.com/blog/a-recursive-php-function-to-turn-digits-into-words</link>
		<comments>http://netlumination.com/blog/a-recursive-php-function-to-turn-digits-into-words#comments</comments>
		<pubDate>Wed, 31 Mar 2010 21:16:21 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[recursion]]></category>

		<guid isPermaLink="false">http://netlumination.com/?p=635</guid>
		<description><![CDATA[Warning: This is a big spoiler for Project Euler Problem 17, so if you&#8217;re still working on it, don&#8217;t keep reading! I quickly mentioned recursion in my previous post. Turning digits into letters is clearly another example for which recursion is useful. Think about a number like 524. If you know that 4 = &#8220;four&#8221;, [...]]]></description>
			<content:encoded><![CDATA[<p>Warning: This is a big spoiler for Project Euler Problem 17, so if you&#8217;re still working on it, don&#8217;t keep reading!</p>
<p>I quickly mentioned recursion in <a href="http://netlumination.com/blog/quick-note-on-recursion">my previous post</a>. Turning digits into letters is clearly another example for which recursion is useful. Think about a number like 524. If you know that 4 = &#8220;four&#8221;, 5 = &#8220;five&#8221;,  and that 20 = &#8220;twenty&#8221; then all you have to do, to turn 524 into words,  is run your script on 5, add &#8220;hundred,&#8221; run your script on 20, and run your script on 4, and if you write your script well, then you can do this using recursion.</p>
<p>Take a look at a working example of <a href="http://peter-ajtai.com/examples/numbers.php">the function I wrote</a> to do just this with PHP.</p>
<p>Take a look at <a href="http://peter-ajtai.com/euler/problems/Problem-017.php">my solution to Project Euler Problem 17</a> [SPOILER ALERT]</p>
]]></content:encoded>
			<wfw:commentRss>http://netlumination.com/blog/a-recursive-php-function-to-turn-digits-into-words/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick Note on Recursion</title>
		<link>http://netlumination.com/blog/quick-note-on-recursion</link>
		<comments>http://netlumination.com/blog/quick-note-on-recursion#comments</comments>
		<pubDate>Mon, 29 Mar 2010 23:44:27 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[recursion]]></category>

		<guid isPermaLink="false">http://netlumination.com/?p=626</guid>
		<description><![CDATA[Recursion Example in PHP using the factorial: // $n must be a positive integer or 0 - function returns 1 for 0 and all errors function factorial($n) { if ($n &#62; 0 &#38;&#38; is_int($n)) { // factorial of positive integer return $n * factorial($n - 1); } else { // factorial of 0 return 1; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Recursion_(computer_science)">Recursion</a></p>
<p>Example in PHP using the factorial:</p>
<ol>
<li>
<pre>// $n must be a positive integer or 0 - function returns 1 for 0 and all errors</pre>
</li>
<li>
<pre>function factorial($n)</pre>
</li>
<li>
<pre>{</pre>
</li>
<li>
<pre>  if ($n &gt; 0 &amp;&amp; is_int($n))</pre>
</li>
<li>
<pre>  {</pre>
</li>
<li>
<pre>    // factorial of positive integer</pre>
</li>
<li>
<pre>    return $n * factorial($n - 1);</pre>
</li>
<li>
<pre>  } else</pre>
</li>
<li>
<pre>  {</pre>
</li>
<li>
<pre>    // factorial of 0</pre>
</li>
<li>
<pre>    return 1;</pre>
</li>
<li>
<pre>  }</pre>
</li>
<li>
<pre>}</pre>
</li>
</ol>
<p>There are many other blog posts that cover this topic well. Some include:</p>
<p><a href="http://devzone.zend.com/article/1235">Recursion in PHP (and iteration)</a></p>
<p><a href="http://php.net/manual/en/regexp.reference.recursive.php">Recursive patterns in Regex from the PHP manual</a></p>
<p>Here is a little snippet of a C++ function that will ask for user input and make sure it is a float. If it&#8217;s not a float, the functions recurses (is that a word?)&#8230; calls itself. The end effect is that the user is asked to enter a float until they do. You must include both iostream and limits.</p>
<ol>
<li>
<pre>float collectFloat()</pre>
</li>
<li>
<pre>{</pre>
</li>
<li>
<pre>  float user_input(0.0);</pre>
</li>
<li>
<pre>  try</pre>
</li>
<li>
<pre>  {</pre>
</li>
<li>
<pre>    // Could also use cin.fail() to check for success</pre>
</li>
<li>
<pre>    if (cin &gt;&gt; user_input) cinIgnoreRest();</pre>
</li>
<li>
<pre>    else</pre>
</li>
<li>
<pre>      throw (string(“\nNot a float entered.\n Please enter a number.\n”));</pre>
</li>
<li>
<pre>  }</pre>
</li>
<li>
<pre>  catch (string error)</pre>
</li>
<li>
<pre>  {</pre>
</li>
<li>
<pre>    cout &lt;&lt; error &lt;&lt; “==&gt; “;</pre>
</li>
<li>
<pre>    cin.clear();</pre>
</li>
<li>
<pre>    cinIgnoreRest();</pre>
</li>
<li>
<pre>    // This will keep repeating until user enters a float</pre>
</li>
<li>
<pre>    user_input = collectFloat();</pre>
</li>
<li>
<pre>  }</pre>
</li>
<li>
<pre>    return user_input;</pre>
</li>
<li>
<pre>}</pre>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://netlumination.com/blog/quick-note-on-recursion/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Multiple Loops in WordPress with query_posts() and get_categories()</title>
		<link>http://netlumination.com/blog/using-multiple-loops-in-wordpress-with-query_posts-and-get_categories</link>
		<comments>http://netlumination.com/blog/using-multiple-loops-in-wordpress-with-query_posts-and-get_categories#comments</comments>
		<pubDate>Tue, 05 May 2009 20:12:53 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Category]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[get_categories]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[query_posts]]></category>
		<category><![CDATA[Template]]></category>
		<category><![CDATA[The Loop]]></category>

		<guid isPermaLink="false">http://netlumination.com/?p=390</guid>
		<description><![CDATA[This is a tutorial to demonstrate how to show entire posts from multiple categories in WordPress. This would be especially useful for blogs that contain very short posts like a photo with a short description. This is a very simple problem and solution, but I didn&#8217;t find many posts describing how to do it, so [...]]]></description>
			<content:encoded><![CDATA[<p>This is a tutorial to demonstrate how to show entire posts from multiple categories in WordPress. This would be especially useful for blogs that contain very short posts like a photo with a short description. This is a very simple problem and solution, but I didn&#8217;t find many posts describing how to do it, so I thought it may help a few people out.</p>
<p>In some blogs, it can be useful to show entire posts from multiple categories on one page. For instance for a recipe blog you may want to list the name of each of your recipe categories and the recipes in each of those categories. This would look something like:</p>
<ul>
<li>Breakfast Recipes
<ul>
<li>[Eggs Benedicts Recipe Post]</li>
<li>[Pancakes Recipe Post]</li>
</ul>
</li>
<li>Lunch Recipes
<ul>
<li>[Grilled Cheese Sandwish Recipe Post]</li>
</ul>
</li>
<li>Dinner Recipes
<ul>
<li>[Fettucini Alfredo Recipe Post]</li>
<li>[Lasagna Recipe Post]</li>
</ul>
</li>
</ul>
<p><span id="more-390"></span>WordPress uses <a href="http://codex.wordpress.org/The_Loop">The Loop</a> to display posts. In this case we want to first retrieve all the categories from the database, then we want to cycle through each category and use The Loop to display all the posts in that category.</p>
<p>For most pages on your WordPress site, the url of the page will contain a query string which will dictate which posts are shown with The Loop. We can overide any existing query string using the <a href="http://codex.wordpress.org/Template_Tags/query_posts">query_posts()</a> function to display the specific posts we want. The nice thing is that we can use the <a href="http://codex.wordpress.org/Template_Tags/query_posts">query_posts()</a> function multiple times in order to run The Loop multiple times on the same page.</p>
<p>There are several ways of using <a href="http://codex.wordpress.org/The_Loop#Multiple_Loops">multiple Loops</a> in WordPress. With using multiple query_posts, the only trick is that you should save and reset the original query for the page. This is so that later down the page (like in the Sidebar) you can use all the functions that apply to the page you&#8217;re on and not on the last query_post you performed. The query is stored in $wp_query, so we can use <code>&lt;?php $temp_query = $wp_query; ?&gt;</code> before the Loops to save the query, and we can use <code>&lt;?php $wp_query = $temp_query; ?&gt;</code> at the end of the Loops to retrieve it. This is like in Multiple Loops Example 2 from <a href="http://codex.wordpress.org/The_Loop#Multiple_Loops">The Loops WordPress page</a>.</p>
<p>There are basically two important parts of this loop. First we must create an array that lists all the categories. Then we cycle through that array and retrieve all the posts from each category, one category at a time. To create an array of all the categories we&#8217;ll use <a href="http://codex.wordpress.org/Function_Reference/get_categories">get_categories()</a>. This simply returns an array with all the categories. Now, this array has a lot of information associated with it. To get an idea of what it looks like you can always print the array out in readable form with <code>&lt;?php print_r( get_categories() ); ?&gt;</code> . There&#8217;s information like the name and category ID for each category. We&#8217;ll make use of those.</p>
<p><script type="text/javascript">// <![CDATA[
 google_ad_client = "pub-4166122661117213"; /* 336x280, created 4/27/09 */ google_ad_slot = "9673694336"; google_ad_width = 336; google_ad_height = 280;
// ]]&gt;</script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script></p>
<p>To show the posts in a category we have to use query_posts and an identifying characteristic of the category we want. The name of the category isn&#8217;t always the best solution. For example if you have a category called, &#8220;Breakfast &amp; Juices,&#8221; the ampersand will trip the PHP up, so we&#8217;ll just use the ID number of the category. If we make our category array like this: <code>&lt;?php $categories=get_categories(); ?&gt;</code> . Then we can retrieve the category ID from the created array with <code>category-&gt;cat_ID</code>. We can retrieve the name of the category with <code>$category-&gt;name</code>.</p>
<p>Finally, putting everything together we get:</p>
<ol>
<li>
<pre>&lt;?php $temp_query = $wp_query; ?&gt;</pre>
</li>
<li>
<pre>  &lt;?php $categories=get_categories();</pre>
</li>
<li>
<pre>  foreach($categories as $category)</pre>
</li>
<li>
<pre>  { ?&gt;</pre>
</li>
<li>
<pre>    &lt;h2&gt;&lt;?php echo($category-&gt;name); ?&gt;&lt;/h2&gt;</pre>
</li>
<li>
<pre>    &lt;?php query_posts("cat=$category-&gt;cat_ID"); ?&gt;</pre>
</li>
<li>
<pre>    &lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;</pre>
</li>
<li>
<pre>      [ YOUR POST DISPLAYING CODE GOES HERE ]</pre>
</li>
<li>
<pre>    &lt;?php endwhile; endif; ?&gt;</pre>
</li>
<li>
<pre>  } ?&gt;</pre>
</li>
<li>
<pre>&lt;?php $wp_query = $temp_query; ?&gt;</pre>
</li>
</ol>
<p>If you want to see what happens if you don&#8217;t reassign the original query just remove the last line. It will probably make your sidebar say something odd&#8230; though the particulars will depend on the theme you&#8217;re using.</p>
<p>This solution will work if you do not have child categories. If you do have child categories (an example would be Ice Cream category in the Deserts parent category), then you will get repetition of posts, since this will display all the posts from all parent categories then the posts for child categories. There are several ways to resolve this. The simplest way is to only display parent categories and not child categories. To do this we must check if a category is a child before we display it&#8217;s posts. To do this we&#8217;ll use <code>$category-&gt;parent</code>. This returns the parent of the category. If it returns nothing, or false, we know that the category is not a child but a parent. We&#8217;ll put an if statement that only proceeds if there is no parent of the category inside the foreach loop. Here we go:</p>
<ol>
<li>
<pre>&lt;?php $temp_query = $wp_query; ?&gt;</pre>
</li>
<li>
<pre>&lt;?php $categories=get_categories();</pre>
</li>
<li>
<pre>foreach($categories as $category)</pre>
</li>
<li>
<pre>{</pre>
</li>
<li>
<pre>  if(!$category-&gt;parent)</pre>
</li>
<li>
<pre>  { ?&gt;</pre>
</li>
<li>
<pre>    &lt;h2&gt;&lt;?php echo($category-&gt;name); ?&gt;&lt;/h2&gt;</pre>
</li>
<li>
<pre>    &lt;?php query_posts("cat=$category-&gt;cat_ID"); ?&gt;</pre>
</li>
<li>
<pre>    &lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;</pre>
</li>
<li>
<pre>      [ YOUR POST DISPLAYING CODE GOES HERE ]</pre>
</li>
<li>
<pre>    &lt;?php endwhile; endif; ?&gt;</pre>
</li>
<li>
<pre>  } ?&gt;</pre>
</li>
<li>
<pre>}</pre>
</li>
<li>
<pre>&lt;?php $wp_query = $temp_query; ?&gt;</pre>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://netlumination.com/blog/using-multiple-loops-in-wordpress-with-query_posts-and-get_categories/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
