<?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>Michael Gareth Morgan &#187; function</title>
	<atom:link href="http://www.michaelgarethmorgan.com/tags/function/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.michaelgarethmorgan.com</link>
	<description>Michaels website</description>
	<lastBuildDate>Tue, 03 Aug 2010 13:33:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using numbers to represent data</title>
		<link>http://www.michaelgarethmorgan.com/using-numbers-to-represent-data/</link>
		<comments>http://www.michaelgarethmorgan.com/using-numbers-to-represent-data/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 18:15:28 +0000</pubDate>
		<dc:creator>Michael Morgan</dc:creator>
				<category><![CDATA[Web development]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.michaelgarethmorgan.com/?p=115</guid>
		<description><![CDATA[I&#8217;m currently in the process of rebuilding one of my older sites from the ground up. One thing that the new version makes use of is a field in several database tables called &#8216;state&#8217;. Basically, this holds an integer value ranging from 0 upwards where each number represents a specific meaning. For example&#8230; 0: Inactive [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently in the process of rebuilding one of my older sites from the ground up. One thing that the new version makes use of is a field in several database tables called &#8216;state&#8217;. Basically, this holds an integer value ranging from 0 upwards where each number represents a specific meaning. For example&#8230;</p>
<blockquote><p>0: Inactive<br />
1: Active<br />
2: Processed<br />
3: Archived</p></blockquote>
<p>The reasons for doing this are quite simple; it saves space in the database and is also a lot easier (and nicer) to work with. </p>
<p>Now, previously, when for example I wanted to loop through each row and display the textual meaning of each of the values I would either use a switch statement or a big if-else statement block. Now though I make use of a much simpler method&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> number_state<span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$labels</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$labels</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$state</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Unknown&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$text</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This function simply takes two parameters. The first is the number itself; the second is an array of the corresponding value meanings. To use it just do this&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> number_state<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;State zero&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;State one&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;State two&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;State three&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Or, in a simple loop&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> number_state<span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;State zero&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;State one&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;State two&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;State three&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s a nice and simple solution to a simple problem. From my perspective it is much better having something like this compared to big chunks of code doing the same thing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.michaelgarethmorgan.com/using-numbers-to-represent-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
