<?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>Catch My Fame &#187; PHP</title>
	<atom:link href="http://www.catchmyfame.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.catchmyfame.com</link>
	<description>A web designoper&#039;s journal</description>
	<lastBuildDate>Thu, 02 Feb 2012 14:18:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP Pagination Class Updated &#8211; Version 2</title>
		<link>http://www.catchmyfame.com/2011/10/23/php-pagination-class-updated-version-2/</link>
		<comments>http://www.catchmyfame.com/2011/10/23/php-pagination-class-updated-version-2/#comments</comments>
		<pubDate>Sun, 23 Oct 2011 20:24:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.catchmyfame.com/?p=927</guid>
		<description><![CDATA[Based on feedback (and the long time between updates), I&#8217;m releasing an updated version of the PHP pagination class. Changes No more SQL errors if using the class on a query with no results (although I fail to see the point of using it on an empty query anyway, but the bug is fixed) When [...]]]></description>
			<content:encoded><![CDATA[<p>Based on feedback (and the long time between updates), I&#8217;m releasing an updated version of the PHP pagination class.</p>
<h3>Changes</h3>
<ul>
<li>No more SQL errors if using the class on a query with no results (although I fail to see the point of using it on an empty query anyway, but the bug is fixed)</li>
<li>When selecting &#8216;ALL&#8217; only one page is displayed, and if you are using the display_items_per_page drop-down menu, &#8216;ALL&#8217; is now selected as well.</li>
<li>If a page less than page one, or greater than the total number of pages is requested, no results are displayed and the appropriate prevous/next links are disabled</li>
<li>The items per page array that populates the display_items_per_page drop-down menu is now an option that can be configured when instantiating the class. Previously this was hard-coded to the class.</li>
<li>The $high option has been removed from the class (it was never used).</li>
</ul>
<p>Note that if you receive a PHP notice about undefined indexes that you should turn down your error reporting. This notice may be displayed when loading the page for the first time when no $_GET variables are present. The class accounts for this, however PHP throws a notice about it.</p>
<h3>Examples</h3>
<ul>
<li><a href="/paginator/example.2.php" target="_blank">Basic example</a></li>
<li><a href="/paginator/example-form.2.php" target="_blank">Form variables carry over example</a></li>
</ul>
<p>I&#8217;ve also added license info at the head of the class (CC Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)).</p>
<h3>Download</h3>
<ul>
<li><a href="http://www.catchmyfame.com/downloads/download.php?48">PHP Pagination Class v2</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.catchmyfame.com/2011/10/23/php-pagination-class-updated-version-2/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Narrow Minded</title>
		<link>http://www.catchmyfame.com/2010/03/01/narrow-minded/</link>
		<comments>http://www.catchmyfame.com/2010/03/01/narrow-minded/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 02:42:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript/jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Usability]]></category>

		<guid isPermaLink="false">http://www.catchmyfame.com/?p=492</guid>
		<description><![CDATA[Recently I was checking out some code written many years ago by another developer, and as I was hacking it up to be re-used, I started wondering why he had decided to split up his lines of code so that they were very narrow. Comments, for example, that were a few sentences long ended up [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was checking out some code written many years ago by another developer, and as I was hacking it up to be re-used, I started wondering why he had decided to split up his lines of code so that they were very narrow. Comments, for example, that were a few sentences long ended up spanning nearly a dozen lines when they could easily exist on just a couple of lines. It was then that I noticed that he had been wrapping his text at 80 columns – in other words, each line contained no more than 80 characters. It&#8217;s been a long time since I forced myself to wrap code at 80 columns, either because what I had written was concise to begin with, or because no one else would be looking at the code and it just didn&#8217;t matter, and I had almost forgotten why people did this in the first place.</p>
<p>I’ve seen code wrapped at 80 columns plenty of times before, but something about picking apart this code at that moment made me suddenly ask why. What was so special about this 80 column “standard” and why was it seen throughout the programming world? When I first started out coding this was just assumed to be the way to do things but I had never fully heard an explanation as for why this was done.</p>
<p>As it turns out the answer lies in old technology and tradition. In the early days of coding,  text based terminal displays were only 80 characters wide, so any lines longer than 80 characters needed to be wrapped. There just wasn’t any choice in the matter. Remember that GUIs and mice didn’t exist back then and scrolling was typically done only vertically. Some people believe that the even older computing method of using punch cards, which also typically had 80 columns, is the reason for this style of coding, however while it may have influenced the design of those old terminals, I don’t believe that punch cards directly gave way to practice of coding text at 80 columns.</p>
<p>Now since virtually no one uses an old terminal to write code, and today’s monitors are larger and have larger display resolution than older machines, why do we still see this? The answer? Tradition. Well that and readability. While most modern code editors will let you type a single line for as long as you want, reading that line becomes a huge pain in the ass. You’re forced to scroll horizontally to the right to read the code or comment, and then scroll back to the left to get back to the rest of the code. While an 80 column limit might seem constrictive or limiting, it actually makes code easier to read, and can help you write better code by making you find ways to shrink your code down to fit in one line. Another advantage to limiting the characters per line is code comparison. By staying with the 80 column rule you can usually fit two files side by side and examine them line by line quite easily.</p>
<p>Oh and while 80 columns is sort of a standard, 72 and 76 column layout are also quite common, but slightly more restrictive. Personally I am lazier than I like to admit and I often don’t pay a load of attention to how many columns each line of my code takes up. But that’s being both a bit lazy and selfish. By making your code readable, it helps you and anyone else looking at it&#8230;and you just know you’re going to need another set of eyes looking at your code at some point to help ferret out a bug down the line. And while I’m no advocate for 80 columns, if you stick with a layout that makes your code easy to read why not pick something like 100, 120, or 132 columns now that most people have larger monitors? Being lazy I also don’t think that any of this should be a rule but more of a suggestion or guideline. Bottom line, once you get your code to work the way you want it to, why not make it look good and improve the readability at the same time?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.catchmyfame.com/2010/03/01/narrow-minded/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP’s Serialize Function</title>
		<link>http://www.catchmyfame.com/2009/09/29/introduction-to-php%e2%80%99s-serialize-function/</link>
		<comments>http://www.catchmyfame.com/2009/09/29/introduction-to-php%e2%80%99s-serialize-function/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 14:50:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.catchmyfame.com/?p=316</guid>
		<description><![CDATA[I was tinkering with a WordPress database the other day and was nosing around through some tables, looking for some statistical data which I never found, and noticed that a few of the tables were storing data in an unusual format (to me at least). Specifically one record I found looked like this: a:6:{s:5:"width";i:250;s:6:"height";i:323;s:14:"hwstring_small";s:22:"height='96' width='74'";s:4:"file";s:72:"/uploads/2008/04/wireframes7.jpg";s:5:"sizes";a:2:{s:9:"thumbnail";a:3:{s:4:"file";s:23:"wireframes7-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;}s:6:"medium";a:3:{s:4:"file";s:23:"wireframes7-232x300.jpg";s:5:"width";i:232;s:6:"height";i:300;}}s:10:"image_meta";a:10:{s:8:"aperture";i:0;s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";i:0;s:9:"copyright";s:0:"";s:12:"focal_length";i:0;s:3:"iso";i:0;s:13:"shutter_speed";i:0;s:5:"title";s:0:"";}} [...]]]></description>
			<content:encoded><![CDATA[<p>I was tinkering with a WordPress database the other day and was nosing around through some tables, looking for some statistical data which I never found, and noticed that a few of the tables were storing data in an unusual format (to me at least). Specifically one record I found looked like this:</p>
<p><code>a:6:{s:5:"width";i:250;s:6:"height";i:323;s:14:"hwstring_small";s:22:"height='96' width='74'";s:4:"file";s:72:"/uploads/2008/04/wireframes7.jpg";s:5:"sizes";a:2:{s:9:"thumbnail";a:3:{s:4:"file";s:23:"wireframes7-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;}s:6:"medium";a:3:{s:4:"file";s:23:"wireframes7-232x300.jpg";s:5:"width";i:232;s:6:"height";i:300;}}s:10:"image_meta";a:10:{s:8:"aperture";i:0;s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";i:0;s:9:"copyright";s:0:"";s:12:"focal_length";i:0;s:3:"iso";i:0;s:13:"shutter_speed";i:0;s:5:"title";s:0:"";}}</code></p>
<p>Whoa. Looked like some sort of delimited data (it is) but not in a form I recognized. Now over the years I thought I had seen my share of input that is stored in a database. Like most PHP developers I didn’t learn PHP through a class, I learned it by reading and trial and error, but I don’t remember seeing something like this before. Now some of you already know what kind of data this is while others are probably scratching your head and wondering what kind of strange delimited data this is. Isn’t it obvious, that’s an array.</p>
<h2>Serialization</h2>
<p><a href="http://www.catchmyfame.com/wp-content/uploads/2009/09/cereal.jpg" rel="thumbnail"><img class="alignnone size-full wp-image-327 right" title="cereal" src="http://www.catchmyfame.com/wp-content/uploads/2009/09/cereal.jpg" alt="cereal" width="300" height="225" /></a>According to <a href="http://en.wikipedia.org/wiki/Serialization">Wikipedia</a>, serialization “<em>is the process of converting an object into a sequence of bits so that it can be persisted on a storage medium (such as a file, or a memory buffer) or transmitted across a network connection link. When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object.</em>”</p>
<p>Now that I’ve gotten the obligatory definition out of the way, let’s humanize exactly what we’re talking about with a real world example.</p>
<p>Let’s say that you have a feedback form on your website and you want to store all the feedback in a database so that you can analyze it later. Along with the feedback that visitors provide you’re also interested in storing the date and time of their visit, their IP address, and some other information. One way to do this would be to create a table where you had a column for each field in your form. Every record entered in the table would need to insert each field of your feedback form, and other data you wanted to store (e.g. IP address, time submitted), into the table with a long SQL statement. Later if you needed to change your form, say add or remove a field, then you would not only need to change your table schema, but you’d also have to alter your SQL statement. Managing the feedback data this way would also result in the database table growing quite large over time. Since you can&#8217;t insert an object or array into a MySQL database (you&#8217;ll see an error like Unknown column &#8216;Array&#8217; in &#8216;field list&#8217; ) it would be nice if there was another way to easily store all the values we receive, especially if the number of fields could change. So what’s the alternative? <a href="http://us2.php.net/manual/en/function.serialize.php">PHP’s serialize() function</a>.</p>
<p>PHP’s serialize function was designed for just this sort of job. What it does is basically flatten an array or object into a string (according to php.net, it “Generates a storable representation of a value”). Using our feedback form example, we could pass the $_POST array through the serialize function, and end up with a string which we can store in our database. When the serialize function is done doing its magic with whatever you throw at it, the output is a whole bunch of curly braces and semicolons which looks similar to <code>a:1:{i:0;s:7:"slugdiv";}</code> and the example in the first paragraph.<em> (Note that whenever you enter user submitted data its a good idea to run it through the PHP mysql_real_escape_string function first.)</em> So serializing an array converts it from the array to a string:<em><br />
</em></p>
<p><a href="http://www.catchmyfame.com/wp-content/uploads/2009/09/serialize.gif" rel="thumbnail"><img class="alignnone size-full wp-image-321" title="serialize" src="http://www.catchmyfame.com/wp-content/uploads/2009/09/serialize.gif" alt="serialize" width="640" height="114" /></a></p>
<p>All the array keys and values are preserved by the serialize function so that they can be reconstituted later. If we use serialize() to store our feedback form data, we can eliminate all the fields in our table and just keep the one field for our serialized string. That’s a big space saver. Plus, by storing just the one serialized string in our database we can greatly simplify our SQL statements and no matter how many times we change our form, all the array contents will neatly fit in our table without it or the SQL statements needing to be modified.</p>
<p>Right about now you might be asking, well that’s pretty cool but how do I get my data back out of the database when I need it? No need to fear, just use <a href="http://us2.php.net/manual/en/function.unserialize.php">unserialize()</a> to reverse the process. If you assign the output of unserialize() to a variable then that variable will be a clone of what the original input was. Unserialize will keep all the keys and values of our feedback form’s $_POST array so that they’re instantly available just as they were when they were first created.  For example, if you retrieved a serialized row from our table, we could run it through the unserialize function and assign the output to a variable: $reconstituted = unserialize($row['feedback']);.</p>
<p>Sounds great, so what’s the down side? Well by storing our feedback form data in serialized form in the database we lose the ability to use SQL to manipulate the data. So if we wanted to run a query that returned only the email addresses for all of our feedback, then we’d be out of luck. We’d have to use PHP to pull all the data out of the table, unserialize it, and then search through it using PHP instead of MySQL. If our data was stored in unserialized form, where each column corresponded to a field in our feedback form, then searching through and manipulating the data would be far faster and easier with MySQL. So knowing when to use serialization is important. Storing user preferences, or say attributes for an image (like WordPress does), is a perfect use for serialization. If you’ll have the need to search, update or delete parts of serialized data, or otherwise manipulate parts of the data you’re storing, then serialization may not be the way to go. However if you simply need to store arrays or objects in a database so that they can be pulled out in their entirety later, it may be the perfect solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.catchmyfame.com/2009/09/29/introduction-to-php%e2%80%99s-serialize-function/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP Pagination Class</title>
		<link>http://www.catchmyfame.com/2009/08/04/php-pagination-class/</link>
		<comments>http://www.catchmyfame.com/2009/08/04/php-pagination-class/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 20:59:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.catchmyfame.com/?p=203</guid>
		<description><![CDATA[Some of you have asked for more examples of the PHP pagination class I wrote, specifically the same examples I&#8217;m using here on the site. The database comes from MySQL&#8217;s free example databases available at http://dev.mysql.com/doc/#sampledb (my example uses the world database). The two examples I use here can be seen at http://www.catchmyfame.com/paginator/example.php and http://www.catchmyfame.com/paginator/example-form.php. [...]]]></description>
			<content:encoded><![CDATA[<p>Some of you have asked for more examples of the PHP pagination class I wrote, specifically the same examples I&#8217;m using here on the site. The database comes from<br />
MySQL&#8217;s free example databases available at http://dev.mysql.com/doc/#sampledb (my example uses the world database). The two examples I use here can be seen at<br />
http://www.catchmyfame.com/paginator/example.php and http://www.catchmyfame.com/paginator/example-form.php. You can download both PHP files, along with the<br />
pagination class in a zip file. Don&#8217;t forget to grab the sample database from MySQL.</p>
<p>Note that the MySQL site seems to be undergoing some updates and the sample databases are hard to find. If the above links don&#8217;t work try http://dev.mysql.com/doc/index-other.html</p>
]]></content:encoded>
			<wfw:commentRss>http://www.catchmyfame.com/2009/08/04/php-pagination-class/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>You Learn Something New Everyday</title>
		<link>http://www.catchmyfame.com/2008/12/18/you-learn-something-new-everyday/</link>
		<comments>http://www.catchmyfame.com/2008/12/18/you-learn-something-new-everyday/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 14:57:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.catchmyfame.com/?p=48</guid>
		<description><![CDATA[I&#8217;ve been coding in PHP for probably eight years and thought I knew the language pretty well. Was I surprised the other day when I came across someone else&#8217;s code and stared at it blankly, wondering why they had done what they did, and why wasn&#8217;t it generating an error. Specifically they had something like [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been coding in PHP for probably eight years and thought I knew the language pretty well. Was I surprised the other day when I came across someone else&#8217;s code and stared at it blankly, wondering why they had done what they did, and why wasn&#8217;t it generating an error. Specifically they had something like this:</p>
<pre>echo "Your IP address is {$_SERVER['<span class="term">REMOTE_ADDR'</span>]}";</pre>
<p>Why were those curly brackets in the echo statement and why wasn&#8217;t this giving me errors? I removed the curly brackets and as expected, I received syntax errors. Apparently the {} were magical. Now in my own code, I would typically write the above line like:</p>
<pre>echo "Your IP address is " . $_SERVER['<span class="term">REMOTE_ADDR'</span>];</pre>
<p>or</p>
<pre>echo "Your IP address is $_SERVER[<span class="term">REMOTE_ADDR</span>]";</pre>
<p>Now method one is perfectly valid code, but it&#8217;s a bit ugly having to starty and stop the string to insert a variable. Method 2 is somewhat wrong as the quotes have been removed from the server array key.  According to php.net, &#8220;Always use quotes around a string literal array index. For example,     <em>$foo['bar']</em> is correct, while     <em>$foo[bar]</em> is not.&#8221; But with this new, more compact method, I could make my code cleaner.  I was off to learn more.</p>
<p>After some <a href="http://www.php.net/manual/en/language.types.string.php">searching</a> I discovered that PHP will allow you to place curly brackets around a variable in a string (either around the entire variable or with the dollar sign sticking out). &#8220;If a dollar sign (<em>$</em>) is encountered, the parser will      greedily take as many tokens as possible to form a valid variable name.      Enclose the variable name in curly braces to explicitly specify the end of      the name. Similarly, an <a class="type array" href="http://www.php.net/manual/en/language.types.array.php">array</a> index or an <a class="type object" href="http://www.php.net/manual/en/language.types.object.php">object</a> property      can be parsed. With array indices, the closing square bracket      (<em>]</em>) marks the end of the index. The same rules apply to      object properties as to simple variables.     &#8221; Who knew? I must&#8217;ve missed this day during PHP 101 class. Here&#8217;s the example from the manual:</p>
<pre><code><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php
$beer </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'Heineken'</span><span style="color: #007700;">;
echo </span><span style="color: #dd0000;">"</span><span style="color: #0000bb;">$beer</span><span style="color: #dd0000;">'s taste is great"</span><span style="color: #007700;">; </span><span style="color: #ff8000;">// works; "'" is an invalid character for variable names</span><span style="color: #007700;">
echo </span><span style="color: #dd0000;">"He drank some </span><span style="color: #0000bb;">$beers</span><span style="color: #dd0000;">"</span><span style="color: #007700;">;   </span><span style="color: #ff8000;">// won't work; 's' is a valid character for variable names but the variable is "$beer"</span><span style="color: #007700;">
echo </span><span style="color: #dd0000;">"He drank some </span><span style="color: #007700;">${</span><span style="color: #0000bb;">beer</span><span style="color: #007700;">}</span><span style="color: #dd0000;">s"</span><span style="color: #007700;">; </span><span style="color: #ff8000;">// works</span><span style="color: #007700;">
echo </span><span style="color: #dd0000;">"He drank some </span><span style="color: #007700;">{</span><span style="color: #0000bb;">$beer</span><span style="color: #007700;">}</span><span style="color: #dd0000;">s"</span><span style="color: #007700;">; </span><span style="color: #ff8000;">// works</span><span style="color: #0000bb;">
?&gt;

</span></span></code></pre>
<p>So in a nushell, when using variables in a string you are allowed to enclose the varable in curly brackets to allow variable expansion without having to jump in and out of quotes, or drop quotes. Who doesn&#8217;t love PHP?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.catchmyfame.com/2008/12/18/you-learn-something-new-everyday/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Bracket Style and Indenting Code</title>
		<link>http://www.catchmyfame.com/2007/12/18/bracket-style-and-indenting-code/</link>
		<comments>http://www.catchmyfame.com/2007/12/18/bracket-style-and-indenting-code/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 00:24:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript/jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Rants]]></category>

		<guid isPermaLink="false">http://www.catchmyfame.com/2007/12/18/bracket-style-and-indenting-code/</guid>
		<description><![CDATA[I&#8217;ll try to keep this short. Having been coding for years I&#8217;ve seen a lot of examples of how other people write code. Now while there are certain requirements in coding, there are areas where you can inject your own person style or preference. One area that has always bugged me is how code is [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll try to keep this short. Having been coding for years I&#8217;ve seen a lot of examples of how other people write code. Now while there are certain requirements in coding, there are areas where you can inject your own person style or preference. One area that has always bugged me is how code is indented and how brackets are used to separate blocks of code. Here&#8217;s a very simple example of how I normally code a simple if/else block:</p>
<pre>if (hours &lt; 24 &amp;&amp; minutes &lt; 60 &amp;&amp; seconds &lt; 60)
{
 return true;
}
else
{
 return false;
}</pre>
<p>This is known as Allman style (named after Eric Allman, the developer of sendmail). The most common variant of this would be K&amp;R style (from Kernighan and Ritchie&#8217;s book The C Programming Language) and the same code looks like this:</p>
<pre>if (hours &lt; 24 &amp;&amp; minutes &lt; 60 &amp;&amp; seconds &lt; 60) {
 return true;
} else {
 return false;
}</pre>
<p>Equivalent in every way except readability. Some people call this &#8220;The One True Brace Style&#8221; because it&#8217;s been around so long. The major difference is that the opening bracket is placed at the end of the line that the control statement is on where in Allman style the brackets are each on their own line.</p>
<p>To me it makes sense to make code as readable as possible by lining up brackets to match the block they go with. It becomes apparent what blocks of code belong to what conditions.</p>
<table style="border-collapse: collapse" border="0" bordercolor="#111111" cellspacing="1">
<tr>
<td><img src="/images/indent1.gif" border="0" height="160" width="412" /></td>
</tr>
<tr>
<td>Allman style</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><img src="/images/indent2.gif" border="0" height="192" width="471" /></td>
</tr>
<tr>
<td>K&amp;R style</td>
</tr>
</table>
<p>If I was to have another condition nested within the example, it would look like this:</p>
<pre>if (hours &lt; 24 &amp;&amp; minutes &lt; 60 &amp;&amp; seconds &lt; 60)
{
 if(hours % 2 == 0)
 {
 	return true;
 }
 else
 {
 	return false;
 }
}</pre>
<p>Again, very readable.</p>
<p>The funny thing about K&amp;R style is that its roots seems to be based in the fact that programmers used to have to deal with limited screen space and by squishing the brackets together with the blocks that they belonged to, it saved precious screen real estate. Most programmers either picked up the style they use most often from learning coding in a class or by following the examples set by others, while other programmers code for readability, especially now that space isn&#8217;t the issue it once was.</p>
<p>While Allman and K&amp;R are just two of the most popular styles (see<a href="http://en.wikipedia.org/wiki/Indent_style"><br />
http://en.wikipedia.org/wiki/Indent_style</a> and<br />
<a href="http://en.wikipedia.org/wiki/Programming_style">http://en.wikipedia.org/wiki/Programming_style</a><br />
for some others and a detailed explanation about the history of each)</p>
<p>(please pardon the less than perfect wordpress code formatting)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.catchmyfame.com/2007/12/18/bracket-style-and-indenting-code/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Simple Pagination Class Revisited</title>
		<link>http://www.catchmyfame.com/2007/09/18/simple-pagination-class-revisited/</link>
		<comments>http://www.catchmyfame.com/2007/09/18/simple-pagination-class-revisited/#comments</comments>
		<pubDate>Tue, 18 Sep 2007 16:10:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.catchmyfame.com/2007/09/18/simple-pagination-class-revisited/</guid>
		<description><![CDATA[I wanted to make a small addition to the pagination class talked about at http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/. I added another example which uses a basic form at http://www.catchmyfame.com/paginator/example-form.php. This is very similar to the first example (http://www.catchmyfame.com/paginator/example.php) where the query is hard coded. This new example is meant to show how the pagination class can be used [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to make a small addition to the pagination class talked about at <a href="http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/">http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/</a>. I added another example which uses a basic form at <a href="http://www.catchmyfame.com/paginator/example-form.php">http://www.catchmyfame.com/paginator/example-form.php</a>. This is very similar to the first example (<a href="http://www.catchmyfame.com/paginator/example.php">http://www.catchmyfame.com/paginator/example.php</a>) where the query is hard coded. This new example is meant to show how the pagination class can be used with forms where the data determines the query. In this case, the pull down menu for a continent selection determines the query. Note that the pagination class works fine with both GET and POST requests.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.catchmyfame.com/2007/09/18/simple-pagination-class-revisited/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Finally, the simple pagination class</title>
		<link>http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/</link>
		<comments>http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/#comments</comments>
		<pubDate>Sat, 28 Jul 2007 20:10:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/</guid>
		<description><![CDATA[In the years that I&#8217;ve been a web developer, I have written very few OOP PHP scripts. I just haven&#8217;t had the need to, and to be honest the whole OOP concept seemed like something I didn&#8217;t need to bother with. Why do I need to write a script in an object-oriented style and how [...]]]></description>
			<content:encoded><![CDATA[<p>In the years that I&#8217;ve been a web developer, I have written very few <acronym title="Object Oriented Programming">OOP</acronym> PHP scripts. I just haven&#8217;t had the need to, and to be honest the whole OOP concept seemed like something I didn&#8217;t need to bother with. Why do I need to write a script in an object-oriented style and how will it help me? I had seen examples and read tutorials about OOP (some horribly bad and some <a href="http://devzone.zend.com/node/view/id/638">really good</a>) but never had any desire to try it myself until one day when I was working on a couple of different projects that both needed the same thing, pagination.</p>
<p>Pagination just means numbering pages. In the project that I was working on, I was retrieving large amounts of data from a MySQL database and realized that I would have to break the data up into pages for two main reasons, 1) there was so much data that it could hang the browser when it was loading and 2) from a user perspective (we still care about that right?) it was unruly to say the least. If I was returning 50,000 rows from a database query, I knew that I would need to slice that up into more manageable chunks. Now in the past, I had written basic pagination functions to handle this type of issue and sometimes copied and pasted functions from older scripts to cut down on development time, fixing and re-coding where necessary. But I realized that for this new project I could try to write a pagination class that I would be able to just plug into any script and have it do the pagination for me. I could write a pagination class that I could use for my projects and future projects, and learn OOP in the process. I love killing two birds with one stone.<br />
<span id="more-22"></span></p>
<p>Which brings me to our pagination class (paginator.class.php). Now I&#8217;m not claiming to be the first person to write a pagination class &#8212; far from it. But I do feel that this one is the most lightweight, feature-rich, and easy to use that I&#8217;ve seen. <strong>For an example of the pagination class in action, <a href="http://www.catchmyfame.com/paginator/example.php">click here</a>.</strong></p>
<h2><strong>To download the pagination class, <a href="http://www.catchmyfame.com/downloads/download.php?48">click here</a>.</strong></h2>
<p><strong>Update (Aug 4, 2009): </strong>Some of you have asked for more examples of the PHP pagination class I wrote, specifically the same examples I&#8217;m using here on the site. The database comes from MySQL&#8217;s free example databases available at <a href="http://dev.mysql.com/doc/#sampledb" target="_blank">http://dev.mysql.com/doc/#sampledb</a> (my example uses the world database). The two examples I use here can be seen at <a href="http://www.catchmyfame.com/paginator/example.php">http://www.catchmyfame.com/paginator/example.php</a> and <a href="http://www.catchmyfame.com/paginator/example-form.php">http://www.catchmyfame.com/paginator/example-form.php</a>. You can download both PHP files in a <a href="/paginator/catchmyfame-pagination-example-files.zip"><strong>zip file</strong></a>. You&#8217;ll need to setup the database yourself and fill in the example PHP files with your database info. Don&#8217;t forget to grab the sample database from MySQL.</p>
<p><strong>Update (Oct 25, 2011</strong>): Version 2 now available for download. See <a title="PHP Pagination Class Updated – Version 2" href="http://www.catchmyfame.com/2011/10/23/php-pagination-class-updated-version-2/">here</a> for more info. Note that the examples on this page still use an older version of the class.</p>
<p>In the example below, you can see the paginator used at the top and bottom of the database query.<br />
<a title="thumb1" href="http://www.catchmyfame.com/images/pag1.gif" rel="thumbnail"><img style="border: 1px solid #000;" title="thumb1" src="http://www.catchmyfame.com/images/pag1.gif" alt="" width="211" height="236" /></a></p>
<p>In this example, you can see the mid range property which creates a range of pages around the current page and the use of &#8230; to distinguish the break between contiguous pages<br />
<a href="http://www.catchmyfame.com/images/pag2.gif" rel="thumbnail"><br />
<img style="border: 1px solid #000;" src="http://www.catchmyfame.com/images/pag2.gif" alt="" width="211" height="200" border="0" /></a></p>
<p>In this example you can see the optional items per page drop down menu<br />
<a href="http://www.catchmyfame.com/images/pag3.gif" rel="thumbnail"><br />
<img style="border: 1px solid #000;" src="http://www.catchmyfame.com/images/pag3.gif" alt="" width="211" height="189" border="0" /></a></p>
<p>In this example you can see the optional page jump menu<br />
<a href="http://www.catchmyfame.com/images/pag4.gif" rel="thumbnail"><br />
<img style="border: 1px solid #000;" src="http://www.catchmyfame.com/images/pag4.gif" alt="" width="211" height="199" border="0" /></a></p>
<p>In this example we see how the previous and next buttons are sensitive to what the current page is.<br />
<a href="http://www.catchmyfame.com/images/pag5.gif" rel="thumbnail"><br />
<img style="border: 1px solid #000;" src="http://www.catchmyfame.com/images/pag5.gif" alt="" width="211" height="121" border="0" /></a></p>
<p>This example shows how when there are less than 11 pages to display that the previous, next, and all links are not shown<br />
<a href="http://www.catchmyfame.com/images/pag6.gif" rel="thumbnail"><br />
<img style="border: 1px solid #000;" src="http://www.catchmyfame.com/images/pag6.gif" alt="" width="211" height="136" border="0" /></a></p>
<p>This example shows an alternate styling of the pagination results. By default a Digg-like CSS scheme is used however you can style the menu in limitless ways<br />
<a href="http://www.catchmyfame.com/images/pag7.gif" rel="thumbnail"><br />
<img style="border: 1px solid #000;" src="http://www.catchmyfame.com/images/pag7.gif" alt="" width="211" height="63" border="0" /></a></p>
<hr />
<h2>Paginator.class.php</h2>
<p>The paginator class allows you to easily generate page numbers and restrict database results without having to create complex pagination code of your own.</p>
<h3>Features:</h3>
<ul>
<li>Easy to use and reuse.</li>
<li>Dynamically creates page numbers based on the total number of items in a query and the desired number of items per page.</li>
<li>Ability to select a number of pages to display around the currently select page.</li>
<li>Links to show all results instead of paginated results.</li>
<li>Easily styled via CSS.</li>
<li>Returns SQL which can be used to modify the results in query.</li>
<li>The items per page can be changed by the user using a build in method which generates a simple drop down menu.</li>
<li>A ‘jump to page’ menu can be generated to give the user quick access to jump to any page of the results.</li>
<li>Creates ‘previous’ and ‘next’ buttons when more than 10 pages are generated.</li>
</ul>
<h3>Properties:</h3>
<ul>
<li>$items_per_page – The desired number of items to be shown on a page. If you use this and the display_items_per_page method at the same time, it will override anything the user chooses from the drop down menu created by display_items_per_page.</li>
<li>$items_total – The total number of items you’ll be paginating. Typically set by querying a table for a count of rows.</li>
<li>$current_page – The page the user is viewing. Will always be an integer &gt;= 1.</li>
<li>$num_pages – The total number of pages as generated by the paginator method.</li>
<li>$mid_range – The number of pages to show ‘around’ the current page. See step 5 in How To Use below.</li>
<li>$low – The offset to use in a SQL LIMIT statement (e.g. SELECT * FROM employees LIMIT 20,10).</li>
<li>$high – The number of rows to select in a SQL LIMIT statement (e.g. SELECT * FROM employees LIMIT 20,10).</li>
<li>$limit – A string used in an SQL statement to automatically handle the limiting of results based on the current page of results.</li>
<li>$return – A string used to store the HTML containing the page numbers.Used in the display_pages method.</li>
<li>$default_ipp = 25 – The default number of items to display per page.</li>
</ul>
<h3>Methods:</h3>
<ul>
<li>Paginator. Constructor. Usage: <code>$pages = new Paginator;</code></li>
<li>paginate. Calculates pages to display. Usage: <code>$pages-&gt;paginate();</code></li>
<li>display_items_per_page. Returns a string of HTML used to display an items per page menu. Usage: <code>echo  $pages-&gt;display_items_per_page();</code></li>
<li>display_jump_menu. Returns a string of HTML used to display a page jump menu. Usage: <code>echo $pages-&gt;display_jump_menu();</code></li>
<li>display_pages. Returns a string of HTML used to display the pages generated by the paginator<br />
method. Usage: <code>echo $pages-&gt;display_pages();</code></li>
</ul>
<h3>How to use:</h3>
<ol>
<li>Include paginator.class.php at the top of your script<code>require_once 'paginator.class.php';</code>Note: You could use include(), include_once(), or require() however using require_once ensures that the class will only be included once and if it can’t be found, will cause a fatal error.</li>
<li>Set the total number of items that you’ll be paginating.There are two ways to do this, 1) you can hard code the total, but that’s unrealistic since you’ll probably be using this class to paginate a database anyway (the whole point no?) but it can be a convenient way of testing or 2) make a quick call to your database to count the items you’ll be paginating. Ex: ‘SELECT COUNT(*) FROM table’ and save the total in a variable for use in step 4.<code>$pages-&gt;items_total = $num_rows;</code></li>
<li>Create a new Paginator object.<code>$pages = new Paginator;</code></li>
<li>Set the total items property of this new Paginator object.<code>$pages-&gt;items_total = X</code> where X is the total from step 2</li>
<li>Set the mid range property.The mid range is the number of pages that the paginator will display, centered around and including the selected page. For example, if the mid range is set to seven ($pages-&gt;mid_range = 7;) then when browsing page 50 of 100, the mid range generates links to pages 47, 48, 49, 50, 51, 52, and 53. The mid range moves in relation to the selected page. If the user is at either the low or high end of the list of pages, it will slide  the range toward the other side to accommodate the position. For example, if  the user visits page 99 of 100, the mid range will generate links for pages  94, 95, 96, 97, 98, 99, and 100.<code>$pages-&gt;mid_range = X;</code> where X is odd and &gt;=3. The default mid_range property is 7.</li>
<li>That’s all for the configuration, now you can generate page numbers. To paginate, call the pagination method (<em>$pages-&gt;paginate();</em>). This generates no output so to show the page numbers call the display method ($pages-&gt;display_pages();).<br />
<strong>You MUST call the paginate method before calling the display method</strong>. You can call the display method more than once which can be handy for display page numbers both above and below a result set.<code>echo $pages-&gt;display_pages();</code></li>
<li>Optionally, you can give the visitor features in addition to what is provided by the display method.
<ol>
<li>A drop down page jump menu is available via the display_jump_menu method (echo $pages-&gt;display_jump_menu();). This is a simple select menu that lists all page numbers. When a user changes the page number, the page will automatically switch to the selected page.</li>
<li>A drop down items per page menu is available via the display_items_per_page method(echo $pages-&gt;display_items_per_page();). This select menu allows visitors to change the number of items that are displayed on a page. By default, the list it generates is 10, 25, 50, 100, All. You can modify the class if needed to allow for other options however the All options is case-sensitive. Note that the drop down menu that this creates will not work if you also set the $items_per_page property. Using both at the same time doesn&#8217;t really make sense anyway</li>
</ol>
</li>
<li>Once the paginate method has been called (see step 6), you can execute your db query to fetch the limited result set (or all results). Paginate creates the SQL used to limit results automatically so you can edit your query to take advantage of this. For example, if your query before pagination was <code>SELECT id, name, address FROM table ORDER BY id ASC</code> you would change it to be <code>SELECT id, name, address FROM table ORDER BY id ASC $pages-&gt;limit</code>When a user selects the ‘All’ option, they effectively set $pages-&gt;limit to nothing and any page selected automatically creates the appropriate limit.<br />
<code>$pages-&gt;limit</code></li>
<li>Styles. The pagination class has been created so that it can be styles with CSS easily. The recommended CSS is<code>&lt;style type="text/css"&gt;<br />
.paginate {<br />
font-family: Arial, Helvetica, sans-serif;<br />
font-size: .7em;<br />
}<br />
a.paginate {<br />
border: 1px solid #000080;<br />
padding: 2px 6px 2px 6px;<br />
text-decoration: none;<br />
color: #000080;<br />
}<br />
a.paginate:hover {<br />
background-color: #000080;<br />
color: #FFF;<br />
text-decoration: underline;<br />
}<br />
a.current {<br />
border: 1px solid #000080;<br />
font: bold .7em Arial,Helvetica,sans-serif;<br />
padding: 2px 6px 2px 6px;<br />
cursor: default;<br />
background:#000080;<br />
color: #FFF;<br />
text-decoration: none;<br />
}<br />
span.inactive {<br />
border: 1px solid #999;<br />
font-family: Arial, Helvetica, sans-serif;<br />
font-size: .7em;<br />
padding: 2px 6px 2px 6px;<br />
color: #999;<br />
cursor: default;<br />
}<br />
&lt;/style&gt;<br />
</code></li>
<li>Basic example:<code>&lt;!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”<br />
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;<br />
&lt;html xmlns=”http://www.w3.org/1999/xhtml”&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1? /&gt;<br />
&lt;title&gt;Paginator&lt;/title&gt;<br />
&lt;style type=”text/css”&gt;<br />
.paginate {<br />
font-family: Arial, Helvetica, sans-serif;<br />
font-size: .7em;<br />
}<br />
a.paginate {<br />
border: 1px solid #000080;<br />
padding: 2px 6px 2px 6px;<br />
text-decoration: none;<br />
color: #000080;<br />
}<br />
a.paginate:hover {<br />
background-color: #000080;<br />
color: #FFF;<br />
text-decoration: underline;<br />
}<br />
a.current {<br />
border: 1px solid #000080;<br />
font: bold .7em Arial,Helvetica,sans-serif;<br />
padding: 2px 6px 2px 6px;<br />
cursor: default;<br />
background:#000080;<br />
color: #FFF;<br />
text-decoration: none;<br />
}<br />
span.inactive {<br />
border: 1px solid #999;<br />
font-family: Arial, Helvetica, sans-serif;<br />
font-size: .7em;<br />
padding: 2px 6px 2px 6px;<br />
color: #999;<br />
cursor: default;<br />
}<br />
&lt;/style&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;?php<br />
require_once ‘paginator.class.php’;<br />
// Make your database connection here and retrieve your total number of<br />
items (i.e. SELECT COUNT(*) FROM…)<br />
$pages = new Paginator;<br />
$pages-&gt;items_total = $db_count;<br />
$pages-&gt;mid_range = 7;<br />
$pages-&gt;paginate();echo $pages-&gt;display_pages();<br />
// Make your db query here. Include $pages-&gt;limit as described in step 8.<br />
(i.e. SELECT id,fname,lname FROM employees $pages-&gt;limit)<br />
echo $pages-&gt;display_pages(); // Optional call which will display the page numbers after the results.<br />
echo $pages-&gt;display_jump_menu(); // Optional – displays the page jump menu<br />
echo $pages-&gt;display_items_per_page(); //Optional – displays the items per<br />
page menu<br />
?&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</code></li>
</ol>
<p>All in all you could add pagination to any database driven set of results with about five lines of code. To display the current page number in the format ‘Page X of Y’ where X is the current page and Y is the total number of pages, use the code:</p>
<p><code>echo "&lt;p class=\"paginate\"&gt;Page: $pages-&gt;current_page of $pages-&gt;num_pages&lt;/p&gt;\n";</code></p>
<p>To display the SQL that was generated, use the code:</p>
<p><code>echo "&lt;p class=\"paginate\"&gt;SELECT * FROM table $pages-&gt;limit (retrieve<br />
records $pages-&gt;low-$pages-&gt;high from table - $pages-&gt;items_total item total / $pages-&gt;items_per_page<br />
items per page)";</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/feed/</wfw:commentRss>
		<slash:comments>233</slash:comments>
		</item>
		<item>
		<title>Ternary Operators (or How to Streamline an If/Else Statement)</title>
		<link>http://www.catchmyfame.com/2007/06/12/ternary-operators-or-how-to-streamline-an-ifelse-statement/</link>
		<comments>http://www.catchmyfame.com/2007/06/12/ternary-operators-or-how-to-streamline-an-ifelse-statement/#comments</comments>
		<pubDate>Wed, 13 Jun 2007 01:49:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.catchmyfame.com/2007/06/12/ternary-operators-or-how-to-streamline-an-ifelse-statement/</guid>
		<description><![CDATA[PHP, like most programming or scripting language allows for comparison operations bu using if/else. It&#8217;s a staple of any good language yet at the same time you can end up writing a lot of code in order to accomplish something very simple. Take for example this simple comparison: if (empty($_POST['action'])) { $action = 'default'; } [...]]]></description>
			<content:encoded><![CDATA[<p>PHP, like most programming or scripting language allows for comparison operations bu using if/else. It&#8217;s a staple of any good language yet at the same time you can end up writing a lot of code in order to accomplish something very simple. Take for example this simple comparison:</p>
<p><code>if (empty($_POST['action']))<br />
{<br />
$action = 'default';<br />
}<br />
else<br />
{<br />
$action = $_POST['action'];<br />
}</code></p>
<p>All we&#8217;re doing here is assigning one value to $action if the form variable action isn&#8217;t empty, and different variable if it is. This took up eight lines of code (OK, so you could argue that you could lower that by moving braces around but the code gets ugly fast). How can we clean this up? Enter ternary operators!<br />
What&#8217;s a ternary operator you ask? In a nutshell, a ternary operator takes three elements and combines them into one. The basic syntax is (expression 1) ? (expression 2) : (expression 3); which means do expression 2 if expression 1 is true, otherwise do expression 3. It&#8217;s just an if/else condition rewritten in one simple line. Taking our previous example, we could consolidate it to this:</p>
<p><code>$action = (empty($_POST['action'])) ? 'default':$_POST['action'];</code></p>
<p>Voila! The exact same results in one easy to understand line. I am a huge fan of this shortcut and it comes in very handy when you have basic if/else conditions to check.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.catchmyfame.com/2007/06/12/ternary-operators-or-how-to-streamline-an-ifelse-statement/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

