<?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>dom111.co.uk &#187; PHP</title>
	<atom:link href="http://www.dom111.co.uk/blog/category/coding/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.dom111.co.uk/blog</link>
	<description>Move along. Nothing to see here.</description>
	<lastBuildDate>Wed, 26 Oct 2011 16:37:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>jQuery serializeObject 0.1</title>
		<link>http://www.dom111.co.uk/blog/coding/jquery-serializeobject-0-1/283</link>
		<comments>http://www.dom111.co.uk/blog/coding/jquery-serializeobject-0-1/283#comments</comments>
		<pubDate>Thu, 31 Mar 2011 22:37:47 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[formToObject]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[post data]]></category>
		<category><![CDATA[serializeObject]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=283</guid>
		<description><![CDATA[I found myself in a situation recently where I wanted to have access to variables that would have been posted, in a the same structure as if the form had been posted and returned the JSON, using this jQuery plugin and Douglas Crockford&#8217;s JSON library, I think I&#8217;ve done it! You can download the script [...]]]></description>
			<content:encoded><![CDATA[<p>I found myself in a situation recently where I wanted to have access to variables that would have been posted, in a the same structure as if the form had been posted and returned the JSON, using this jQuery plugin and <a href="https://github.com/douglascrockford/JSON-js">Douglas Crockford&#8217;s JSON library</a>, I think I&#8217;ve done it!</p>
<p>You can <a href="/files/jquery-serializeObject/jquery.serializeObject-0.1.tar.gz">download the script here</a> and there&#8217;s <a href="/files/jquery-serializeObject">a demo here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/jquery-serializeobject-0-1/283/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>simpleGallery 0.2 &#8211; Minor updates</title>
		<link>http://www.dom111.co.uk/blog/coding/simplegallery-0-2-minor-updates/268</link>
		<comments>http://www.dom111.co.uk/blog/coding/simplegallery-0-2-minor-updates/268#comments</comments>
		<pubDate>Mon, 25 Oct 2010 13:39:49 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Photography]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[image gallery]]></category>
		<category><![CDATA[photography]]></category>
		<category><![CDATA[photography gallery]]></category>
		<category><![CDATA[php image gallery]]></category>
		<category><![CDATA[php images]]></category>
		<category><![CDATA[simplegallery]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=268</guid>
		<description><![CDATA[I&#8217;ve updated the gallery script I created in July. I&#8217;ve re-factored the code into a static class for easier modification and have created a config.ini settings file, to save modifying the script itself. New features: * Image titles * Separate configuration * Standard mode with safe URLs as well as mod_rewrite enabled &#8216;nice&#8217; URLs * [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve updated the gallery script I created in July. I&#8217;ve re-factored the code into a static class for easier modification and have created a config.ini settings file, to save modifying the script itself.</p>
<p>New features:</p>
<p>* Image titles<br />
* Separate configuration<br />
* Standard mode with safe URLs as well as mod_rewrite enabled &#8216;nice&#8217; URLs<br />
* Added &#8216;..&#8217; parent folder to galleries</p>
<p><a href="/gallery">The demo page has been updated</a> and <a href="/files/gallery/gallery-0.2.tar.gz">the script is available to download here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/simplegallery-0-2-minor-updates/268/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gallery 0.1</title>
		<link>http://www.dom111.co.uk/blog/coding/gallery-0-1/245</link>
		<comments>http://www.dom111.co.uk/blog/coding/gallery-0-1/245#comments</comments>
		<pubDate>Wed, 14 Jul 2010 19:28:08 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[gallery]]></category>
		<category><![CDATA[image gallery]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[simple gallery]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=245</guid>
		<description><![CDATA[I recently took it upon myself to create a very small gallery project to easily display images from a folder structure in a fairly pleasing way&#8230; This is the result! You can get the script here]]></description>
			<content:encoded><![CDATA[<p>I recently took it upon myself to create a very small gallery project to easily display images from a folder structure in a fairly pleasing way&#8230;</p>
<p><a href="/gallery">This is the result!</a></p>
<p><a href="/files/gallery/gallery-0.1.tar.gz">You can get the script here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/gallery-0-1/245/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XML Entities in PHP</title>
		<link>http://www.dom111.co.uk/blog/coding/xml-entities-in-php/224</link>
		<comments>http://www.dom111.co.uk/blog/coding/xml-entities-in-php/224#comments</comments>
		<pubDate>Mon, 15 Feb 2010 20:34:35 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[unicode]]></category>
		<category><![CDATA[utf8]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xmlentities]]></category>
		<category><![CDATA[xml_entitiy_decode]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=224</guid>
		<description><![CDATA[Because htmlentities() doesn&#8217;t even come close. This small file contains 4 functions (2 of which are taken from the PHP manual, credit given!) which will allow you to encode and decode entities from ASCII/unicode strings in either decimal or hexadecimal format for use in valid XML documents. The xml_entity_decode() function accepts an optional second parameter [...]]]></description>
			<content:encoded><![CDATA[<p>Because <code lang="php">htmlentities()</code> doesn&#8217;t even come close.</p>
<p>This small file contains 4 functions (2 of which are taken from the PHP manual, credit given!) which will allow you to encode and decode entities from ASCII/unicode strings in either decimal or hexadecimal format for use in valid XML documents.</p>
<p>The <code lang="php">xml_entity_decode()</code> function accepts an optional second parameter to allow non-standard XML entities (that may have been specified in your schema) in the format:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #666666; font-style: italic;">// 'entity' =&gt; 'char'</span>
  <span style="color: #0000ff;">'amp'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'lt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'gt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&gt;'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'apos'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'\''</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'quot'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&quot;'</span>
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>Example usage:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'funcs.xmlentities.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$s</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;strong&gt;This&lt;/strong&gt; should be safe, but don\'t assume!&lt;br/&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">'&lt;Field&gt;'</span><span style="color: #339933;">.</span>xmlentities<span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/Field&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// outputs: &lt;Field&gt;&amp;lt;strong&amp;gt;This&amp;lt;/strong&amp;gt; should be safe, but don&amp;apos;t assume!&amp;lt;br/&amp;gt;&lt;/Field&gt;</span></pre></div></div>

<p>You can <a href="/files/xmlentities/funcs.xmlentities.tar.gz">get the script here</a>, or there&#8217;s <a href="/files/xmlentities/">a demo here too</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/xml-entities-in-php/224/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP CSV Reader</title>
		<link>http://www.dom111.co.uk/blog/coding/php-csv-reader/201</link>
		<comments>http://www.dom111.co.uk/blog/coding/php-csv-reader/201#comments</comments>
		<pubDate>Wed, 07 Oct 2009 19:08:22 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[csv]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=201</guid>
		<description><![CDATA[I&#8217;ve never really built a definitive CSV reader and end up building a quick implementation of one any time I need one. But after working on the chunk script I decided that next time I build a CSV class I&#8217;d do it properly. This is the result: &#60;?php require&#40;'class.csv.php'&#41;; &#160; header&#40;'Content-type: text/plain'&#41;; &#160; $csv = [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve never really built a definitive CSV reader and end up building a quick implementation of one any time I need one.</p>
<p>But after working on the chunk script I decided that next time I build a CSV class I&#8217;d do it properly.</p>
<p><a href="/files/csv/csv.tar.gz">This is the result</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class.csv.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: text/plain'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$csv</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CSV<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.csv'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$csv</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p><a href="/files/csv/">There&#8217;s an information page here</a>, and <a href="/files/csv/csv.tar.gz">the script is here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/php-csv-reader/201/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePHP: Select Box Pagination</title>
		<link>http://www.dom111.co.uk/blog/coding/cakephp-select-box-pagination/182</link>
		<comments>http://www.dom111.co.uk/blog/coding/cakephp-select-box-pagination/182#comments</comments>
		<pubDate>Thu, 27 Aug 2009 12:40:54 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[drop-down]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[select box]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=182</guid>
		<description><![CDATA[Using CakePHP&#8217;s built in pagination system has saved me so much time in my current day job, but the latest designs I&#8217;m working on have drop-down box style pagination, as it&#8217;s possible to get many pages of results. To change the named parameters in the current URL I&#8217;ve created this small javascript function that will [...]]]></description>
			<content:encoded><![CDATA[<p>Using CakePHP&#8217;s built in pagination system has saved me so much time in my current day job, but the latest designs I&#8217;m working on have drop-down box style pagination, as it&#8217;s possible to get many pages of results.</p>
<p>To change the named parameters in the current URL I&#8217;ve created this small javascript function that will amend the URL:<span id="more-182"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * setURL
 *
 * Modifies the current URL and redirects the browser
 *
 * @param string key The name of the parameter to set
 * @param mixed value The value to set the parameter to
 * @return void
 * @author Dom Hastings
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> setURL<span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// set up the url separators</span>
  <span style="color: #003366; font-weight: bold;">var</span> separator <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// site.url/controller/action/key1:value1/key2:value2</span>
    <span style="color: #3366CC;">'key'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'value'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">':'</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// get the current url</span>
  <span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// check if the specified key already exists</span>
  <span style="color: #003366; font-weight: bold;">var</span> exists <span style="color: #339933;">=</span> url.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>separator.<span style="color: #660066;">key</span> <span style="color: #339933;">+</span> key <span style="color: #339933;">+</span> separator.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// if it does</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>exists <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// find the next separator.key</span>
    <span style="color: #003366; font-weight: bold;">var</span> last <span style="color: #339933;">=</span> url.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>separator.<span style="color: #660066;">key</span><span style="color: #339933;">,</span> exists <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// if there is one</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>last <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// replcae the existing value with the one passed</span>
      url <span style="color: #339933;">=</span> url.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> exists<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> separator.<span style="color: #660066;">key</span> <span style="color: #339933;">+</span> key <span style="color: #339933;">+</span> separator.<span style="color: #660066;">value</span> <span style="color: #339933;">+</span> escape<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> url.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span>last<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// if not</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// just append it</span>
      url <span style="color: #339933;">=</span> url.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> exists<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> separator.<span style="color: #660066;">key</span> <span style="color: #339933;">+</span> key <span style="color: #339933;">+</span> separator.<span style="color: #660066;">value</span> <span style="color: #339933;">+</span> escape<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// if it's not already in there</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// if the URL doesn't end with a separator.key</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>url.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> separator.<span style="color: #660066;">key</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// append it</span>
      url <span style="color: #339933;">+=</span> separator.<span style="color: #660066;">key</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// append the value</span>
    url <span style="color: #339933;">+=</span> key <span style="color: #339933;">+</span> separator.<span style="color: #660066;">value</span> <span style="color: #339933;">+</span> escape<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// set the url</span>
  window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span> <span style="color: #339933;">=</span> url<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Coupled with the following code to my view:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!-- Items per page: --&gt;
&lt;select onchange=&quot;setURL('limit', this.value)&quot;&gt;
  &lt;option value=&quot;10&quot;&lt;?=($params['named']['limit'] == '10') ? ' selected=&quot;selected&quot;' : ''?&gt;&gt;10&lt;/option&gt;
  &lt;option value=&quot;20&quot;&lt;?=($params['named']['limit'] == '20') ? ' selected=&quot;selected&quot;' : ''?&gt;&gt;20&lt;/option&gt;
  &lt;option value=&quot;50&quot;&lt;?=($params['named']['limit'] == '50') ? ' selected=&quot;selected&quot;' : ''?&gt;&gt;50&lt;/option&gt;
  &lt;option value=&quot;100&quot;&lt;?=($params['named']['limit'] == '100') ? ' selected=&quot;selected&quot;' : ''?&gt;&gt;100&lt;/option&gt;
&lt;/select&gt;
&nbsp;
&lt;!-- Page: --&gt;
&lt;select onchange=&quot;setURL('page', this.value)&quot;&gt;
&lt;?php for ($i = 1; $i &lt;= $paginator-&gt;counter(array('format' =&gt; '%pages%')); $i++): ?&gt;
  &lt;option value=&quot;&lt;?=$i?&gt;&quot;&lt;?=($params['named']['page'] == $i) ? ' selected=&quot;selected&quot;' : ''?&gt;&gt;&lt;?=$i?&gt;&lt;/option&gt;
&lt;?php endfor ?&gt;
&lt;/select&gt;</pre></div></div>

<p>Hopefully it&#8217;ll help anyone else deal with a similar problem!</p>
<p><strong>Note</strong>: If there&#8217;s a simpler, more accessible way to do it, I&#8217;d really like to know!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/cakephp-select-box-pagination/182/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CakePHP: Components, redirect fail (On my part&#8230;)</title>
		<link>http://www.dom111.co.uk/blog/coding/cakephp-components-redirect-fail-on-my-part/171</link>
		<comments>http://www.dom111.co.uk/blog/coding/cakephp-components-redirect-fail-on-my-part/171#comments</comments>
		<pubDate>Wed, 26 Aug 2009 19:58:46 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[component]]></category>
		<category><![CDATA[epic fail]]></category>
		<category><![CDATA[lessons learned]]></category>
		<category><![CDATA[redirect]]></category>
		<category><![CDATA[redirect loses id]]></category>
		<category><![CDATA[redirect losing id]]></category>
		<category><![CDATA[redirect strips id]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=171</guid>
		<description><![CDATA[I&#8217;ve been working on a CakePHP project lately and created a small component which was only needed in one of my controllers: class CounterComponent extends Component &#123; var $components = array&#40; 'Session' &#41;; &#160; function i&#40;&#41; &#123; if &#40;$this-&#62;Session-&#62;check&#40;'Counter.i'&#41;&#41; &#123; $i = &#40;$this-&#62;Session-&#62;read&#40;'Counter.i'&#41; + 1&#41;; &#160; &#125; else &#123; $i = 0; &#125; &#160; $this-&#62;Session-&#62;write&#40;'Counter.i', [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a <a href="http://www.cakephp.org/">CakePHP</a> project lately and created a small component which was only needed in one of my controllers:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> CounterComponent <span style="color: #000000; font-weight: bold;">extends</span> Component <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$components</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'Session'</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> i<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Counter.i'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Counter.i'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Counter.i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> clear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Counter.i'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-171"></span>It simply kept track of a number over the life of the current session and incremented it each time it was accessed. I did&#8217;t think it would interfere in my other controllers, and it didn&#8217;t until I tried to perform a simple redirect later on in the app:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">function</span> edit<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// process it</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Customer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Customer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'customers'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$id</span>
      <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Customer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'first'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$id</span>
      <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">get_defined_vars</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'form'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>That code seemed simple enough, but each and every time it was run, I&#8217;d be sent to <code>/customers/edit</code>, instead of <code>/customers/view/1</code>.</p>
<p>WTF? Cake is losing my <code>id</code>s?</p>
<p>So I tried changing the redirect to a string:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/customers/view/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>No difference, still redirects to <code>/customers/edit</code>. Ok fine. Lets try a full URL:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.google.co.uk/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Serisouly. Still? All this happened over a series of a couple of days, each session of <del>coding</del><ins>banging my head against a brick wall</ins> leaving me more frustrated. I got mad I created a <code>TestsController</code> with an action <code>test</code> which exhibited the same behaviour&#8230; (But only after I&#8217;d moved everything into the <code>AppController</code>, whilst franticly trying everything&#8230; *sigh*)</p>
<p>After many visits to the <a href="http://book.cakephp.org/">CakePHP documentation</a> I decided to re-investigate the <a href="http://api.cakephp.org/view_source/controller/#line-536">source code</a> and see if I had missed anything. The <code lang="php">Controller::redirect()</code> function contains this code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">		<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Component</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">beforeRedirect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$status</span><span style="color: #339933;">,</span> <span style="color: #000088;">$exit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<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;">$response</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$resp</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<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;">$resp</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resp</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resp</span><span style="color: #339933;">,</span> EXTR_OVERWRITE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$resp</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>
					<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$resp</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&#8220;But that&#8217;s fine!&#8221;, I thought, &#8220;I don&#8217;t even have a <code>beforeRedirect()</code> function in my component, and surely the class I&#8217;m extending will just return <code>null</code>.&#8221; Oh dear. Assumption. <a href="http://api.cakephp.org/view_source/component/#line-139">There it is</a>, done now.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Component::beforeRedirect</span>
	<span style="color: #000000; font-weight: bold;">function</span> beforeRedirect<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$controller</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$status</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$exit</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$response</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>
&nbsp;
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_primary <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$component</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_loaded<span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$component</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">enabled</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$component</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'beforeRedirect'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$resp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$component</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">beforeRedirect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$controller</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$status</span><span style="color: #339933;">,</span> <span style="color: #000088;">$exit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$resp</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000088;">$response</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$resp</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$response</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Crap. It returns an empty array, which the <code>redirect()</code> function will use because it isn&#8217;t <code>null</code>, which will redirect to the current controller/action but strip out the <code>id</code>.</p>
<p><strong>Lesson</strong>: Always, <strong><em>ALWAYS</em></strong>, create a base component to extend, or, alternatively, just <a href="http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components">read the fricking manual properly</a>.</p>
<p><strong>TL;DR</strong>: I failed at creating a CakePHP component. Fix: <code>class MyComponent extends Object {}</code> instead of <code>class MyComponent extends Component {}</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/cakephp-components-redirect-fail-on-my-part/171/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP: Object Oriented Image Manipulation</title>
		<link>http://www.dom111.co.uk/blog/coding/php-object-oriented-image-manipulation/156</link>
		<comments>http://www.dom111.co.uk/blog/coding/php-object-oriented-image-manipulation/156#comments</comments>
		<pubDate>Wed, 19 Aug 2009 16:26:08 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[image manipulation]]></category>
		<category><![CDATA[php5]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=156</guid>
		<description><![CDATA[I&#8217;ve been working on a CMS lately and having to create thumbnails for uploaded images is always a pain, lots of maths working out the correct sizes and such, so I&#8217;ve created a fairly small script to manipulate images in an object-oriented style. For example: scale&#40;400, 300&#41;; $image-&#38;gt;write&#40;'small-image.jpg'&#41;; &#160; $image2 = new Image&#40;'image2.jpg'&#41;; $image2-&#38;gt;watermark = [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a CMS lately and having to create thumbnails for uploaded images is always a pain, lots of maths working out the correct sizes and such, so I&#8217;ve created a fairly small script to manipulate images in an object-oriented style.</p>
<p>For example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">scale<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">400</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>write<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'small-image.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$image2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'image2.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image2</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>watermark <span style="color: #339933;">=</span> <span style="color: #0000ff;">'sample.png'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$image2</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>output<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I&#8217;ve only implemented a few of the GD library methods, but I think these are the most useful methods. I might have to work on something that rounds the corners too, but I don&#8217;t have any need for it just yet.</p>
<p>There&#8217;s a <a href="/files/php-ooim/">demo page as ever</a>, and you can <a href="/files/php-ooim/php-ooim-0.1.tar.gz">get the script here</a> (It&#8217;s a bit big, because the sample image is included, <a href="/files/php-ooim/php-ooim-no-sample-image-0.1.tar.gz">here&#8217;s one without it</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/php-object-oriented-image-manipulation/156/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>String Variable Concatenation</title>
		<link>http://www.dom111.co.uk/blog/coding/string-variable-concatenation/149</link>
		<comments>http://www.dom111.co.uk/blog/coding/string-variable-concatenation/149#comments</comments>
		<pubDate>Fri, 14 Aug 2009 13:27:09 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[string concatenation]]></category>
		<category><![CDATA[variable concatenation]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=149</guid>
		<description><![CDATA[I stumbled across something odd today in PHP: $r = ''; $r .= $r .= $r .= 'a'; Now, personally, I&#8217;d have expected a syntax error from the above code, but the result was even more confusing at first&#8230; print $r; // 'aaaa' Not sure if this was the expected output or not I tested [...]]]></description>
			<content:encoded><![CDATA[<p>I stumbled across something odd today in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$r</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$r</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, personally, I&#8217;d have expected a syntax error from the above code, but the result was even more confusing at first&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">print</span> <span style="color: #000088;">$r</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 'aaaa'</span></pre></div></div>

<p>Not sure if this was the expected output or not I tested similar code in other languages:</p>
<p>Ruby:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">r = <span style="color:#996600;">''</span>
r <span style="color:#006600; font-weight:bold;">+</span>= r <span style="color:#006600; font-weight:bold;">+</span>= r <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#996600;">'a'</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> r <span style="color:#008000; font-style:italic;"># 'a'</span></pre></div></div>

<p>Python:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">r = <span style="color: #483d8b;">''</span>
r += r += r += <span style="color: #483d8b;">'a'</span>
<span style="color: #808080; font-style: italic;">#   File &quot;&quot;, line 1</span>
<span style="color: #808080; font-style: italic;">#     r += r += r += 'a'</span>
<span style="color: #808080; font-style: italic;">#             ^</span>
<span style="color: #808080; font-style: italic;"># SyntaxError: invalid syntax</span></pre></div></div>

<p>Javascript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
r <span style="color: #339933;">+=</span> r <span style="color: #339933;">+=</span> r <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'a'</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>r<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 'a'</span></pre></div></div>

<p>Perl:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$r</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$r</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">$r</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">$r</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">'a'</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$r</span><span style="color: #339933;">;</span> <span style="color: #339933;">//</span> <span style="color: #ff0000;">'aaaa'</span></pre></div></div>

<p>That explains it!</p>
<p>So the reason the string is &#8216;aaaa&#8217; seems to be that the code is evaluated from right to left:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$r</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$r</span> <span style="color: #339933;">+=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// How it works:</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">// $r += 'a'; // 'a'</span>
<span style="color: #666666; font-style: italic;">// $r += $r += 'a'; // 'a' + 'a';</span>
<span style="color: #666666; font-style: italic;">// $r += $r += $r += 'a'; // 'aa' + ('a' + 'a')</span></pre></div></div>

<p>I don&#8217;t think it&#8217;s a bug, well, at least I assume not, but is there a name for this?</p>
<p><strong>Update</strong>: I <a href="http://www.reddit.com/r/PHP/comments/9amid/string_concatenation/">asked some clever people for help understanding it</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/string-variable-concatenation/149/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSS 0.2 &#8211; Nested CSS update</title>
		<link>http://www.dom111.co.uk/blog/coding/jss-0-2-nested-css-update/118</link>
		<comments>http://www.dom111.co.uk/blog/coding/jss-0-2-nested-css-update/118#comments</comments>
		<pubDate>Tue, 28 Jul 2009 22:33:08 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[jss]]></category>
		<category><![CDATA[nested css]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=118</guid>
		<description><![CDATA[So I thought I&#8217;d update the JSS script I wrote previously with a few changes I&#8217;ve been thinking about for a while. The primary reason for the update is to add a new &#8216;non-CSS&#8217; property of &#8216;include&#8217;. This property allows you to include styles that have already been defined in the current class. For example, [...]]]></description>
			<content:encoded><![CDATA[<p>So I thought I&#8217;d update the JSS script I wrote previously with a few changes I&#8217;ve been thinking about for a while.</p>
<p>The primary reason for the update is to add a new &#8216;non-CSS&#8217; property of &#8216;include&#8217;. This property allows you to include styles that have already been defined in the current class.<span id="more-118"></span></p>
<p>For example, if you had a stylesheet that looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">.<span style="color: #993333;">bold</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.important</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>You could rewrite this using:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">JSS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;.bold&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;font-weight&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;bold&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;.important&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;include&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;.bold&quot;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Which I know you shouldn&#8217;t really do, and yes, it probably does go against the whole point of CSS, but I&#8217;m sure it&#8217;ll be useful in some situation at some point&#8230; No? Well it was interesting making it anyway.</p>
<p>Other minor updates are a PHP version so you can write nested stylesheets and output minified CSS all server-side and a bunch of HTML colour names that are actually shorter than the 6 digit hex codes.</p>
<p>I might work on doing other languages if I fancy the practice&#8230; Probably make a CSS to JSS conversion script first though&#8230;</p>
<p>You can see the <a href="/files/jss/0.2/"> updated demo page here</a>, or just <a href="/files/jss/jss-0.2.tar.gz">download the files here and have a look for yourself</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/jss-0-2-nested-css-update/118/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

