<?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>Lunatic Notes &#187; html</title>
	<atom:link href="http://lunatic.web.id/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://lunatic.web.id</link>
	<description>Rants about tech and design</description>
	<lastBuildDate>Fri, 22 May 2009 11:28:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Coding PHP Loops Easily</title>
		<link>http://lunatic.web.id/webdev/coding-php-loops-easily/</link>
		<comments>http://lunatic.web.id/webdev/coding-php-loops-easily/#comments</comments>
		<pubDate>Tue, 19 May 2009 06:20:12 +0000</pubDate>
		<dc:creator>anima</dc:creator>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Webdev]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://lunatic.web.id/?p=157</guid>
		<description><![CDATA[Working with loops on PHP is inevitable, and I've seen many beginners (read: me) pulling their hair because of it. The thing with coding a loop is, sometimes you have to do stuffs like this:

<code>&#60;?php for ($i=1; $i &#60; 10; $i++) { echo "&#60;a href=\"something.php?id=$i\"&#62;No $i&#60;/a&#62;"; }</code>

Am I right am I right?]]></description>
			<content:encoded><![CDATA[<p><a href="http://lunatic.web.id/wp-content/uploads/2009/05/php.png"><img class="alignleft size-full wp-image-166 hidden" title="PHP" src="http://lunatic.web.id/wp-content/uploads/2009/05/php.png" alt="PHP" width="200" height="200" /></a>Working with loops on PHP is inevitable, and I&#8217;ve seen many beginners (read: me) pulling their hair because of it. The thing with coding a loop is, sometimes you have to do stuffs like this:</p>
<p><code>&lt;?php for ($i=1; $i &lt; 10; $i++) { echo "&lt;a href=\"something.php?id=$i\"&gt;No $i&lt;/a&gt;"; }</code></p>
<p>Am I right am I right?</p>
<p>Looks complicated? Hell yeah. Truth is, there&#8217;s a much simpler way to achieve the same goal. I learned this trick from Wordpress&#8217; Loop. Check this out.</p>
<p>Let&#8217;s take a look at this piece of code, in here we&#8217;re doing a <code>For</code> loop:</p>
<pre class="php" name="code">&lt;ul&gt;
&lt;?php
for ($i=1; $i&lt;10; $i++) {
    echo "&lt;li&gt;&lt;a href=\"?p=$i\"&gt;Go to page # $i&lt;/a&gt;&lt;/li&gt;";
}
?&gt;
&lt;/ul&gt;</pre>
<p>As simple as that looks, the echo command could be a little tricky because we often have to use double quote and single quote characters inside. Debugging can be a pain in the ass.</p>
<p>An approach I propose: the Wordpress way. Here&#8217;s how:</p>
<pre class="php" name="code">&lt;ul&gt;
&lt;?php
for ($i=1; $i&lt;10; $i++) { ?&gt;
    &lt;li&gt;&lt;a href="?p=&lt;?php echo $i;?&gt;"&gt;Go to page # &lt;?php echo $i;?&gt; &lt;/a&gt;&lt;/li&gt;
&lt;?php }
?&gt;
&lt;/ul&gt;</pre>
<p>This way we exclude the HTML tags from the PHP loop. Wow I can&#8217;t really describe that, if you do PHP you know what I mean. Now you can HTML not inside the echo command.</p>
<p>Wordpress is using this approach on templating and I find it very easy to use once you get used to it. So why don&#8217;t you give it a try and let us know what you think :)</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://lunatic.web.id/webdev/coding-php-loops-easily/" target="_blank" title="Share on Facebook">Share on Facebook</a></p><p class="shareparagraph" align="left"><a class="tt" href="http://twitter.com/home/?status=Coding+PHP+Loops+Easily+http://bit.ly/INcEb" title="Tweet This"><img class="nothumb" src="http://lunatic.web.id/wp-content/plugins/tweet-this/icons/tt-twitter.png" alt="[Post to Twitter]" border="0" /></a> <a class="tt" href="http://twitter.com/home/?status=Coding+PHP+Loops+Easily+http://bit.ly/INcEb" title="Tweet This">Tweet This</a>&nbsp; <a class="tt" href="http://delicious.com/post?url=http://lunatic.web.id/webdev/coding-php-loops-easily/&amp;title=Coding+PHP+Loops+Easily" title="Delicious This"><img class="nothumb" src="http://lunatic.web.id/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="[Post to Delicious]" border="0" /></a> <a class="tt" href="http://delicious.com/post?url=http://lunatic.web.id/webdev/coding-php-loops-easily/&amp;title=Coding+PHP+Loops+Easily" title="Delicious This">Delicious This</a>&nbsp; <a class="tt" href="http://digg.com/submit?url=http://lunatic.web.id/webdev/coding-php-loops-easily/&amp;title=Coding+PHP+Loops+Easily" title="Digg This"><img class="nothumb" src="http://lunatic.web.id/wp-content/plugins/tweet-this/icons/tt-digg.png" alt="[Post to Digg]" border="0" /></a> <a class="tt" href="http://digg.com/submit?url=http://lunatic.web.id/webdev/coding-php-loops-easily/&amp;title=Coding+PHP+Loops+Easily" title="Digg This">Digg This</a>&nbsp; <a class="tt" href="http://ping.fm/ref/?method=microblog&amp;title=Coding+PHP+Loops+Easily&amp;link=http://lunatic.web.id/webdev/coding-php-loops-easily/" title="Ping.fm This"><img class="nothumb" src="http://lunatic.web.id/wp-content/plugins/tweet-this/icons/tt-ping.png" alt="[Post to Ping.fm]" border="0" /></a> <a class="tt" href="http://ping.fm/ref/?method=microblog&amp;title=Coding+PHP+Loops+Easily&amp;link=http://lunatic.web.id/webdev/coding-php-loops-easily/" title="Ping.fm This">Ping This</a>&nbsp; <a class="tt" href="http://reddit.com/submit?url=http://lunatic.web.id/webdev/coding-php-loops-easily/&amp;title=Coding+PHP+Loops+Easily" title="Reddit This"><img class="nothumb" src="http://lunatic.web.id/wp-content/plugins/tweet-this/icons/tt-reddit.png" alt="[Post to Reddit]" border="0" /></a> <a class="tt" href="http://reddit.com/submit?url=http://lunatic.web.id/webdev/coding-php-loops-easily/&amp;title=Coding+PHP+Loops+Easily" title="Reddit This">Reddit This</a>&nbsp; <a class="tt" href="http://stumbleupon.com/submit?url=http://lunatic.web.id/webdev/coding-php-loops-easily/&amp;title=Coding+PHP+Loops+Easily" title="StumbleUpon This"><img class="nothumb" src="http://lunatic.web.id/wp-content/plugins/tweet-this/icons/tt-su.png" alt="[Post to StumbleUpon]" border="0" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://lunatic.web.id/webdev/coding-php-loops-easily/&amp;title=Coding+PHP+Loops+Easily" title="StumbleUpon This">Stumble This</a>&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://lunatic.web.id/webdev/coding-php-loops-easily/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
