<?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>The Journal of Tim Roberts &#187; PHP</title>
	<atom:link href="http://www.wiseguysonly.com/category/web-development/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wiseguysonly.com</link>
	<description>PHP,MySQL and jQuery Developer</description>
	<lastBuildDate>Sun, 20 Jun 2010 17:12:02 +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>A workaround for the CakePHP alphaNumeric issue</title>
		<link>http://www.wiseguysonly.com/2009/11/27/a-workaround-for-the-cakephp-alphanumeric-issue/</link>
		<comments>http://www.wiseguysonly.com/2009/11/27/a-workaround-for-the-cakephp-alphanumeric-issue/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 14:50:51 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.wiseguysonly.com/?p=278</guid>
		<description><![CDATA[Recently whilst building a CakePHP app on a shiny new server I noticed some unexpected behaviour when using an alphaNumeric rule to validate a model. Whatever I passed into the model returned false. That is to say that even a valid alphanumeric string threw up an exception. (if you haven&#8217;t already dived into CakePHP, you [...]]]></description>
			<content:encoded><![CDATA[<p>Recently whilst building a CakePHP app on a shiny new server I noticed some unexpected behaviour when using an alphaNumeric rule to validate a model. Whatever I passed into the model returned false. That is to say that even a valid alphanumeric string threw up an exception. (if you haven&#8217;t already dived into <a href="http://www.cakephp.org">CakePHP</a>, you should &#8211; it&#8217;s a rapid development framework based on <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" title="Model View Controller">MVC</a>.)</p>
<p>The issue seems to be tied to PHP 5.1.2 and the way regular expressions are handled, however it seems the it may appear in other configurations too. Before I get to a workaround, note I haven&#8217;t levelled the &#8220;bug&#8221; word at either PHP nor CakePHP &#8211; for all I know it could be a difference of opinion on how something should be handled. What I do know is that I would rather harness CakePHP&#8217;s flexibility to create custom model rules than try upgrading my current live server PHP installation.</p>
<p>Here is an example of how you should be able to implement the alphaNumeric rule using validation in CakePHP based on a simpliefied model called &#8220;User&#8221;:<br />
class User extends AppModel</p>
<p>{<br />
<code>var $name = 'User';<br />
var $helpers = array('Form');<br />
var $validate = array(<br />
'username' =&gt; array(<br />
'loginRule-1' =&gt; array(<br />
<em> <strong>'rule' =&gt; 'alphaNumeric',</strong></em><br />
'last' =&gt; true,<br />
'message' =&gt; 'Only letters and numbers allowed'<br />
));<br />
}</code></p>
<p>The rule in bold italics above is the one that doesn&#8217;t always work. However this can be easily rectified with a custom rule built on a regular expression seeking out valid alphanumeric strings.</p>
<p>To achieve this the alphaNumeric rule just needs to be replaced with this:</p>
<p><code><strong><em>"rule" => array('custom', '/^[a-z0-9]*$/i'),</strong></em></code></p>
<p>Problem solved.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wiseguysonly.com/2009/11/27/a-workaround-for-the-cakephp-alphanumeric-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drag and drop reordering of database fields (sortables) with jQuery</title>
		<link>http://www.wiseguysonly.com/2008/12/07/drag-and-drop-reordering-of-database-fields-sortables-with-jquery/</link>
		<comments>http://www.wiseguysonly.com/2008/12/07/drag-and-drop-reordering-of-database-fields-sortables-with-jquery/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 18:21:17 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.wiseguysonly.com/?p=143</guid>
		<description><![CDATA[This tutorial explains how to display a list of items from a database that can be reordered in real time by dragging and dropping, using the jQuery library. Moreover, no page reload is required on every reorder. If you want a bigger introduction to why you may want to do this I suggest reading my [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial explains how to display a list of items from a database that can be reordered in real time by dragging and dropping, using the jQuery library. Moreover, no page reload is required on every reorder. If you want a bigger introduction to why you may want to do this I suggest reading my tutorial on <a href="/2006/08/11/drag-and-drop-reordering-of-database-fields-with-ajax/">how to achieve the same functionality using scriptaculous</a>.</p>
<p>Previously I have explained how to achieve this in <a href="/2006/08/11/drag-and-drop-reordering-of-database-fields-with-ajax/">Scriptaculous</a> and <a href="/2007/10/10/drag-and-drop-reordering-of-database-fields-sortables-with-mootools/">Mootools</a>.</p>
<p><span id="more-143"></span></p>
<p><strong>Step one: Include the jQuery libraries.</strong></p>
<p>Grab <a href="http://docs.jquery.com/Downloading_jQuery">jQuery</a> and <a href="http://ui.jquery.com/download_builder/">jQuery UI</a> libraries.</p>
<p>Include them in the head of your document:<br />
<code>&lt;<span class="start-tag">script</span><span class="attribute-name"> type</span>=<span class="attribute-value">"text/javascript" </span><span class="attribute-name">src</span>=<span class="attribute-value">"javascripts/jquery-1.2.6.min.js"</span>&gt;&lt;/<span class="end-tag">script</span>&gt;<br />
&lt;<span class="start-tag">script</span><span class="attribute-name"> type</span>=<span class="attribute-value">"text/javascript" </span><span class="attribute-name">src</span>=<span class="attribute-value">"javascripts/jquery-ui-personalized-1.5.2.min.js"</span>&gt;&lt;/<span class="end-tag">script</span>&gt;</code></p>
<p><strong>Step two: Build a list of items from the database.</strong></p>
<p>Give your list and id &#8211; I am using item_list for this tutorial &#8211; and then make sure each list item has an id of &#8220;item_PRIMARYKEY&#8221; where PRIMARYKEY is the key of the item from the database. Here is something similar to the list you should have:</p>
<p><code><br />
&lt;ul id="item_list" &gt;<br />
&lt;li id="item_1"&gt;Item One&lt;/li&gt;<br />
&lt;li id="item_2"&gt;Item Two&lt;/li&gt;<br />
&lt;li id="item_3"&gt;Item Three&lt;/li&gt;<br />
&lt;li id="item_4"&gt;Item Four&lt;/li&gt;<br />
&lt;/ul&gt;</code></p>
<p><strong>Step 3: Make the list sortable and add a callback to our database script.</strong></p>
<p>Using jQuery&#8217;s onload function or by adding the following script below our list,  inject some javascript into our list to make the list items sortable</p>
<p><code> $("#item_list").sortable({stop:function(i) {<br />
$.ajax({<br />
type: "GET",<br />
url: "server_items_reorder.php",<br />
data: $("#item_list").sortable("serialize")<br />
});</code></p>
<p>Let&#8217;s look at this line by line so you can understand what is happening.</p>
<p>Line one uses the jQuery selector to target the element with an id of item_list (our unordered list in this case). We then apply the sortable plugin from jQuery UI.</p>
<p><a href="http://docs.jquery.com/UI/Sortable/sortable#options">Sortable has a number of options available</a>, but in this case we are going to use the stop option (when sorting has finished) to make an ajax call. The important options in our ajax call are as follows:</p>
<ul>
<li>type: the type of call (in this case GET);</li>
<li>url: (where the ajax post should be made) in this case &#8220;server_items_reorder.php&#8221;;</li>
<li>data : the string the makes up our GET call;</li>
</ul>
<p>The data can be easily built up by using more of the inbuilt functionality of jQuery&#8217;s sortable:</p>
<p><code>$("#item_list").sortable("serialize")</code></p>
<p>This takes the current order of our list and builds it into an array that can be passed on like a GET string:</p>
<p>item[]=1&amp;item[]=2&amp;item[]=3&amp;item[]=4</p>
<p>jQuery automatically serializes the list item id&#8217;s  into this array so you can see why I chose to give them the id item_PRIMARYKEY for the ease offered by convention.</p>
<p>So now we are making a ajax call and padding on the new list order we can write a script to update the database.</p>
<p><strong>Step four: The database script (updating the position).</strong></p>
<p>I am assuming your database table has an extra field called &#8220;position&#8221; that you can use to hold an integer of each items position as relating to its peers. What we need to do now is create our server_items_reorder.php script to collect the array of items and update the database. Here is that script:</p>
<p><code>// YOUR DB CONNECTION HERE<br />
foreach($_GET['item'] as $key=&gt;$value) {<br />
mysql_query("UPDATE my_items" SET position = '" . $key . "' WHERE id ='" . $value . "'");<br />
}</code></p>
<p>This script is simple enough to be written in the language of your choice. It collects the array of items from the GET variable and for each one updates the position based on the id of the item.</p>
<p>That&#8217;s all there is to it.</p>
<p>This tutorial is pretty low level as I have written most of these concepts including troubleshooting in my  <a href="/2006/08/11/drag-and-drop-reordering-of-database-fields-with-ajax/">Scriptaculous</a> and <a href="/2007/10/10/drag-and-drop-reordering-of-database-fields-sortables-with-mootools/">Mootools</a> versions of these, so if you experience any difficulties look at those tutorials first, read the user comments and/or post any questions here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wiseguysonly.com/2008/12/07/drag-and-drop-reordering-of-database-fields-sortables-with-jquery/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Autocompletion with Scriptaculous and Ajax.</title>
		<link>http://www.wiseguysonly.com/2008/03/09/autocompletion-with-scriptaculous-and-ajax/</link>
		<comments>http://www.wiseguysonly.com/2008/03/09/autocompletion-with-scriptaculous-and-ajax/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 19:40:32 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Scriptaculous]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://wiseguysonly.com/?p=45</guid>
		<description><![CDATA[Demo here &#124; Download sample files
This tutorial supercedes my previous turtorial on autocompletion by adding further tips and tricks to make your autocompletion a much better experience for your site users The tutorial is structured in the following way:

Introduction: What is autocompletion and why use it?
How it works.
Getting scriptaculous and building the form.
Building the autocomplete [...]]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://wiseguysonly.com/demos/scriptaculous/ajax-autocompletion/autocomplete.php">Demo here</a> | <a href="http://wiseguysonly.com/downloads/autocompletion.zip">Download sample files</a></strong></p>
<p>This tutorial supercedes my previous turtorial on autocompletion by adding further tips and tricks to make your autocompletion a much better experience for your site users The tutorial is structured in the following way:</p>
<ol>
<li>Introduction: What is autocompletion and why use it?</li>
<li>How it works.</li>
<li>Getting scriptaculous and building the form.</li>
<li>Building the autocomplete server.</li>
<li>Passing back an id to our form.</li>
</ol>
<p><span id="more-45"></span></p>
<h4>1. What is autocompletion and why use it?</h4>
<p>I predict that most people will want to skip this section and get straight to the action, so I will keep it brief. Autocompletion is a means of helping your users when filling in forms. The most common application of autocomplete is found in your web browser’s address bar; as you type the bar makes suggestions of URLs based on previous pages visited. In our example we will query a database of musical artists and give our users suggestions as they type in an artist name field.</p>
<h4>2. How it works.</h4>
<p>As a user types in a specific input field on a form, on each key up event, we use JavaScript to call a script in the background using Ajax (XMLHttpRequest) features built into the Scriptaculous library. Our form will pass the currently typed text to this script and the script will return a list of suggestions which will be dynamically placed in our page. The important thing is that our user can select one of these items by mouse-click or key-stroke to populate the form field instantly.</p>
<h4>3. Getting scriptaculous and building the form.</h4>
<p>This implementation of autocomplete requires 3 components:</p>
<ol>
<li>The Scriptculous libraries;</li>
<li>An HTML form with a single field;</li>
<li>An autocomplete server script</li>
</ol>
<p>I use PHP as my language of choice for the autocomplete server script, but I have tried to keep this tutorial abstract enough to allow you to use your language of choice. The server script is the only thing that changes for different server-side scripting languages. Download the latest version of the <a href="http://script.aculo.us/downloads">Scriptaculous JavaScript Libraries</a> and include them in the head of a new HTML document as below. Don’t forget to change the paths to match your directory structure:</p>
<p><code>&lt;script type="text/javascript" src="lib/prototype.js"&gt;&lt;/script&gt;<br />
&lt;script type="text/javascript" src="src/scriptaculous.js"&gt;&lt;/script&gt;</code></p>
<p>Now add a form to your HTML page’s body. In this form we are going to add a field that will take our search term (with both id and name attributes as “searchterm”) and an empty div with an id of “hint”. This empty div acts as a holder for our hints when they are generated. You should have some HTML looking like this:</p>
<p><code>&lt;input type="text" name="searchterm" id="searchterm" /&gt;<br />
&lt;div id="hint"&gt;&lt;/div&gt;</code></p>
<p>All we need now is to initiate a new call to Scriptaculous’ built in Ajax.Autocomplete method below our HTML code for the autocompleter &#8211; like this:</p>
<p><code>&lt;script type="text/javascript"&gt;<br />
new Ajax.Autocompleter(”searchterm”,”hint”,”server.php”);<br />
&lt;/script&gt;</code></p>
<p>The Ajax.Autocompleter takes 3 arguments: The element supplying the search string, the element to show suggestions in and the server script that will generate our suggestions. With that in place we can write the server script.</p>
<h4>4. Building the autocomplete server.</h4>
<p>The following server is written in PHP connecting to a MySQL database but you can your language of choice and even a flat text file or XML file for the suggestion database. My database table, “Artists”, is simple &#8211; it comprises a unique id field and an artist “name”.</p>
<p>As the Autocompleter sends the value of a form field as a POST variable, we can use this in our server script’s query againstthe database. All we need to do is output an unordered list for the autocompleter. Scriptaculous does the leg work of inserting the latest list into our hint box everytime the user makes a keystroke. Here is my server code in PHP</p>
<p><code>&lt;?<br />
// don't forget your database connection here.<br />
echo "&lt;ul&gt;";<br />
$sql = "SELECT * FROM artists WHERE name LIKE '%" . $_POST['searchterm'] . "%'";<br />
$rs = mysql_query($sql);<br />
while($data = mysql_fetch_assoc($rs)) {<br />
echo "&lt;li id=\"" . $data['id'] . "\"&gt;" . $data['name'] . "&lt;/li&gt;";<br />
}<br />
echo "&lt;/ul&gt;";<br />
?&gt;</code></p>
<p>Note that I have given each list item an id that is the same as that of the record. We will use this shortly for a more advanced technique. But for now we have all we need for a basic autocompleter.</p>
<h4>5. Passing back an id to our form</h4>
<p>Once a user has selected an item from the autocompleter, the string is entered into the textfield. Often, for the sake of efficiency and accuracy it is better to deal with the id of the item than the string name when it is passed on to the next page. To do this we can use some extra functionality of Ajax.Autocompleter along with some JavaScript that will pass the id to a hidden field in our form.</p>
<p>The first thing we need to add to our form is a hidden field with an id that we can reference:</p>
<p><code>&lt;input type="hidden" id="artist_id" name="artist_id" /&gt;</code></p>
<p>Now we can use an extra piece of functionality built into Ajax.Autocompleter that allows us to pick up the id that we included in our list item from the server script. This is an extra option on our autocompleter call:</p>
<p><code>new Ajax.Autocompleter(”searchterm”,”hint”,”server.php”, {afterUpdateElement : getSelectedId});</code></p>
<p>We are telling our autocompleter to run a function called getSelectedId when an item is selected. All we need to do now is write this function to get the id and inject it into our hidden field. Insert the function below the</p>
<p><code>function getSelectedId(text, li) {<br />
$('searchterm').value=li.id;<br />
}</code></p>
<p>And that’s all there is to it. Of course you can be as clever as you want with your autocomplete server script. Try using the LIMIT clause in your SQL to make it run faster. Make your queries more advanced to bring back more specific results. And don’t forget the fun you can have with CSS on your hint div and the list of suggestions that it displays. If you have any implementations of autocompletion using this tutorial, please post them in the comments here.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p><a href="http://www.ipsukltd.co.uk/green-roofs.php">Single ply membrane</a> Products</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wiseguysonly.com/2008/03/09/autocompletion-with-scriptaculous-and-ajax/feed/</wfw:commentRss>
		<slash:comments>71</slash:comments>
		</item>
		<item>
		<title>Alternating table row colours in PHP.</title>
		<link>http://www.wiseguysonly.com/2006/03/09/alternating-table-row-colours-in-php/</link>
		<comments>http://www.wiseguysonly.com/2006/03/09/alternating-table-row-colours-in-php/#comments</comments>
		<pubDate>Thu, 09 Mar 2006 06:20:21 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.wiseguysonly.com/?p=111</guid>
		<description><![CDATA[Recently I have been tinkering with Ruby on Rails and I came across a rather nice method of alternating row colours using only two lines of code and a couple of CSS rules. I have been using this method in my PHP ever since and never looked back. This is a neat trick to alternate [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have been tinkering with <a href="http://www.rubyonrails.com">Ruby on Rails</a> and I came across a rather nice method of alternating row colours using only two lines of code and a couple of CSS rules. I have been using this method in my PHP ever since and never looked back. This is a neat trick to alternate row colors on a table using PHP.</p>
<p><span id="more-111"></span></p>
<h3>Why would you want to do this?</h3>
<p>Alternating the row colors (sensibly) on tables makes them easier to read. Hard coding styles is easy enough, but when you are bringing rows back dynamically, from a database for example, you can never be sure which row will be where.</p>
<p>Lets say we have an array from the database called $data. Before we iterate over this information, we need to set a variable outside of our loop</p>
<p><code>$current_row = 0;</code></p>
<p>Now inside our loop, we need just one line that will alternate our <code>$current_row</code> count between 1 and 0</p>
<p><code>while( $data = mysql_fetch_array($recordset)) {<br />
$current_row = 1 - $current_row;<br />
... (rest of code goes here)<br />
}</code></p>
<p>What this does is sets the <code>$current_row</code> variable to 1 or 0 on each pass. The maths is that 1 &#8211; 0 = 1 and on the next pass 1 &#8211; 1 = 0 ¦ This logic can continue infinitely.</p>
<p>All we need to do now are two things:</p>
<ol>
<li>Set a dynamic class in the opening table row in our loop:</li>
<li>Create a class for our alternate row(s) in our CSS:<code>.row1 { background: #eed; }<br />
.row0 { background: #fff; }</code></li>
</ol>
<p>Two lines of extra php code, one extra class in your CSS, and you&#8217;re done.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wiseguysonly.com/2006/03/09/alternating-table-row-colours-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
