<?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>Chuck&#039;s Musings &#187; Programming</title>
	<atom:link href="http://blog.chuckcerrillo.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.chuckcerrillo.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 21 Feb 2010 18:43:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to: Insert a new field into a dynamic array</title>
		<link>http://blog.chuckcerrillo.com/2009/09/how-to-insert-a-new-field-into-a-dynamic-array/</link>
		<comments>http://blog.chuckcerrillo.com/2009/09/how-to-insert-a-new-field-into-a-dynamic-array/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 01:59:14 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[PHP and MySQL]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.chuckcerrillo.com/?p=103</guid>
		<description><![CDATA[Inserting new fields into a dynamic array.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been busy with work &#8211; deadlines and all, so it&#8217;s been a while since my last entry. For now I&#8217;ll just share a quick tip. How to <em>Insert a new field into a dynamic array</em>.</p>
<p>If you&#8217;ve been reading the code snippets I&#8217;ve been posting thus far, you&#8217;ll notice that I am doing this thing a lot. Anyway, here&#8217;s a little background on this situation.</p>
<p>Let&#8217;s say you just retrieved records from your database table and stored them into an associative array named <code class="codecolorer php default"><span class="php"><span style="color: #000088;">$results</span></span></code>, now you want to insert an extra field for each row but you don&#8217;t know how to to assign them since you don&#8217;t know how to walk through the array.</p>
<p>Let&#8217;s say you have this array:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Array<br />
(<br />
&nbsp; &nbsp; [0] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; Chuck<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 3<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 1984-09-15<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
&nbsp; &nbsp; [1] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 2<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; Blah<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 2001-01-01<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
&nbsp; &nbsp; [2] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 3<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; New user<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 2002-10-30<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
&nbsp; &nbsp; [3] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 4<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; Random guy<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 1992-07-21<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
)</div></div>
<p>This means, each row has the following fields &#8211; user_id, user_name, user_level, and birthdate.<br />
Now, let&#8217;s say we wanted to create a new field named &#8220;age&#8221; and store it in the same array. Here&#8217;s my preferred method of doing this:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$index</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000088;">$results</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'age'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> get_age<span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'birthdate'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Take note that to maintain the abstraction of the code, I just called a function named <code class="codecolorer php default"><span class="php">get_age<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code>. Remember that there is no such function so you&#8217;ll have to code that in to make it work. For completeness sake, here&#8217;s how I&#8217;d probably do it:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// Accepts 2 parameters, $bday and $now. $bday is of </span><br />
<span style="color: #666666; font-style: italic;">// the 'YYYY-MM-DD' format. The result is based on the </span><br />
<span style="color: #666666; font-style: italic;">// difference between $now and $bday converted to</span><br />
<span style="color: #666666; font-style: italic;">// years. $now is optional. If it is not supplied, it uses </span><br />
<span style="color: #666666; font-style: italic;">// the current time. This is not exactly accurate since I</span><br />
<span style="color: #666666; font-style: italic;">// just indiscriminately used 365.25 days in my calculation.</span><br />
<span style="color: #666666; font-style: italic;">// To improve, I'll have to detect leap years and just</span><br />
<span style="color: #666666; font-style: italic;">// add 1 day to that, instead of 1/4 days to every year.</span><br />
<span style="color: #000000; font-weight: bold;">function</span> get_age<span style="color: #009900;">&#40;</span><span style="color: #000088;">$bday</span><span style="color: #339933;">,</span> <span style="color: #000088;">$now</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000088;">$bday</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/strtotime"><span style="color: #990000;">strtotime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bday</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000088;">$now</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$now</span><span style="color: #009900;">&#41;</span>?<a href="http://www.php.net/strtotime"><span style="color: #990000;">strtotime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$now</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><a href="http://www.php.net/mktime"><span style="color: #990000;">mktime</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <a href="http://www.php.net/floor"><span style="color: #990000;">floor</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$now</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$bday</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span>60<span style="color: #339933;">*</span>60<span style="color: #339933;">*</span>24<span style="color: #339933;">*</span>365<span style="color: #339933;">.</span>25<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Anyway, after doing all that, you will get this result:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Array<br />
(<br />
&nbsp; &nbsp; [0] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; Chuck<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 3<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 1984-09-15<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] =&gt; 24<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
&nbsp; &nbsp; [1] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 2<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; Blah<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 2001-01-01<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] =&gt; 8<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
&nbsp; &nbsp; [2] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 3<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; New user<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 2002-10-30<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] =&gt; 6<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
&nbsp; &nbsp; [3] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 4<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; Random guy<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 1992-07-21<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] =&gt; 17<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
)</div></div>
<p>As an aside, you could probably use <a href="http://us.php.net/manual/en/function.array-walk.php"><code class="codecolorer php default"><span class="php"><a href="http://www.php.net/array_walk"><span style="color: #990000;">array_walk</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code></a> to achieve something similar, but this method is way simpler. I hope this has been helpful to you!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chuckcerrillo.com/2009/09/how-to-insert-a-new-field-into-a-dynamic-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Juggling time zones</title>
		<link>http://blog.chuckcerrillo.com/2009/09/juggling-time-zones/</link>
		<comments>http://blog.chuckcerrillo.com/2009/09/juggling-time-zones/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 00:30:14 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[PHP and MySQL]]></category>
		<category><![CDATA[gmdate]]></category>
		<category><![CDATA[gmt]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[offsets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[strtotime]]></category>
		<category><![CDATA[timestamp]]></category>
		<category><![CDATA[timezones]]></category>
		<category><![CDATA[utc]]></category>

		<guid isPermaLink="false">http://blog.chuckcerrillo.com/?p=100</guid>
		<description><![CDATA[If you're thinking of writing calendars, you better watch out for timezones.]]></description>
			<content:encoded><![CDATA[<p>Handling times for different timezones is one of the usual things I had to face with every project that has in one way or the other involves the element of time.</p>
<p>The usual projects I work on store dates into a <code class="codecolorer sql default"><span class="sql">datetime</span></code> field in the database. This means that the date must use the <code class="codecolorer text default"><span class="text">YYYY-MM-DD HH:MM:SS</span></code> format (e.g. 2009-09-03 07:45:00).</p>
<p>One of the first things you&#8217;d want to do is to let the user specify a timezone setting for that user account (assuming this is in a multi-user environment, as most web applications are). You could view all the timezones using this code:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ul&gt;'</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span>DateTimeZone<span style="color: #339933;">::</span><span style="color: #004000;">listAbbreviations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$timezone_abbreviation</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$timezones</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$timezones</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$timezone</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$timezone</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timezone_id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$timezone_abbreviation</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' - '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$timezone</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timezone_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ul&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;li&gt;Uses DST: '</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$timezone</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dst'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">===</span><span style="color: #009900; font-weight: bold;">TRUE</span>?<span style="color: #0000ff;">'Yes'</span><span style="color: #339933;">:</span><span style="color: #0000ff;">'No'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;li&gt;Offset: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$timezone</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'offset'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/ul&gt;&lt;/li&gt;'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ul&gt;'</span><span style="color: #339933;">;</span></div></div>
<p>You could modify the code above to create a drop-down list of the different timezones to make it easier for the user to pick a timezone (because simply writing GMT+8 or UTC+8 might be confusing for some people).</p>
<p>Next, we will have to convert all of our stored dates into a single timezone, for simplicity, I use GMT/UTC as a reference timezone (since it is easy to add timezone offsets to it). If you store your dates in a <code class="codecolorer sql default"><span class="sql">datetime</span></code> field, you can perform the following conversion after retrieving the timestamp from the database:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// let's assume $parsedtime is the entry we have in the database</span><br />
<span style="color: #666666; font-style: italic;">// and $offset is the timezone offset we got based on the user's settings</span><br />
<span style="color: #000088;">$parsedtime</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'2009-09-15 12:00:00'</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> 8<span style="color: #339933;">*</span>60<span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this is GMT +8</span><br />
<span style="color: #666666; font-style: italic;">// get the GMT counterpart of the parsedtime</span><br />
<span style="color: #000088;">$unix_time</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/strtotime"><span style="color: #990000;">strtotime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parsedtime</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' GMT'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/gmdate"><span style="color: #990000;">gmdate</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'m d Y h:iA'</span><span style="color: #339933;">,</span><span style="color: #000088;">$unix_time</span><span style="color: #339933;">-</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// displays: 09 15 2009 04:00AM</span></div></div>
<p>So that means, 12nn of September 15, 2009 is 4am of September 15, 2009 in GMT/UTC.</p>
<p>Let&#8217;s say we had a Calendar application, and you wanted to set an alarm for 6pm on that same day. You could do the something like the this (assuming you wrote some sort of class for this thing):</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$reminder</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Reminder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'2009-09-15 18:00:00'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Dinner with the family'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$reminder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timeLeft</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Since the current date and time was 12nn of September 15, 2009, this means that the above reminder will display &#8220;6 hours left&#8221;, depending on how the <code class="codecolorer php default"><span class="php">timeLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> method is formatted.</p>
<p>If we didn&#8217;t convert the date into a standard timezone, this will force our functions to use the server&#8217;s timezone setting and that might not be what you want.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chuckcerrillo.com/2009/09/juggling-time-zones/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Regular expressions in MySQL</title>
		<link>http://blog.chuckcerrillo.com/2009/09/regular-expressions-in-mysql/</link>
		<comments>http://blog.chuckcerrillo.com/2009/09/regular-expressions-in-mysql/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 05:19:32 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[PHP and MySQL]]></category>
		<category><![CDATA[chuck cerrillo]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.chuckcerrillo.com/?p=92</guid>
		<description><![CDATA[Using regular expressions in your SQL statements helps to keep it simple.]]></description>
			<content:encoded><![CDATA[<p><a href="http://mysql.com"><img style="float: left" src="http://dev.mysql.com/common/logos/logo_mysql_sun_a.gif" alt="MySQL" /></a> I&#8217;ve been using MySQL for the better part of the past 6 years so it comes as a pleasant surprise when I found out last month that it can do regular expressions within its SQL!</p>
<p>I&#8217;m not sure about the other databases, the last time I&#8217;ve used Oracle was around 2004-2005 during my college days, so I can&#8217;t really say if this is a MySQL-exclusive feature or something along those lines. I&#8217;m pretty positive that this isn&#8217;t standard SQL though, since we never had such lessons back in college.</p>
<p>Anyway, if you&#8217;re familiar with SQL and string matching within SQL, it makes use of the <code class="codecolorer sql default"><span class="sql"><span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'[string]'</span></span></code> clause. Where [string] is the string to be searched for. Optionally, you could also use wildcards like _ (underscore), % (percent), [charlist] (character list) or [^charlist] (negated character list) or a combination of those to match string fragments. Unfortunately this has also been one of the major points of entry for hacking/hijacking a database-driven website, via <a href="http://en.wikipedia.org/wiki/SQL_injection">SQL injection</a>. Due to this vulnerability, I&#8217;ve either been using heavy data validation, or store encoded data, or at times, avoiding this altogether&#8230; but I digress.</p>
<p>Using regex in my SQL queries is a godsend. It helps reduce my data processing and validation overhead.</p>
<p>MySQL supports the use of almost all POSIX regex  metacharacters via the <code class="codecolorer sql default"><span class="sql"><span style="color: #993333; font-weight: bold;">REGEXP</span></span></code> or <code class="codecolorer sql default"><span class="sql"><span style="color: #993333; font-weight: bold;">RLIKE</span></span></code> clause (or a negation using <code class="codecolorer sql default"><span class="sql"><span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">REGEXP</span></span></code> or <code class="codecolorer sql default"><span class="sql"><span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">RLIKE</span></span></code>).</p>
<p><strong>The following list describes some characteristics of extended regular expressions:</strong></p>
<ul>
<li>“.” matches any single character.</li>
<li>A character class “[...]” matches any character within the brackets. For example, “[abc]” matches “a”, “b”, or “c”. To name a range of characters, use a dash. “[a-z]” matches any letter, whereas “[0-9]” matches any digit.</li>
<li>“*” matches zero or more instances of the thing preceding it. For example, “x*” matches any number of “x” characters, “[0-9]*” matches any number of digits, and “.*” matches any number of anything.</li>
<li>A REGEXP pattern match succeeds if the pattern matches anywhere in the value being tested. (This differs from a LIKE pattern match, which succeeds only if the pattern matches the entire value.)</li>
<li>To anchor a pattern so that it must match the beginning or end of the value being tested, use “^” at the beginning or “$” at the end of the pattern.</li>
</ul>
<p>Let&#8217;s start with the traditional <code class="codecolorer sql default"><span class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">...</span>  <span style="color: #993333; font-weight: bold;">LIKE</span></span></code> query. Here&#8217;s a query that selects all animals whose name begins with &#8220;ant&#8221;</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> name<br />
<br />
<span style="color: #993333; font-weight: bold;">FROM</span> animals<br />
<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'ant%'</span></div></div>
<p>The results could be something like this:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">name<br />
<br />
<span style="color: #808080; font-style: italic;">---------</span><br />
<br />
ant<br />
<br />
anteater<br />
<br />
antelope<br />
<br />
<span style="color: #66cc66;">...</span></div></div>
<p>A regex version of that would be:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> name<br />
<br />
<span style="color: #993333; font-weight: bold;">FROM</span> animals<br />
<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #993333; font-weight: bold;">REGEXP</span> <span style="color: #ff0000;">'^ant'</span></div></div>
<p>So far they look similar&#8230; but, say, what if you wanted to use a complex rule like: &#8220;select all animals whose names starts with either &#8216;a&#8217; or &#8216;c&#8217; and ends with either &#8216;t&#8217; or &#8216;p&#8217;.  It would look messy if you do it this way:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> name<br />
<br />
<span style="color: #993333; font-weight: bold;">FROM</span> animals<br />
<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'a%t'</span><br />
<br />
<span style="color: #993333; font-weight: bold;">OR</span> name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'a%p'</span><br />
<br />
<span style="color: #993333; font-weight: bold;">OR</span> name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'c%t'</span><br />
<br />
<span style="color: #993333; font-weight: bold;">OR</span> name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'c%p'</span></div></div>
<p>However with regex, it is as simple as this:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> name<br />
<br />
<span style="color: #993333; font-weight: bold;">FROM</span> animals<br />
<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #993333; font-weight: bold;">REGEXP</span> <span style="color: #ff0000;">'^[ac].*[tp]$'</span></div></div>
<p>Imagine if your filtering conditions were much more complex. It&#8217;s not hard to see how regexp can help with that!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chuckcerrillo.com/2009/09/regular-expressions-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A neat and clean CSS menu</title>
		<link>http://blog.chuckcerrillo.com/2009/08/a-neat-and-clean-css-menu/</link>
		<comments>http://blog.chuckcerrillo.com/2009/08/a-neat-and-clean-css-menu/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 07:13:22 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[HTML and CSS]]></category>
		<category><![CDATA[chuck cerrillo]]></category>
		<category><![CDATA[clean]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[list menu]]></category>
		<category><![CDATA[markup]]></category>
		<category><![CDATA[menu]]></category>
		<category><![CDATA[neat]]></category>

		<guid isPermaLink="false">http://blog.chuckcerrillo.com/?p=74</guid>
		<description><![CDATA[Want a functioning navigation bar without using JavaScript? Read on...]]></description>
			<content:encoded><![CDATA[<p>Earlier this week, our team of developers was temporarily reassigned to help with another team&#8217;s debugging tasks due to a pressing deadline. Our front-end team was busy and only some of us from the back-end team were free to help.</p>
<p>The framework was based on Magento. Since our teams were all CodeIgniter developers, we found it hard to adapt to that framework. We were shocked to find out that majority of the fixes were front-end related, requiring mostly CSS and JS to fix. This was because navigating through Magento&#8217;s files was a pain. In addition, most of my teammates and colleagues from the other teams avoided the CSS bits because they either had limited CSS knowledge or were simply not confident enough to manipulate that website&#8217;s huge CSS file. So I took that task.</p>
<p>The website we were working on had a 3-tier navigation menu on top, controlled by JavaScript. I spent the better part of the day on that navigation bar building the content for the other menu and tackling the JS browser incompatibility problems&#8230; I previously mentioned in another entry that I suck at JS, needless to say what I did was a quick and dirty hack that works but one I&#8217;m not proud of, haha.</p>
<p>Anyway, 2 days later (that&#8217;s today), I was thinking &#8211; I bet it&#8217;s possible to mimic that navigation bar using purely CSS and no JS. So I set out and tried my hand at it. I relied heavily on nested  declarations in order to keep the styles neat and the markup less cluttered. Here&#8217;s the stylesheet I came up with:</p>
<div class="codecolorer-container css default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">html<span style="color: #00AA00;">,</span> body <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<span style="color: #cc00cc;">#navigation</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#888</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#EEE</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#444</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">22px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">&quot;Trebuchet MS&quot;</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">30px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<span style="color: #cc00cc;">#navigation</span><span style="color: #00AA00;">&gt;</span>li <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">pointer</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<span style="color: #cc00cc;">#navigation</span> li<span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#navigation</span><span style="color: #00AA00;">&gt;</span>li<span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">&gt;</span>a <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#FFF</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<span style="color: #cc00cc;">#navigation</span> li a<span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#navigation</span> li ul <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<span style="color: #cc00cc;">#navigation</span> li a<span style="color: #3333ff;">:first-letter </span><span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">blue</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #cc00cc;">#navigation</span> li ul <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">white</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<span style="color: #cc00cc;">#navigation</span> li ul li <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1em</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">padding-right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1em</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<span style="color: #cc00cc;">#navigation</span> li<span style="color: #3333ff;">:hover </span>ul <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<span style="color: #cc00cc;">#navigation</span> li ul li<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></div>
<p>As for the menu&#8217;s markup itself, it consists of a master <code class="codecolorer html4strict default"><span class="html4strict"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span></span></code> with an id of &#8220;navigation&#8221;, with nested <code class="codecolorer html4strict default"><span class="html4strict"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span></span></code>s contained inside of it.</p>
<p>Here&#8217;s what the markup looks like:</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;navigation&quot;</span>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;&lt;<a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Dogs<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Dog 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Dog 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Dog 3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Cats<br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Cat 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Cat 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Cat 3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Cat 4<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Cat 5<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Pigs<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span>Place your content here<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span></div></div>
<p>The markup looks really neat and simple with none of those pesky <code class="codecolorer html4strict default"><span class="html4strict">class=&quot;&quot;</span></code> attributes to get in the way. Also you can add as much menu items as you please.</p>
<p>So far this CSS declaration only supports 2-tier menus. I could try working on 3-tier, or even maybe a dynamic-depth CSS menu, if I don&#8217;t feel lazy :p.</p>
<p>You could see it in action by <a href="http://thedirtlab.chuckcerrillo.com/html-and-css/css navbar/navbar.html">viewing this page</a>.</p>
<p>Alternately, if you&#8217;re not a fan of horizontal menus, you can easily convert it into a vertical menu by removing the <code class="codecolorer css default"><span class="css"><span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span></span></code> property in the <code class="codecolorer css default"><span class="css"><span style="color: #cc00cc;">#navigation</span> li ul li</span></code> declaration. Getting rid of the white background should be easy.</p>
<p>Update: August 29, 2009</p>
<p>I wrote another CSS declaration for 3-tier menus in vertical format. You could see the sample <a href="http://thedirtlab.chuckcerrillo.com/html-and-css/css navbar/navbar_vertical.html">by going here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chuckcerrillo.com/2009/08/a-neat-and-clean-css-menu/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Sorting uneven 2-dimensional arrays</title>
		<link>http://blog.chuckcerrillo.com/2009/08/sorting-uneven-2-dimensional-arrays/</link>
		<comments>http://blog.chuckcerrillo.com/2009/08/sorting-uneven-2-dimensional-arrays/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 16:09:24 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[PHP and MySQL]]></category>
		<category><![CDATA[2-dimensional arrays]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[multisort]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://blog.chuckcerrillo.com/?p=60</guid>
		<description><![CDATA[Is array_multisort() not working for you? You might want to try this.]]></description>
			<content:encoded><![CDATA[<p>I was working on a search function for the module I was assigned to. The contents stored in the database isn&#8217;t plaintext, but rather encoded in base64. There&#8217;s currently no native MySQL functions to handle base64 data so that means I can&#8217;t make inline text and regex comparisons.</p>
<p>I thought hard about it and came up with 2 solutions:</p>
<ol>
<li>Save the data in plaintext instead and use heavy data validation</li>
<li>Retrieve all rows and store them in a PHP array and do my regex magic there</li>
</ol>
<p>Option 1 was out of the question, because in every part of the module, we&#8217;ve made sure to protect against XSS (<a href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross site scripting</a>). Going with this option will always risk creating a security hole regardless of how carefully I validate the supplied data.</p>
<p>Option 2 would be more secure and would allow me to apply my regex skills to the fullest extent, but it would cost more memory overhead because I&#8217;d have to retrieve the rows, store them into memory while doing the pattern matching&#8230;</p>
<p>Until I thought of <em>option 3 </em>(well, more like option 2.1). What I&#8217;d do is to limit the fetched rows by imposing more conditions, say for example, <em>retrieve the rows that the currently logged user is only allowed to see</em>. Then perform my regex thingamabob on those filtered results.</p>
<p>I had another problem though, doing this means I have to work on some sort of system to weigh the <em>relevance</em> of the results. Since this <em>relevance value</em> is computed after the database retrieval, this means I&#8217;ll be adding it into an extra column in the result array. The result array I am supposed to work on is a multidimensional array. It would have a similar structure as this example:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Chuck'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Blue'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Rupert'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Black'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Cerrillo'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Red'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Abarro'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'White'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>The above array is a 2-dimensional array.  This means that <code class="codecolorer php default"><span class="php"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></span></code> will display <em><strong>&#8220;Chuck&#8221;</strong></em>.</p>
<p>Anyway, as I earlier mentioned, I am supposed to add a <em>relevance value</em> to each row (or what I&#8217;d like to call <em>weight</em>). In addition, I might add some custom fields to some rows. Here&#8217;s how that array would look like with the additional fields and arbitrary weight values. (take note of the custom field added in line #6)</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'weight'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Chuck'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Blue'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'custom'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Boo!'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'weight'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Rupert'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Black'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'weight'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Cerrillo'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Red'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'weight'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Abarro'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'White'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>By my weight system, the &#8220;heavier&#8221; a row is, the more relevant it is to the search query. So I should sort it by weight in a descending order. </p>
<p>PHP has a native array function called <code class="codecolorer php default"><span class="php"><a href="http://www.php.net/array_multisort"><span style="color: #990000;">array_multisort</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code>, so I tried that one. It doesn&#8217;t work with my array because the array sizes are not the same. I gave up a while after looking for workarounds to get it to work with that function, so I decided to write my own sorting function for arrays like these.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
* 2-Dimensional Array Sort by Chuck Cerrillo (August 26, 2009)<br />
* Sorts a 2-dimensional array according to a given field/index.<br />
* This is particularly useful for sorting post-processed MySQL data (i.e. where <br />
* custom fields have been added AFTER the retrieval of table data, and you'd need <br />
* to resort the results).<br />
*<br />
* For more information on how to use this array, please visit my blog entry at<br />
* http:///blog.chuckcerrillo.com/2009/08/sorting-uneven-2-dimensional-arrays/<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">function</span> array_2dsort<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sort</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'asc'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000088;">$temp</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: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// First we grab the index names of the sub-array</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// by walking through the array</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$subarrayindex</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$subarray</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$subarray</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$subarray</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$index</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Store the fieldnames into the $fieldnames array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$fieldnames</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$subarray</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// We then store the field names into a temporary array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#123;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$subarrayindex</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Sort the temporary array based according to the $sort</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// argument, and maintain index associations by using </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// asort() or arsort()</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sort</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'asc'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #990000;">asort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$$field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #990000;">arsort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$$field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Now we recreate a new array with the desired order</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldnames</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$fieldname</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$fieldindex</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldindex</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$rowindex</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$rowvalue</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#123;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$index</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$fieldname</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$fieldname</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now this one works perfectly according to my needs. This function takes in 3 parameters,</p>
<ul>
<li><code class="codecolorer php default"><span class="php"><span style="color: #000088;">$array</span></span></code> &#8211; the array to sort (required)</li>
<li><code class="codecolorer php default"><span class="php"><span style="color: #000088;">$field</span></span></code> &#8211; the field or index to sort by (required)</li>
<li><code class="codecolorer php default"><span class="php"><span style="color: #000088;">$sort</span></span></code> &#8211; the sorting order, either asc or desc, defaults to asc (optional)</li>
</ul>
<p>Given the above array, I would call my function this way &#8211; <code class="codecolorer php default"><span class="php"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> array_2dsort<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'weight'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code>. If you would do a <code class="codecolorer php default"><span class="php"><span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code>, it would give you this: (note that I didn&#8217;t specify &#8216;desc&#8217; as my sorting order)</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #990000;">Array</span><br />
<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span>1<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>weight<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Rupert<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>color<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Black<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>custom<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>weight<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 20<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Chuck<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>color<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Blue<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>custom<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Boo<span style="color: #339933;">!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span>3<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>weight<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 40<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Abarro<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>color<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> White<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>custom<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span>2<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>weight<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">50</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Cerrillo<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>color<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Red<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>custom<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
<br />
<span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>This function <em>automagically</em> adds blank fields to rows that do not contain them.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chuckcerrillo.com/2009/08/sorting-uneven-2-dimensional-arrays/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Regex saves the day, again</title>
		<link>http://blog.chuckcerrillo.com/2009/08/regex-saves-the-day-again/</link>
		<comments>http://blog.chuckcerrillo.com/2009/08/regex-saves-the-day-again/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 22:21:08 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[PHP and MySQL]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[search tool]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.chuckcerrillo.com/?p=44</guid>
		<description><![CDATA[Searching is easier with regex... Good thing MySQL supports that in SQL queries too!]]></description>
			<content:encoded><![CDATA[<p>I was supposed to do a &#8220;search&#8221; feature and a &#8220;related items&#8221; feature for the module I was assigned to. This was were my regex skills came in handy yet again.</p>
<p>I made a very barebone prototype of it, which takes some input text for the search query against some stored content, in this case, a full-text article (although the final version should be against a database).</p>
<p>The prototype I made is still very rough around the edges. What it does is that it extracts all keywords from the search query using this expression:</p>
<p><div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$regex</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/\b([\p{L}|\p{Ll}|\p{Lu}|_]+?)\b/i'</span><span style="color: #339933;">;</span></div></div>
</p>
<p>That rule can be translated as follows:</p>
<p style="padding-left: 30px;"><em><span style="color: #993366;">take any combination of unicode characters and underscores that are enclosed in word boundaries </span></em></p>
<p>Having done that, I place all matched data into an array and call that my &#8220;keywords&#8221; array, to be used later.</p>
<p>The next thing I did was to chop the article down into smaller pieces, currently by sentence (I only used periods as a delimiter, I should probably include other punctuation marks I guess). Then I ran <em>preg_match()</em> through each piece to quickly check for matches for any of the keywords. These results are then compiled.</p>
<p>In each compiled piece, I assign a corresponding weight. This weight is my arbitrary way of picking the best match. My currently implementation <em>sets weight to be equal to the number of characters in the piece that are matched with keywords</em>. I still have to refine these rules later on.</p>
<p>When all pieces have their weight assigned, I sort them according to weight in descending order (highest to lowest weight). Then I display the results and highlight the matched characters.</p>
<p>Here&#8217;s the prototype I made: <a href="http://thedirtlab.chuckcerrillo.com/php-and-mysql/search.php">search tool</a>.</p>
<p>I&#8217;m still thinking of the refinements I could make so I&#8217;ll just post them here as I go along.</p>
<p>Also, it&#8217;s good to know that MySQL supports regex in your SQL queries, this should save me a lot of time!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chuckcerrillo.com/2009/08/regex-saves-the-day-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Picking a JavaScript framework</title>
		<link>http://blog.chuckcerrillo.com/2009/08/picking-a-javascript-framework/</link>
		<comments>http://blog.chuckcerrillo.com/2009/08/picking-a-javascript-framework/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 18:13:06 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[Javascript and AJAX]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JS]]></category>
		<category><![CDATA[Mootools]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.chuckcerrillo.com/?p=34</guid>
		<description><![CDATA[Prototype? Scriptaculous? jQuery? Mootools? What to use?]]></description>
			<content:encoded><![CDATA[<p>I had my own little experiences wrestling with JavaScript early into my webdevelopment days, but I never really went beyond simple DOM manipulation. JavaScript has always been overwhelming for me especially because I&#8217;d have to write different versions of the code as well as workarounds to ensure compatibility with most browsers&#8230; and I didn&#8217;t like rewriting stuff when I had to consider yet another browser.</p>
<p>Now,  a few years later, here I am again trying to get my feet wet with JS but at least there are several frameworks readily available to guide me through it, and take care of my problems (a pity they can&#8217;t help with personal problems, heh). I&#8217;m trying to choose from among the popular JS frameworks &#8211; Prototype, Scriptaculous, jQuery and Mootools. Based on my limited research, many devs prefer Mootools over the others, so I am thinking of starting with that.</p>
<p>So let me just formally state this before I begin&#8230;</p>
<p><strong><a href="http://www.mootools.net/">Mootools</a></strong>, I choose you!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chuckcerrillo.com/2009/08/picking-a-javascript-framework/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

