<?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>thisMayhem</title>
	<atom:link href="http://www.thismayhem.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thismayhem.com</link>
	<description>gathering Knowledge</description>
	<lastBuildDate>Wed, 08 Feb 2012 16:04:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Android &#8211; Log personalizado para desarrolladores</title>
		<link>http://www.thismayhem.com/android/android-log-personalizado-para-desarrolladores/</link>
		<comments>http://www.thismayhem.com/android/android-log-personalizado-para-desarrolladores/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 16:01:59 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Principiantes]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[errores]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[principiantes]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=896</guid>
		<description><![CDATA[A medida que he ido creando la aplicación de pepeSaldo (si, si, solo hablo de ella porque es la única que tengo en Android) una cosa que me molestaba bastante...]]></description>
			<content:encoded><![CDATA[<p>A medida que he ido creando la aplicación de pepeSaldo (si, si, solo hablo de ella porque es la única que tengo en Android) una cosa que me molestaba bastante era el tema de los logs. Me explico, puedo tener algunos mensajes en la consola para comprobar si algo funciona o se ha roto.</p>
<p>Cuando he comprobado que funcionan o no, o simplemente cuando ya no necesito que esos datos se muestren en la consola, tengo que ir a la clase y o bien comentar las líneas o eliminarlas. Como siempre os digo, es probable que exista una mejor solución que la que propongo, pero si declaramos una nueva clase como esta:</p>
<div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Arrays</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.SharedPreferences</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.util.Log</span><span style="color: #339933;">;</span><br />
<br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Say <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; SharedPreferences prefs<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">Boolean</span> LogEnabled &nbsp;&nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//main switch</span><br />
&nbsp; &nbsp; <span style="color: #003399;">String</span> list<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;generic&quot;</span>,<span style="color: #0000ff;">&quot;alerts&quot;</span>,<span style="color: #0000ff;">&quot;widget&quot;</span>,<span style="color: #0000ff;">&quot;query&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> valid <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Arrays</span>.<span style="color: #006633;">asList</span><span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> it<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> msg,<span style="color: #003399;">String</span> title<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; msg <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;[&quot;</span><span style="color: #339933;">+</span>msg<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>LogEnabled<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>title.<span style="color: #006633;">compareTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> title <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Generic&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>msg.<span style="color: #006633;">compareTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>&nbsp; &nbsp;msg &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ouput&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Build output string</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>valid.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>title<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;[%s]&quot;</span>,title<span style="color: #009900;">&#41;</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;[%s]&quot;</span>, msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Como podeis ver, la clase es una tontería y en realidad no hace nada más que comprobar si tenemos listado la &#8220;categoría&#8221; del mensaje para que salga en los logs o no. Es decir, instancias la clase en cada &#8220;onCreate&#8221; de las clases en las que queramos usarlo (asegurandonos de que es global, para poder acceder a ella en toda la clase).</p>
<div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Say say <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Say<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Habiendo importado la class</span></div></td></tr></tbody></table></div>
<p>Ahora por ejemplo, imaginemos que estoy depurando las consultas a la base de datos de la aplicación y no estoy seguro de qué consulta se genera, o de si se genera correctamente. Imaginemos también, que dicha consulta está guardada en una variable llamada <code class="codecolorer java dawn"><span class="java">consulta</span></code>. Puesto que ya tenemos la instancia de la clase &#8220;<em>Say</em>&#8220;, podemos hacer lo siguiente:</p>
<div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">say.<span style="color: #006633;">it</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;query&quot;</span>,<span style="color: #0000ff;">&quot;La consulta número uno es:&quot;</span> <span style="color: #339933;">+</span> consulta<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Esta llamada al método <code class="codecolorer java dawn"><span class="java">it<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> titulo,<span style="color: #003399;">String</span> mensaje<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> de la instancia, comprueba primero que tenemos activados los logs (el valor de &#8220;<em>LogEnabled</em>&#8221; dentro de la clase Say. Si eso se cumple, comprueba que la &#8220;categoría&#8221; que hemos utilizado, en este caso &#8220;query&#8221;, está disponible en la lista. Como podéis ver, lo está y nos mostraría en el log el valor de la variable tal y como es de esperar.</p>
<p>Cuando terminemos de depurar esto, podemos eliminar <code class="codecolorer java dawn"><span class="java">query</span></code> de la lista y no veremos los mensajes, pero tendremos la oportunidad en cualquier momento de volver a añadirla y disponer de dicha información, en un momento. Sin añadir ni eliminar líneas en las clases afectadas, solo una entrada más o menos en la lista de nuestra pequeña clase.</p>
<p>Si hacemos una llamada sin título, mostrará &#8220;<em>generic</em>&#8221; como habréis podido adivinar.Creo que eso es todo, espero que os sirva, no sabéis lo que hubiera dado por disponer de esto al principio.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/android/android-log-personalizado-para-desarrolladores/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preferencias de Usuario con nombre variable &#8211; ObjectiveC</title>
		<link>http://www.thismayhem.com/iphone/preferencias-de-usuario-con-nombre-variable-objectivec/</link>
		<comments>http://www.thismayhem.com/iphone/preferencias-de-usuario-con-nombre-variable-objectivec/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 00:50:24 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective c]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[NSUserDefaults]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[preferencias]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=876</guid>
		<description><![CDATA[¿Pero qué dices? El título probablemente no ayude nada para que os hagáis una idea de lo que os voy a contar, pero como siempre, os hago un pequeño resumen...]]></description>
			<content:encoded><![CDATA[<p><strong>¿Pero qué dices?</strong><br />
El título probablemente no ayude nada para que os hagáis una idea de lo que os voy a contar, pero como siempre, os hago un pequeño resumen aquí. Resulta que estoy guardando una serie de objetos en mis preferencias de usuario (eso ya deberíais hacerlo) y el nombre (key) de las mismas sigue un patrón tal que este <code class="codecolorer java dawn"><span class="java">nombreComun<span style="color: #339933;">-</span>nombreVariable</span></code> como por ejemplo <strong>edad-juan</strong>. Ni que decir tiene que si vamos a almacenar un numero considerable de elementos de este tipo es posible que nos convenga usar una base de datos.</p>
<p><strong>El problema:</strong><br />
Para conseguir esos datos, tenía que hacer algo como lo siguiente:</p>
<div class="codecolorer-container objc dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #400080;">NSUserDefaults</span> <span style="color: #002200;">*</span>preferencias <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>nombreVariable <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Juan&quot;</span>; <span style="color: #11740a; font-style: italic;">//Solo para el ejemplo, pensad que la siguiente vez, pueda ser otro.</span><br />
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>valor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>preferencias objectForKey<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;edad-%@&quot;</span>,nombreVariable<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<p>Como podeis ver, la parte que no me gusta de todo eso, es tener que unir dos cadenas de esa manera, con la perdida de claridad que conlleva en mi código y por otro lado, sabiendo que el valor de &#8220;nombreVariable&#8221; es dinámico, la llamada a esta u otra preferencia del usuario Juan sería siempre similar por lo que podemos automatizar el proceso.</p>
<p>Para ello extendemos la clase NSUserDefaults de Objective C, creando una clase (declaración e implementación) con el siguiente nombre NSUserDefaults+NombreVariable (claramente la parte que sigue al + la podéis elegir).</p>
<p>Os pongo como me queda a mi:</p>
<div class="codecolorer-container objc dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">// &nbsp;NSUserDefaults+defNumber.h</span><br />
<br />
<span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span><br />
<br />
<span style="color: #a61390;">@interface</span> <span style="color: #400080;">NSUserDefaults</span> <span style="color: #002200;">&#40;</span>defNumber<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>UsrObjForKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>key;<br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p>Como podeis ver, el nombre de la clase no corresponde al del archivo, y esto es así por un motivo. Estamos generando una categoría a partir de NSUserDefaults y, siempre que importemos esta clase, tendremos disponibles los métodos que declaremos, en cualquier instancia de NSUserDefaults. Pues bien, la implementación de la clase que os acabo de pegar, solo tiene un método nuevo:</p>
<div class="codecolorer-container objc dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">// &nbsp;NSUserDefaults+defNumber.m</span><br />
<br />
<span style="color: #6e371a;">#import &quot;NSUserDefaults+defNumber.h&quot;</span><br />
<br />
<span style="color: #a61390;">@implementation</span> <span style="color: #400080;">NSUserDefaults</span> <span style="color: #002200;">&#40;</span>defNumber<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>UsrObjForKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>key<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self objectForKey<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@-%@&quot;</span>,key,<span style="color: #002200;">&#91;</span>self objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;nombreVariable&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p>Si os fijais bien, el método nuevo <code class="codecolorer objc dawn"><span class="objc"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>UstObjForKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>key;</span></code> funciona exactamente igual que el ejemplo primero en el que el nombre variable &#8220;Juan&#8221; lo obteníamos de una variable solo que en este caso, lo tenemos guardado con una preferencia y de este modo, podemos conseguir el valor de la preferencia &#8220;edad-x&#8221; donde x es una variable cuyo valor establecemos en algún momento y nos encargamos de mantener/modificar así:</p>
<div class="codecolorer-container objc dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">//Habiendo importado la clase que extiende a NSUserDefaults </span><br />
<span style="color: #400080;">NSUserDefaults</span> <span style="color: #002200;">*</span>preferencias <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>valor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>preferencias UsrObjForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;edad&quot;</span><span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<p>Espero que veais el potencial que tiene este tipo de modificaciones para facilitaros la vida, ya que esa &#8220;tontería&#8221; y otras del estilo me han limpiado bastante el código para la versión de iPhone de pepeSaldo en la que estoy trabajando actualmente.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/iphone/preferencias-de-usuario-con-nombre-variable-objectivec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android para principiantes 2 &#8211; Tareas asíncronas parte 1</title>
		<link>http://www.thismayhem.com/android/android-para-principiantes-2-tareas-asincronas-parte-1/</link>
		<comments>http://www.thismayhem.com/android/android-para-principiantes-2-tareas-asincronas-parte-1/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 19:31:19 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Principiantes]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[asíncrona]]></category>
		<category><![CDATA[principiantes]]></category>
		<category><![CDATA[ui]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=856</guid>
		<description><![CDATA[¿Qué es una tarea asíncrona? Si nunca os habeis enfrentado a una igual os pareace algo complicado pero en definitiva, una tarea asíncrona es cualquier acción que corre en un...]]></description>
			<content:encoded><![CDATA[<p><strong>¿Qué es una tarea asíncrona?</strong></p>
<p>Si nunca os habeis enfrentado a una igual os pareace algo complicado pero en definitiva, una tarea asíncrona es cualquier acción que corre en un <em>plano</em> o <em>hilo</em> diferente al principal, es decir, sin interrumpir la ejecución del programa a ojos del usuario. Para que tengáis un ejemplo más claro de tarea asíncrona, pensad en lo que pasa cuando descargamos un archivo de internet: Pulsamos en un enlace y comienza una descarga, en segundo plano. Si esto no fuera una tarea asíncrona, no podríamos hacer NADA hasta que dicha tarea concluyera.</p>
<p>&nbsp;</p>
<p><strong>¿Cómo es una tarea asíncrona en Android?</strong></p>
<p>Personalmente, llegué bastante tarde al tema de las tareas asíncronas o más bien, tardé en darme cuenta de que eran necesarias. La estructura básica de una tarea asíncrona en la siguiente:</p>
<div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">Class</span> MiTarea <span style="color: #000000; font-weight: bold;">extends</span> AsynkTask<span style="color: #339933;">&lt;</span><span style="color: #003399;">Integer</span>,<span style="color: #003399;">Integer</span>,Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; @Override <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">Integer</span> doInBackground<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>... <span style="color: #006633;">params</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Algo que queremos ejecutar en otro hilo</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Como podeis ver, estamos sobreescribiendo el método <code class="codecolorer java dawn"><span class="java">doInBackground</span></code> de AsynkTask y especificando qué queremos hacer en este nuevo hilo. No voy a entrar en detalles sobre qué hacer en este método, solo recalcar que lo que hagamos aquí no debe modificar la interfaz de usuario directamente, nunca.</p>
<p>Aparte del método que os acabo de presentar disponemos de <code class="codecolorer java dawn"><span class="java">onPreExecute</span></code> y <code class="codecolorer java dawn"><span class="java">onPostExecute</span></code> que como sus nombres indican se ejecutarán antes y después del <code class="codecolorer java dawn"><span class="java">doInBackground</span></code></p>
<p>Así, podemos crear una tarea asíncrona desde nuestro código.</p>
<div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">MiTarea ejemplo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MiTarea<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ejemplo.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Esto, ejecutará lo que sea que tengamos definido en el &#8220;doInBackground&#8221; de la clase en otro hilo y a no ser que lo especifiquemos, no notificará al hilo principal de su progreso. El método que yo utilizo y que como siempre, es probable que no sea el más indicado, es pasarle la actividad a la instancia de la clase, y dependiendo de qué clase sea, llamar a una u otra función, al terminar. Me explico:</p>
<div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">Class</span> MiTarea <span style="color: #000000; font-weight: bold;">extends</span> AsynkTask<span style="color: #339933;">&lt;</span><span style="color: #003399;">Integer</span>,<span style="color: #003399;">Integer</span>,Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Activity myApp<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> MiTarea setActivity<span style="color: #009900;">&#40;</span>Activity myApp<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myApp</span> <span style="color: #339933;">=</span> myApp<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">Integer</span> doInBackground<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>... <span style="color: #006633;">params</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Algo que queremos ejecutar en otro hilo</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Con el nuevo método &#8220;setActivity&#8221; que acabamos de declarar, podemos indicarle a la instancia de &#8220;MiTarea&#8221; qué actividad la está llamando, y sabiendo eso, llamar a métodos en el hilo principal según no sea conveniente. Para que me entendáis, si quiero hacer que un elemento de la interfaz desaparezca, no lo podemos hacer desde la tarea asíncrona, pero sí, si llamamos a un método que realice la acción por nosotros, en el hilo principal. Aclarar que el tipo de datos que devuelve es &#8220;MiTarea&#8221;, para poder encadenar llamadas a métodos, ahora lo veréis:</p>
<div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">MiActividad app <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span><br />
MiTarea ejemplo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MiTarea<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ejemplo.<span style="color: #006633;">setActivity</span><span style="color: #009900;">&#40;</span>app<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>De este modo, podemos definir qué actividad somos, y ejecutar la tarea en la misma línea. Hay gente a la que le gusta este tipo de sintaxis, y otra a la que no. Yo como podéis ver, soy de los que si. Ahora que sabemos, qué actividad crea la tarea, debemos hacer algo cuando dicha tarea termine. Para eso, disponemos del método <code class="codecolorer java dawn"><span class="java">onPostExecute<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> resultado<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> del que os hablaba antes.</p>
<div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">Class</span> MiTarea <span style="color: #000000; font-weight: bold;">extends</span> AsynkTask<span style="color: #339933;">&lt;</span><span style="color: #003399;">Integer</span>,<span style="color: #003399;">Integer</span>,Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Activity myApp<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> MiTarea setActivity<span style="color: #009900;">&#40;</span>Activity myApp<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myApp</span> <span style="color: #339933;">=</span> myApp<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">Integer</span> doInBackground<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>... <span style="color: #006633;">params</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Algo que queremos ejecutar en otro hilo</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onPostExecute<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> resultado<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Comprobamos que la clase de la actividad que nos ha llamado</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//es la que nos interesa, y si es el caso llamamos al método</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>myApp.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">compareTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;class MiActividad&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>MiActividad<span style="color: #009900;">&#41;</span>myApp<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">metodoEnPrincipal</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Lo que hacemos en el <code class="codecolorer java dawn"><span class="java">onPostExecute<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> resultado<span style="color: #009900;">&#41;</span></span></code> es primero comprobar que la clase corresponde a &#8220;MiActividad&#8221;, y si es el caso, llamamos al método <code class="codecolorer java dawn"><span class="java">metodoEnPrincipal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> de la actividad. Para ello deberíamos declarar ese método, claro está, pero deberíais entender el funcionamiento hasta ahora. Lo siento si esperáis código que podáis copiar y pegar, creo que es mejor entenderlo y ser capaces de escribirlo vosotros mismos.</p>
<p>Una vez explicado el método de <code class="codecolorer java dawn"><span class="java">onPostExecute</span></code>, no puede faltar explicar sus métodos hermanos, <code class="codecolorer java dawn"><span class="java">onPreExecute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> y <code class="codecolorer java dawn"><span class="java">onCancelled<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code>, no voy a poner ejemplo de código puesto que con el anterior debería bastar. Solo recalcar que el primer se ejecuta ANTES del <code class="codecolorer java dawn"><span class="java">doInBackground</span></code> y que para que lleguemos al <code class="codecolorer java dawn"><span class="java">onCancelled</span></code> tenemos que haber cancelado la ejecución (¿obvio no?)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/android/android-para-principiantes-2-tareas-asincronas-parte-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Lista de archivos en una carpeta</title>
		<link>http://www.thismayhem.com/php/php-lista-de-archivos-en-una-carpeta/</link>
		<comments>http://www.thismayhem.com/php/php-lista-de-archivos-en-una-carpeta/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 15:48:24 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Function Finds]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Tip of the day]]></category>
		<category><![CDATA[archivos]]></category>
		<category><![CDATA[carpetas]]></category>
		<category><![CDATA[handler]]></category>
		<category><![CDATA[opendir]]></category>
		<category><![CDATA[readdir]]></category>
		<category><![CDATA[rutas]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=847</guid>
		<description><![CDATA[¡Nunca se deja de aprender! Ayer me estaba preparando un mini panel de control para administrar una serie de archivos que tengo y tener la opción de borrar los que...]]></description>
			<content:encoded><![CDATA[<p>¡Nunca se deja de aprender! Ayer me estaba preparando un mini panel de control para administrar una serie de archivos que tengo y tener la opción de borrar los que estuvieran corruptos o simplemente los que no me interesan. Este artículo no es sobre cómo borrarlos (todos sabemos que es <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ruta/archivo.extension&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code>, sino sobre cómo listar los archivos de la carpeta.</p>
<p>Hasta ahora, siempre había utilizado el mismo método, con un handler para la carpeta, excluyendo las entradas &#8220;.&#8221; y &#8220;..&#8221; y especificando la extensión que quiero. El código sería algo así:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">$archivos</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 />
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">opendir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/ruta/'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">readdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;.&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;..&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">&quot;txt&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$archivos</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/ruta/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #990000;">closedir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>No he comrpobado si funciona, porque se escapa del objetivo del artículo. Quiero mostraros la forma que descubrí el otro día echando un vistazo por stackoverflow. Enlazaría con la respuesta pero no consigo encontrarla. Esta solución es mucho más elegante como podéis observar:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">$archivos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ruta/*.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Como veis, es solamente la función http://php.net/manual/es/function.glob.php ,que encuentra todos los archivos en la ruta especificada que cumplan con el filtro que le digamos. Estoy seguro de que le veis el potencial a esa línea de código para mejorar el vuestro (si es que no la usabais ya).</p>
<p>Aclarar, que ambas soluciones nos darían un array llamado <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$archivos</span></span></code> que contiene las rutas y nombres de archivo coincidentes. Para acceder a esos archivos, podemos usar otras funciones, como <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$archivo</span><span style="color: #009900;">&#41;</span></span></code> para sus contenidos, <code class="codecolorer text dawn"><span class="text">filesize($archivo)</span></code> para el tamaño, o <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">filemtime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$archivo</span><span style="color: #009900;">&#41;</span></span></code> para la fecha de modificación de los archivos.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/php-lista-de-archivos-en-una-carpeta/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android &#8211; Consejos #1</title>
		<link>http://www.thismayhem.com/android/android-consejos-1/</link>
		<comments>http://www.thismayhem.com/android/android-consejos-1/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 04:32:39 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Principiantes]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=844</guid>
		<description><![CDATA[El otro día tuve que re-escribir una clase de pepeSaldo que me estaba dando problemas. No porque no funcionara sino porque, a pesar de hacerlo, resultaba complicado reutilizarla para realizar...]]></description>
			<content:encoded><![CDATA[<p>El otro día tuve que re-escribir una clase de pepeSaldo que me estaba dando problemas. No porque no funcionara sino porque, a pesar de hacerlo, resultaba complicado reutilizarla para realizar una tarea muy similar con pequeñas modificaciones.</p>
<p>En un principio, los parametros configurables de la clase los establecía a partir del constructor de la misma, es decir, en el momento en el que creaba la instancia de dicha clase. Si hacemos esto, podemos tener algo como esto:</p>
<div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">miClase miInstancia <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> miClase<span style="color: #009900;">&#40;</span>parametro1,parametro2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Esto <strong>puede</strong> tener sentido siempre que los parámetros que le pasemos sean comunes a todas las acciones que queremos que abarque nuestra clase. En mi caso particular, tenía una clase encargada de actualizar los datos de llamadas y datos de una cuenta de pepephone. La llamada a la versión más actual de los datos, las llamadas, o simplemente la extracción de los números de una línea son acciones que quiero que las ejecute una clase en particular, ya que comparten muchos elementos.</p>
<p>Si nos abstraemos de las funciones particulares y vemos el ámbito global de la clase que queremos, podremos ver qué atributos son genéricos, como el contexto, o las preferencias de usuario, y qué atributos o parámetros son innecesarios en ciertos casos.</p>
<div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">miClase miInstancia <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> miClase<span style="color: #009900;">&#40;</span>parametroGlobal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
miInstance.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span>valor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Volviendo a mi consejo, si creamos una serie de setters (funciones encargadas de establecer un valor dentro de la instancia de una clase) que devuelvan el propio elemento, podemos encadenarlas, quedando un código, a mis ojos, de una lectura más fácil y sobretodo más abierto a ser extendido.</p>
<p>Siento no poner código en este consejo, creo que con mi explicación basta.<br />
Son las 5:30 de la mañana y no puedo dormir por culpa de la &#8220;música&#8221; de los borrachuzos que tengo en casa.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/android/android-consejos-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android para principiantes 1 &#8211; Preferencias de usuario</title>
		<link>http://www.thismayhem.com/android/android-para-principiantes-1-preferencias-de-usuario/</link>
		<comments>http://www.thismayhem.com/android/android-para-principiantes-1-preferencias-de-usuario/#comments</comments>
		<pubDate>Thu, 29 Dec 2011 23:01:01 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Principiantes]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[configuración]]></category>
		<category><![CDATA[preferences]]></category>
		<category><![CDATA[preferencias]]></category>
		<category><![CDATA[principiantes]]></category>
		<category><![CDATA[sharedpreferences]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=825</guid>
		<description><![CDATA[Recuerdo cuando empecé a programar para Android que uno de los primeros problemas que tuve fue cómo guardar datos de manera persistente entre un lanzamiento de la aplicación y el...]]></description>
			<content:encoded><![CDATA[<p>Recuerdo cuando empecé a programar para Android que uno de los primeros problemas que tuve fue cómo guardar datos de manera persistente entre un lanzamiento de la aplicación y el siguiente. En mi caso, la mayoría de los artículos que escriba sobre Android tendrán una base en el desarrollo de pepeSaldo, el problema era cómo guardar un valor (del tipo que queramos) en el teléfono del usuario y poder recuperarlo en cualquier momento.</p>
<p>Lo primero que encontré es el tipo de actividad <a href="http://developer.android.com/reference/android/preference/PreferenceActivity.html" target="_blank">PreferenceActivity</a> y de hecho fue la solución que utilicé durante un par de meses. Tiene limitaciones en el aspecto de la personalización de la interfaz de usuario, pero para principiantes igual os es útil. En pocas palabras,creais un xml en el propio Eclipse con un listado de preferencias que quereis que el usuario rellene, y nombres de las mismas, que se guardarán de manera automática cuando sean modificadas y tendremos acceso a las mismas desde cualquier parte de la aplicación.</p>
<p>Pero eso excede el ámbito de este artículo ya que solo quería abordar cómo guardar una variable y cómo recuperar ese valor en el futuro.</p>
<p>Para guardar una variable, primero creamos una instancia del objeto SharedPreferences.</p>
<p><div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003399;">Context</span> x <span style="color: #339933;">=</span> getApplicationContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p></p>
<div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SharedPreferences prefs <span style="color: #339933;">=</span> PreferenceManager.<span style="color: #006633;">getDefaultSharedPreferences</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
</p>
<p>Le estamos pidiendo las preferencias compartidas de la aplicación a &#8220;PreferenceManager&#8221; indicandole el contexto de la misma. Con el objeto &#8220;prefs&#8221; podemos empezar a trabajar para guardar variables.</p>
<p>Un sencillo ejemplo para guardar el nombre del usuario &#8220;Juan&#8221; en la variable &#8220;nombreUsuario&#8221;, sería asi:</p>
<p><div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">prefs.<span style="color: #006633;">edit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nombreUsuario&quot;</span>,<span style="color: #0000ff;">&quot;Juan&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
</p>
<p>Lo que hace esta linea es, en primer lugar llama al editor de preferencias, en segundo lugar le indicamos que queremos guardar una variable del tipo String en las preferencias compartidas, y que dicha variable se llamará &#8220;nombreUsuario&#8221; y tendrá un valor &#8220;Juan&#8221;. Por último llamamos a &#8220;commit()&#8221; que significa que ejecuta los cambios que hagamos en el editor. Esta llamada es imprescindible puesto que sin ella, la variable no se crearía.</p>
<p>Pues bien, ya tenéis vuestra primera variable guardada en las preferencias compartidas, y os estareis preguntando como diantres la recuperamos de ahi para usarla en distintas ejecuciones de la aplicación, o en distintas actividades de la misma.</p>
<p>El código es bastante sencillo, necesitamos haber creado las preferencias compartidas como en el ejemplo anterior, pero esta vez no llamamos el editor.</p>
<p><div class="codecolorer-container java dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003399;">String</span> nombre <span style="color: #339933;">=</span> prefs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nombreUsuario&quot;</span>,<span style="color: #0000ff;">&quot;valor por defecto&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
</p>
<p>En esa línea guardamos el valor de la variable &#8220;nombreUsuario&#8221; en la variable de tipo String &#8220;nombre&#8221;. Como seguro que habeis sido capaces de deducir, el segundo parametro de la llamada a &#8220;getString&#8221; es un valor por defecto en caso de no existir dicha variable. Es decir, si le pedimos el nombre antes de haber añadido el valor, nos devolvería &#8220;valor por defecto&#8221; pero si ya hemos llamado al código anterior que la establecía como &#8220;Juan&#8221;, nos devolverá &#8220;Juan&#8221;.</p>
<p>Los mismo se puede hacer con distintos tipos de datos, igual que hemos hecho con String&#8217;s. Existen métodos como &#8220;getFloat, getInt, getBoolean etc&#8221; que nos permites guardar el tipo de datos que nos interese.</p>
<p>Lamentablemente en las preferencias no podemos guardar (por lo menos no de manera sencilla) objetos, como podemos en iOS.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/android/android-para-principiantes-1-preferencias-de-usuario/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Problemas con PepePhone</title>
		<link>http://www.thismayhem.com/android/problemas-con-pepephone/</link>
		<comments>http://www.thismayhem.com/android/problemas-con-pepephone/#comments</comments>
		<pubDate>Tue, 27 Dec 2011 18:25:41 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[pepephone]]></category>
		<category><![CDATA[pepesaldo]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=809</guid>
		<description><![CDATA[Estos días, los detalles de llamadas de &#8220;Mi pepephone&#8221; tardan una barbaridad en actualizarse y por tanto se ve afectado el rendimiento de la aplicación pepeSaldo que desarrollé. He intentado...]]></description>
			<content:encoded><![CDATA[<p>Estos días, los detalles de llamadas de &#8220;Mi pepephone&#8221; tardan una barbaridad en actualizarse y por tanto se ve afectado el rendimiento de la aplicación pepeSaldo que desarrollé. He intentado darle un nuevo enfoque a la forma de extraer los datos pero todos flaquean en el tiempo de respuesta de sus servidores.</p>
<p>La aplicación funciona perfectamente y la única crítica que recibo y comparto es el tiempo que tarda en actualizar los datos. No sé si con el release de la aplicación oficial ellos habrán logrado solventar este problema pero por lo que a mi respecta, no puedo hacer mas. Si va lento, va lento.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/android/problemas-con-pepephone/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>pepeSaldo &#8211; Nos comemos la manzana</title>
		<link>http://www.thismayhem.com/iphone/pepesaldo-nos-comemos-la-manzana/</link>
		<comments>http://www.thismayhem.com/iphone/pepesaldo-nos-comemos-la-manzana/#comments</comments>
		<pubDate>Sat, 26 Nov 2011 20:30:48 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[aplicación]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[pepephone]]></category>
		<category><![CDATA[pepesaldo]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=795</guid>
		<description><![CDATA[Lo que llevo hasta ahora: Tras un caluroso verano peleandome con Android y aprendiendo lo justo y lo necesario para crear una aplicación más o menos decente (que ha salido...]]></description>
			<content:encoded><![CDATA[<p>Lo que llevo hasta ahora:</p>
<p>Tras un caluroso verano peleandome con Android y aprendiendo lo justo y lo necesario para crear una aplicación más o menos decente (que ha salido bastante bien, todo hay que decirlo), me animé a empezar el desarrollo de la versión para iPhone de pepeSaldo.</p>
<p>Hay que decir que hace poco vendí mi iPhone (la crisis&#8230;) y que he tenido que trabajar con el emulador. No quiero que esto suene a queja, ¡para nada!, ya que mi experiencia con la versión virtual del iPhone es bastante buena.</p>
<p>Llevo algo más de 10 días en lo que considero aprendizaje-desarrollo de la aplicación dedicando todo el tiempo libre que tengo, que no es mucho y poco a poco voy teniendo resultados. Tengo que admitir que me ha enganchado este lenguaje (objetive c) y que me parece mucho más intuitivo y bonito de lo que parece a simple vista.</p>
<p>Os pongo alguna captura de una primera versión, muy cruda todavía, para que os hagáis una idea (si es que alguien llega al post!). De momento solo muestra los detalles más básicos, como el total de consumo de datos y llamadas, y una provisional página de configuración.</p>
<p>Sin animaciones, ni gráficas, ni reporte de bugs, ni ninguna de las cosas que fui añadiendo poco a poco a la versión original. Para los que vivisteis ese proceso, deciros que pretendo que sea igual, poco a poco.</p>

<a href='http://www.thismayhem.com/iphone/pepesaldo-nos-comemos-la-manzana/attachment/main/' title='main'><img width="150" height="150" src="http://www.thismayhem.com/wp-content/uploads/2011/11/main-150x150.jpg" class="attachment-thumbnail" alt="main" title="main" /></a>
<a href='http://www.thismayhem.com/iphone/pepesaldo-nos-comemos-la-manzana/attachment/setup/' title='setup'><img width="150" height="150" src="http://www.thismayhem.com/wp-content/uploads/2011/11/setup-150x150.jpg" class="attachment-thumbnail" alt="setup" title="setup" /></a>
<a href='http://www.thismayhem.com/iphone/pepesaldo-nos-comemos-la-manzana/attachment/untitled-1/' title='Untitled-1'><img width="150" height="150" src="http://www.thismayhem.com/wp-content/uploads/2011/11/Untitled-1-150x150.jpg" class="attachment-thumbnail" alt="Untitled-1" title="Untitled-1" /></a>

<p>Entre hoy y mañana pretendo tener alguna cosa medio decente que subir al appstore (previo pago de la maldita licencia). Eso si, como dije en su momento, esta app <strong>no</strong> será gratuita, aunque considero el pago de 1€ algo simbólico  en relación al trabajo y sobre todo el tiempo que le he dedicado a la de Android y el que le voy a dedicar a esta.</p>
<p>De momento nada más, iré actualizando el post a medida que vaya añadiendo cosas a la aplicación.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/iphone/pepesaldo-nos-comemos-la-manzana/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Find and Replace text from Blob in MySql</title>
		<link>http://www.thismayhem.com/general/find-and-replace-text-from-blob-in-mysql/</link>
		<comments>http://www.thismayhem.com/general/find-and-replace-text-from-blob-in-mysql/#comments</comments>
		<pubDate>Tue, 02 Aug 2011 14:30:04 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[blob]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[moodle]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[text replacement]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=774</guid>
		<description><![CDATA[So here&#8217;s the scenario: I had a huge database that came from moodle which contained a table with a blob field that had some text in it. The thing is,...]]></description>
			<content:encoded><![CDATA[<p>So here&#8217;s the scenario:<br />
I had a huge database that came from moodle which contained a table with a blob field that had some text in it. The thing is, I had placed some embed code inside of it and wanted to update the domain from which to call the flash swf file. When I opened the database in Navicat I saw that I could edit those fields one by one but of course that&#8217;s not something I&#8217;d like to do over a thousand records.</p>
<p>I&#8217;m no mysql guru but I came up with theses two queries.<br />
The first one will return the field you want to see in text mode, the second one will replace some text with some other text on the specified field.</p>
<div class="codecolorer-container mysql dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #990099; font-weight: bold;">SELECT</span> CAST<span style="color: #FF00FF;">&#40;</span>fieldName <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #000099;">CHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">10000</span><span style="color: #FF00FF;">&#41;</span>CHARACTER <span style="color: #990099; font-weight: bold;">SET</span> utf8<span style="color: #FF00FF;">&#41;</span> <br />
<span style="color: #990099; font-weight: bold;">FROM</span> &nbsp; tableName<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> &nbsp;conditions<span style="color: #000033;">;</span></div></td></tr></tbody></table></div>
<div class="codecolorer-container mysql dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #990099; font-weight: bold;">UPDATE</span> tableName<br />
<span style="color: #990099; font-weight: bold;">SET</span> fieldName <span style="color: #CC0099;">=</span> <span style="color: #000099;">replace</span><span style="color: #FF00FF;">&#40;</span>CAST<span style="color: #FF00FF;">&#40;</span>fieldName <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #000099;">CHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">10000</span><span style="color: #FF00FF;">&#41;</span>CHARACTER <span style="color: #990099; font-weight: bold;">SET</span> utf8<span style="color: #FF00FF;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000033;">,</span><span style="color: #008000;">&quot;needle&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000033;">,</span><span style="color: #008000;">&quot;replacement&quot;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></div></td></tr></tbody></table></div>
<p>As usual, I hope it&#8217;s useful for someone out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/general/find-and-replace-text-from-blob-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8220;pepeSaldo&#8221; Historia, desarrollo y futuro</title>
		<link>http://www.thismayhem.com/android/new-android-app-pepesaldo-spanish/</link>
		<comments>http://www.thismayhem.com/android/new-android-app-pepesaldo-spanish/#comments</comments>
		<pubDate>Sat, 11 Jun 2011 17:38:15 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[android application]]></category>
		<category><![CDATA[android pepephone]]></category>
		<category><![CDATA[gasto pepephone]]></category>
		<category><![CDATA[pepephone]]></category>
		<category><![CDATA[saldo]]></category>
		<category><![CDATA[saldo contrato pepephone]]></category>
		<category><![CDATA[spanish]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=708</guid>
		<description><![CDATA[Actualización: Desde hace unos días estoy desarrollando la versión para Iphone de pepesaldo. Podría seguir su progreso en el siguiente enlace: http://www.thismayhem.com/iphone/pepesaldo-nos-comemos-la-manzana/ Historia: Vamos a hacer un poco de memoria...]]></description>
			<content:encoded><![CDATA[<p><strong>Actualización:</strong><br />
Desde hace unos días estoy desarrollando la versión para Iphone de pepesaldo. Podría seguir su progreso en el siguiente enlace:</p>
<p>http://www.thismayhem.com/iphone/pepesaldo-nos-comemos-la-manzana/</p>
<p><strong>Historia:</strong></p>
<p>Vamos a hacer un poco de memoria y ver el progreso que tuvo la aplicación desde sus comienzos.  Me presento, me llamo Juan, soy un programador de Madrid (si, los que tenéis la app instalada ya habéis leído esto <img src='http://www.thismayhem.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ), tengo 23 años y empecé el desarrollo de esta aplicación básicamente por la necesidad de conocer el consumo de mi nueva línea de teléfono sin tener que acceder al servicio Mi Pepephone que ofrece la compañía.</p>
<p>Tenía mi flamante móvil <strong>HTC Legend </strong>(Nunca he sido de gastarme una millonada en el móvil), de Android claro, y pensé que podría hacer una aplicación que me diera la información que quiero sin tener que acceder al ya mencionado servicio. Tras evaluar un poco la forma en la que funciona el site de Pepephone me di cuenta de que era más que posible, no voy a entrar en detalles pero en definitiva, la primera versión simulaba la interacción del usuario con el site con PHP y un par de llamadas desde cUrl para loguearme, acceder a los detalles de llamadas, y calcular el total.<br />
Por aquel entonces pepeSaldo tenía un aspecto tal que así:</p>
<p><a href="http://www.thismayhem.com/wp-content/uploads/2011/06/v1.jpg"><img class="size-medium wp-image-785 alignleft" title="v1" src="http://www.thismayhem.com/wp-content/uploads/2011/06/v1-199x300.jpg" alt="" width="199" height="300" /></a></p>
<p>Como podeis ver, solo indicaba el consumo de llamadas y mensajes. Es decir, el gasto en euros, no el consumo en megas. Por aquel entonces contaba con la increible cifra de 20 usuarios. Digo 20, pero unos días antes de eso, eramos solo mi chica Lorena y yo. Todo era tan sencillo por aquel entonces&#8230; Si me funcionaba a mi, y le funcionaba a ella, ¡pues tan contentos!. Pero se me ocurrió postear en el foro de GSMSpain para ver qué clase de acogida tendría una aplicación como la mía, o algo más desarrollada. Ver si efectivamente los usuarios querían algo así. De ahí llegamos a los 20 o 30 usuarios en nada de tiempo, pero el foro de GSMSpain no fue el mayor empujon que recibió esta aplicación. Un día vi un post en HTCMania que hablaba de mi app y creé una cuenta y fui a contar un poco la historia. Desde ahi todo se vuelve borroso, porque pasamos de 20 o 30 usuarios a 200, de 200 a 600 y con las mejoras y el boca a boca hemos llegado a la nada despreciable cifra de 6000 usuarios.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>Desarrollo:</strong></p>
<p>Ya he contado como empezó, siendo una aplicación básicamente en php que hacia una consulta por url desde android y devolvía los valores para mostrarlos en el terminal. Esto es un escenario menos seguro de lo que a mucha gente le gustaría, pero hay que tener en cuenta que era una app desarrollada por mi, usada por mi, que accedía a mi servidor, para recibir una respuesta de mi aplicacion en php. Recalco que era mia tanto porque la gente que criticaba la seguridad de la misma por aquel entonces parece que fallaba a la hora de entender que yo no estaba ganando nada con todo esto, tenía algo que me funcionaba y quería compartirlo con la comunidad.</p>
<p><a href="http://www.thismayhem.com/wp-content/uploads/2011/06/v2.jpg"><img class="size-medium wp-image-786 alignleft" title="v2" src="http://www.thismayhem.com/wp-content/uploads/2011/06/v2-200x300.jpg" alt="" width="200" height="300" /></a>De ahí al primer contacto con pepePhone, me pidieron que fuera a verles a sus oficinas en pozuelo (Madrid), y todo el mundo muy ilusionado. Por aquel entonces estamos hablando de una versión de la aplicación como esta, con un montón de funcionalidad más que la original, incluyendo la primera versión de los detalles de llamadas, consumo de datos básico y un par de iconitos muy graciosos que le daban un toque a la pobre.</p>
<p>Para la reunión tuve que reescribir toda la parte del código que se comunicaba con mi servidor para que lo hiciera directamente desde el terminal y por tanto no hubiera el potencial de fugas de datos al comunicarse con él. A partir de ese momento, me dediqué a mejorar la aplicación corrigiendo los bugs y problemas que me reportaban los usuarios de HTCMania, hice algún pinito con un widget mas bien mediocre que no tuvo mucha acogida.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><a href="http://www.thismayhem.com/wp-content/uploads/2011/06/v3.jpg"><img class="size-medium wp-image-787 alignleft" title="v3" src="http://www.thismayhem.com/wp-content/uploads/2011/06/v3-200x300.jpg" alt="" width="200" height="300" /></a>Cambié el diseño y añadí mucha funcionalidad, incluidas las alertas (que nunca han funcionado del todo bien), en esta versión. Adoptando el color corporativo y una apariencia mucho más atractiva que el azul anterior, aunque como siempre, hubo gente a la que le gustaba más la anterior (he aprendido a ignorar las críticas no constructivas con este proyecto).</p>
<p>Un día, después de hablar con un conocido que desarrolla aplicaciones para Android también, me dio la idea de deshacerme del panel de preferencias que se genera por defecto en Android y crear uno yo para pepeSaldo, dándome así una mayor libertad y eliminando los límites de personalización que impone el tema de los <strong>PreferenceActivity </strong>(o como se llame). Ahí fue cuando tuve una conversación con PepePhone en la que me quedó claro que no iba a ser el desarrollado de la aplicación oficial, ya que fuimos incapaces de llegar a un acuerdo en las condiciones del mismo.</p>
<p>&nbsp;</p>
<p><a href="http://www.thismayhem.com/wp-content/uploads/2011/06/v4.jpg"><img class="alignleft size-medium wp-image-788" title="v4" src="http://www.thismayhem.com/wp-content/uploads/2011/06/v4-200x300.jpg" alt="" width="200" height="300" /></a>Tome la determinación de eliminar todo el material gráfico vinculante a dicha compañía de mi aplicación, ya que si no voy a ser la oficial, por lo menos voy a tener la libertad de darle la estética que me venga en gana. Terminando en la versión actual, (sé que me he saltado muchas apariencias y versiones) que podéis ver en esta imagen.</p>
<p>Opté por las esquinas redondeadas y los tonos de grises. Muchos habéis dicho que era mejor la anterior, otros muchos me habéis felicitado por su simplicidad y limpieza. Esta versión ya incluye el release del widget actual, la imposición del aspecto de la barra de consumo con color amarillo para evitar problemas de lectura del texto superpuesto, y un montón con cositas que hacen que sea por lo menos 6 veces mejor que la anterior! (o más!)</p>
<p>Quité la ventanita de &#8220;Actualizando datos&#8221; y la reemplacé por una animación del icono del botón de actualizar, menos intrusiva.</p>
<p>&nbsp;</p>
<p><strong>Futuro:</strong></p>
<p>A día de hoy (9 de Septiembre de 2011) PepePhone ha realizado un comunicado en el que deja claro  que pepeSaldo y  yo, como único desarrollador de la misma desde sus  comienzos, no va a ser la app oficial que representará a esta gran  compañía de telefonía móvil. Estuvo cerca pero por motivos que no vienen  al caso decidieron dejar la tarea del desarrollo en manos de otro equipo  de programadores. Por tanto es inevitable preguntarse, ¿Y ahora qué?</p>
<p>Pues ahora a seguir desarrollando la aplicación, corrigiendo errores, añadiendo funciones y avanzando en la versión para iPhone (que si, yo también estoy en ello chicos). Podría abandonar la aplicación en cuanto salga la oficial, y es posible que lo haga, pero solo si la misma es mucho mejor que la de un servidor, cumple con todas las expectativas de los usuarios, tiene un soporte igual o más eficiente que el mio y último pero no por ello menos importante, me quede sin usuarios.</p>
<p>Como todos sabéis, la aplicación es gratuita, sin publicidad (aunque hubo un amago de ponerla un tiempo, pero viendo la rentabilidad que me daba lo retiré) costeada por mi generosidad a nivel de horas de desarrollo y soporte, por la ilusión que desde un principio me llevó a crear algo de la nada, embarcando en la aventura que ha sido el desarrollo de mi primera aplicación para Android (espero que no sea la última), y también (por qué negarlo) por vuestras palabras de apoyo y ocasionales cafés (generosos donativos) a través de la aplicación.</p>
<p>Creo que esto es todo lo que tengo que decir, dudo que muchos lleguéis al final del post, que me he currado, una vez más gracias a todos los que habéis estado ahí, y para los que os pille de nuevas la aplicación, daos prisa, antes de que salga la oficial! Capturar el código QR que os pongo aquí, pulsad en él o  buscad &#8220;pepesaldo&#8221; en el market desde vuestro teléfono y echadle un vistazo.</p>
<p><a href="https://market.android.com/details?id=pepe.labs"><img class="size-full wp-image-789 aligncenter" title="qrcode" src="http://www.thismayhem.com/wp-content/uploads/2011/06/qrcode.jpg" alt="" width="198" height="189" /></a>Para los usuarios de iPhone, estoy en ello!</p>
<p>&nbsp;</p>
<p><strong>Actualizaciones desde la publicación de este post:</strong></p>
<p>Con la versión 2.76b añadí la opción de elegir el color de fondo para la aplicación ya que tras eliminar la opción de utilizar los colores corporativos de la compañía recibí una oleada de emails diciendome que el nuevo aspecto era de todo menos bonito y que por favor volviera al anterior con el pepe de fondo y los lunares rojos sobre fondo blanco (¿o era al contrario?). El caso es que en un par de dichos emails se me sugirió que los usuarios pudieran elegir el color de fondo, dando así la posibilidad de un look mas personalizado y evitando el trauma que supone que una aplicación sea gris y no un estampado floral.</p>
<p>Fue una idea que no me gustó demasiado al principio pero puesto que es algo que ya había tenido en una de las primeras versiones, por no decir la primera, y dado que el número de emails pidiendo esta funcionalidad ha sido relativamente grande, la añadí. Aquí os dejo una imagen que muestra el cambio entre mi versión original y un fondo azul que no me gusta nada. Espero que a los que no les gustaba el gris les valga para estar un poquito más a gusto y a los que si, como yo, pues ahí queda.</p>
<p style="text-align: center;"><a href="http://www.thismayhem.com/wp-content/uploads/2011/06/colochange.png"><img class="aligncenter size-large wp-image-793" title="colochange" src="http://www.thismayhem.com/wp-content/uploads/2011/06/colochange-1024x384.png" alt="" width="558" height="209" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/android/new-android-app-pepesaldo-spanish/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>Drupal &#8211; Anonymous user node actions</title>
		<link>http://www.thismayhem.com/php/drupal/drupal-anonymous-user-node-actions/</link>
		<comments>http://www.thismayhem.com/php/drupal/drupal-anonymous-user-node-actions/#comments</comments>
		<pubDate>Thu, 26 May 2011 17:18:58 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Drupal]]></category>
		<category><![CDATA[anonymous]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[node_delete]]></category>
		<category><![CDATA[node_save]]></category>
		<category><![CDATA[thirparty integrations]]></category>
		<category><![CDATA[users]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=695</guid>
		<description><![CDATA[If you, like me, are building some sort of integration which involves the creation of nodes from an anonymous user from, lets say, you module. You may have encountered the...]]></description>
			<content:encoded><![CDATA[<p>If you, like me, are building some sort of integration which involves the creation of nodes from an anonymous user from, lets say, you module. You may have encountered the same problem I did yesterday while writing my own.</p>
<p>Getting this into context, instead of using <strong>services</strong> or some other module that takes care of third party software communication I wrote a simple module that would receive the actions + data from the other site and perform the needed actions. </p>
<p>I will not be mentioning the security measures I took in this post, just the code past the security check.</p>
<p>First I created a <strong>hook</strong> in my module, something like this:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">function</span> thismayhem_menu<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'thirdparty/actions'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">'page callback'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'thismayhem_actions'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">'page arguments'</span> <span style="color: #339933;">=&gt;</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; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$items</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>What that means is that every time a user visits the route <code class="codecolorer php dawn"><span class="php"><span style="color: #339933;">/</span>thidparty<span style="color: #339933;">/</span>actions</span></code> from your site they will trigger the <code class="codecolorer php dawn"><span class="php">thismayhem_actions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> function. </p>
<p>The function will handle basic node creation, modification and deletion.It will read the <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$_POST</span></span></code> vars and depending on the action variable it will add/modify/delete a node.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">function</span> thismayhem_actions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'create'</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$node</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Standard title&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Lorem ipsum dolor sit amet&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node_submit<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node_save<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nid</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; created correctly&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'modify'</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$node</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nid</span> &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nid'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Standard modified title&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Lorem ipsum dolor sit amet modified ;)&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node_submit<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node_save<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nid</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; modified correctly&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'delete'</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$user</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$userB</span> &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$user</span> &nbsp; &nbsp;<span style="color: #339933;">=</span> user_load<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node_delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nid'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$user</span> &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$userB</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Things to take into consideration are the fact that when we call <code class="codecolorer php dawn"><span class="php">node_save</span></code> we are sending the node object by reference and drupal will modify it to insert the <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nid</span></span></code> variable. This variable will contain the id of the newly created node.</p>
<p>Another thing I struggled with was that drupal doesn&#8217;t allow anonymous users to delete nodes (duh!) and therefor we need to tell drupal to make us the admin user, delete the node, and return us to our original user in case we were logged in.</p>
<p>This was a very simple implementation of this concepts so feel free to modify it and make it more complex. I will be releasing an integration with moodle at some point later this year.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/drupal/drupal-anonymous-user-node-actions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding the perfect number</title>
		<link>http://www.thismayhem.com/php/finding-the-perfect-number/</link>
		<comments>http://www.thismayhem.com/php/finding-the-perfect-number/#comments</comments>
		<pubDate>Wed, 18 May 2011 20:14:59 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[modulus]]></category>
		<category><![CDATA[perfect numbers]]></category>
		<category><![CDATA[simple]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=686</guid>
		<description><![CDATA[In number theory, a perfect number is a number n for which the sum of its divisors is equal to itself. That said, todays php snippet is how to find...]]></description>
			<content:encoded><![CDATA[<p>In number theory, a perfect number is a number <strong>n</strong> for which the sum of its divisors is equal to itself. That said, todays php snippet is how to find out if a number is perfect or not. If we re-read the definition, we need to get all divisors of <strong>n</strong>. A divisor of a number is a smaller number that leaves no decimal part when <stron>n</strong> is divided by it.</p>
<p>In that way, 2 is a divider of 4 since 4/2 = 2,0.</p>
<p>To do this in php we have the <code class="codecolorer php dawn"><span class="php"><span style="color: #339933;">%</span></span></code> &#8220;Modulus&#8221; operator which returns the remainder of a division.<br />
With that said, if we loop from 1 to <strong>n</strong> and check if those numbers have a remainder of zero we get all the dividers. If we then add them all together and the resulting number is equal to <strong>n</strong> we have ourselves a perfect number.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> isPerfect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$sum</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$n</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$sum</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #339933;">%</span><span style="color: #000088;">$i</span> <span style="color: #339933;">===</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>?<span style="color: #000088;">$i</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> &nbsp; <span style="color: #666666; font-style: italic;">//If the remainder of dividing $n/$i is zero, add $i to $sum</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sum</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span>?<span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">:</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/finding-the-perfect-number/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Webservices &#8211; Getting moodle lessons data to your site.</title>
		<link>http://www.thismayhem.com/moodle/webservices-getting-moodle-lessons-data-to-your-site/</link>
		<comments>http://www.thismayhem.com/moodle/webservices-getting-moodle-lessons-data-to-your-site/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 15:33:12 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Moodle]]></category>
		<category><![CDATA[Webservices]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[lessons]]></category>
		<category><![CDATA[moodle]]></category>
		<category><![CDATA[pages]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[webservice]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=609</guid>
		<description><![CDATA[Note: There was an encoding problem the first time I posted this, now it&#8217;s fixed. This simple webservice allows us to get lessons from moodle. The data that we will...]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>Note:</strong> There was an encoding problem the first time I posted this, now it&#8217;s fixed.</p></blockquote>
<p>This simple webservice allows us to get lessons from moodle. The data that we will be able to fetch includes the title, the id of the page, the content and some other I wasn&#8217;t really interested in. When I started writting this code I had assumed there&#8217;d be a moodle function that would take the job off my hands and I would just have to go through the process of making and enabling the webservice for it. But it turned out to be simpler than that! Since all the data is already stored in a table in the database.</p>
<p>I&#8217;m going to assume you&#8217;ve read &#8220;<a href="http://docs.moodle.org/en/Development:Web_services_%282.0_onwards%29">Development: Web services (2.0 onwards)</a>&#8221; so I&#8217;ll not explain how to build you webservices. Just how this one works.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<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;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MOODLE_INTERNAL'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">die</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$CFG-&gt;libdir</span>/externallib.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> moodle_lesson_external <span style="color: #000000; font-weight: bold;">extends</span> external_api <span style="color: #009900;">&#123;</span><br />
<span style="color: #009933; font-style: italic;">/**<br />
* Returns description of method parameters<br />
* @return external_function_parameters<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> get_lesson_pages_parameters<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> external_function_parameters<span style="color: #009900;">&#40;</span><br />
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lessonid'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> external_value<span style="color: #009900;">&#40;</span>PARAM_INT<span style="color: #339933;">,</span> &nbsp;<span style="color: #0000ff;">'id of the lesson'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009933; font-style: italic;">/**<br />
* Get Lesson<br />
* @return the lesson we wanted, I just included the lesson id, the url and the title<br />
* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; but there are other fields available, check the lessons table in the DB<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> get_lesson_pages<span style="color: #009900;">&#40;</span><span style="color: #000088;">$lessonid</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//Don't forget to set it as static</span><br />
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$CFG</span><span style="color: #339933;">,</span> <span style="color: #000088;">$DB</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$CFG-&gt;dirroot</span>/mod/lesson/lib.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$DB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_records_list</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lesson_pages'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'lessonid'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$pages</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 />
<br />
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$pages</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><br />
<span style="color: #0000ff;">'page_id'</span> &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">'page_title'</span> &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">'page_url'</span>&nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;YOURSITE/mod/lesson/view.php?id=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$lessonid</span><span style="color: #339933;">.</span><br />
<span style="color: #0000ff;">&quot;&amp;pageid=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">validate_parameters</span><span style="color: #009900;">&#40;</span><br />
<span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">get_lesson_pages_parameters</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lessonid'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$lessonid</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$pages</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009933; font-style: italic;">/**<br />
* &nbsp; Return values<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> get_lesson_pages_returns <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> external_multiple_structure<span style="color: #009900;">&#40;</span><br />
<span style="color: #000000; font-weight: bold;">new</span> external_single_structure<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><br />
<span style="color: #0000ff;">'page_id'</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> external_value<span style="color: #009900;">&#40;</span>PARAM_TEXT<span style="color: #339933;">,</span><span style="color: #0000ff;">'The page id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">'page_title'</span>&nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> external_value<span style="color: #009900;">&#40;</span>PARAM_TEXT<span style="color: #339933;">,</span><span style="color: #0000ff;">'The page title'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">'page_url'</span>&nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> external_value<span style="color: #009900;">&#40;</span>PARAM_TEXT<span style="color: #339933;">,</span><span style="color: #0000ff;">'The url of the page'</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lesson'</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Well, there was not function I could find  that fetched lesson pages, but that was probably because it was achievable by a simple query. In this case I called <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$DB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_records_list</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lesson_pages'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'lessonid'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> which would translate to <code class="codecolorer sql dawn"><span class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`lesson_pages`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`lessonid`</span><span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span></span></code>. That means that if we add more values to the array it would fetch all the pages we want from the given lessons.</p>
<p>Note that to make this a workable webservice you must have followed the steps specified at the previous link and execute the following code in your database.</p>
<div class="codecolorer-container sql dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><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;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> mdl_external_functions <span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">,</span>classname<span style="color: #66cc66;">,</span>methodname<span style="color: #66cc66;">,</span>classpath<span style="color: #66cc66;">,</span>component<span style="color: #66cc66;">,</span>capabilities<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><br />
<span style="color: #66cc66;">&#40;</span>moodle_lesson_get_lesson_pages<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'moodle_lesson_external'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'get_lesson_pages'</span><span style="color: #66cc66;">,</span><br />
<span style="color: #ff0000;">'mod/lesson/externallib.php'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'moodle'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'moodle/mod/lesson:view'</span><span style="color: #66cc66;">&#41;</span></div></td></tr></tbody></table></div>
<p>This basically tells moodle about our function and allows us to  enable/disable it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/moodle/webservices-getting-moodle-lessons-data-to-your-site/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twin Primes in PHP</title>
		<link>http://www.thismayhem.com/php/twin-primes-in-php/</link>
		<comments>http://www.thismayhem.com/php/twin-primes-in-php/#comments</comments>
		<pubDate>Mon, 28 Feb 2011 12:14:45 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[My Functions]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tip of the day]]></category>
		<category><![CDATA[prime numbers]]></category>
		<category><![CDATA[twin primes]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=597</guid>
		<description><![CDATA[Yesterday my girlfriend Lorena showed me a book named La Solitudine dei Numeri Primi that based a story around the twin prime numbers. I think it made a metaphor around...]]></description>
			<content:encoded><![CDATA[<p>Yesterday my girlfriend Lorena showed me a book named La Solitudine dei Numeri Primi that based a story around the twin prime numbers. I think it made a metaphor around it showing how people can be so close yet never together. I must say I haven’t read it yet.</p>
<p>Back to coding though, I like the idea of two prime numbers that are so close yet so far apart since between any two given numbers there are infinite numbers too. I wrote a really simple function that will check if a number is a prime number (only for whole numbers) and if it’s a pair twin with the one two positions ahead, it prints them.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">function</span> twinPrime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span><span style="color: #000088;">$recursive</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><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;=</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$recursive</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>twinPrime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">2</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;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>It’s ugly, I know but it basically works just fine and if you have the need to know if you’re working with a primer number that happens to be a twin prime, feel free to use it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/twin-primes-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lux Vimeo Mod &#8211; float videos</title>
		<link>http://www.thismayhem.com/php/lux-vimeo-mo-float-videos/</link>
		<comments>http://www.thismayhem.com/php/lux-vimeo-mo-float-videos/#comments</comments>
		<pubDate>Sat, 19 Feb 2011 18:25:34 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[float video]]></category>
		<category><![CDATA[floating]]></category>
		<category><![CDATA[mod]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[vimeo]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=590</guid>
		<description><![CDATA[I installed the Lux Vimeo plugin for wordpress for one of my clients recently and wanted to be able to float those videos on demand without having to get into...]]></description>
			<content:encoded><![CDATA[<p>I installed the Lux Vimeo plugin for wordpress for one of my clients recently and wanted to be able to float those videos on demand without having to get into coding (don&#8217;t get me wrong, I love it, but the clients don&#8217;t). So what I did basically is added a new attribute to the plugin, made it default to &#8220;none&#8221; and if the user changed it, float the video inside a div element to the chosen side.</p>
<p>Basically all I did is allowed the user to, instead of using <code class="codecolorer text dawn"><span class="text">[vimeo clip_id='XXX']</span></code> and then having to wrap that into a floating element if they wanted text to wrap around it use the more intuitive <code class="codecolorer text dawn"><span class="text">[vimeo clip_id='XXX' float='left']</span></code>  or <code class="codecolorer text dawn"><span class="text">[vimeo clip_id='XXX' float='right']</span></code> and let the plugin handle the wrapping.</p>
<p>Pretty straightforward code, here&#8217;s the entire plugin for you to download.</p>
<p>Note that I didn&#8217;t modify any of the original code written by the author as it worked just the way I needed to (aside from the mentioned modification.</p>
<p style="text-align: center;"><a href="http://www.thismayhem.com/wp-content/uploads/2011/02/lux_vimeo_mod.txt">Click to Download</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/lux-vimeo-mo-float-videos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ceros a la izquierda</title>
		<link>http://www.thismayhem.com/general/ceros-a-la-izquierda/</link>
		<comments>http://www.thismayhem.com/general/ceros-a-la-izquierda/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 08:32:27 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[funciones]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zeros izquierda]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=579</guid>
		<description><![CDATA[Si conoces la función sprintf puede que ya sepas esto o que, como para mi, sea una novedad. Buscaba un metodo rápido de añadir ceros a la izquierda a un...]]></description>
			<content:encoded><![CDATA[<p>Si conoces la función <a href="http://php.net/manual/es/function.sprintf.php">sprintf </a> puede que ya sepas esto o que, como para mi, sea una novedad.</p>
<p>Buscaba un metodo rápido de añadir ceros a la izquierda a un numero de una longitud indefinida para que cumpla un formato en particular. Por ejemplo a la hora de hacer referencia a una serie de archivos con un nombre que contiene el siguiente formato <code class="codecolorer text dawn"><span class="text">imagen_XXX</span></code> donde las x&#8217;s representan un numero precedido de ceros en caso de tener una longitud menor de 3, no tenía una manera sencilla de hacerlo.</p>
<p>Dí por hecho que la única forma sería comprobar la longitud de la cadena y dependiendo de su longitud añadir uno, dos o ningún cero. Es rápido y funciona, no necesitaba nada más:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$numero</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'1'</span><span style="color: #339933;">:</span> <span style="color: #000088;">$numero</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;00&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$numero</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'2'</span><span style="color: #339933;">:</span> <span style="color: #000088;">$numero</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$numero</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span> <span style="color: #000088;">$numero</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$numero</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Son 5 lineas de codigo para este formato, pero las implicaciones de escalar este metodo a cadenas superiores, como por ejemplo 10 digitos no son nada buenas. Está claro que podriamos coger el número que de caracteres que queremos, restarle la longitud actual y añadir un 0 por cada unidad de dicha resta.<br />
El caso es que el mismo resultado utilizando <code class="codecolorer text dawn"><span class="text">sprintf()</span></code> ocupa una linea. Es más, no solo una linea sino una sola expresión:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%03d</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$numero</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>El formato que le estamos especificando a la funcion es un número de tres digitos, para el cual usara la variable $numero (d) precedida de tantos ceros como necesite.</p>
<p>No quiero dar a entender que el uso de sprint se limitá a esto, porque no es el caso, pero como ese es el título del post, lo dejamos aquí por hoy.</p>
<p>Espero que os guste!</p>
<p><strong>Actualización:</strong></p>
<p>Use esta entrada para contestar <a href="http://stackoverflow.com/questions/4171873/this-php-string-joining-is-driving-me-crazy/4171938" target="_blank">esta pregunta</a> el 13 de Noviembre.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/general/ceros-a-la-izquierda/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Google Maps API Class with PHP</title>
		<link>http://www.thismayhem.com/php/google-maps-api-class-with-php/</link>
		<comments>http://www.thismayhem.com/php/google-maps-api-class-with-php/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 12:28:38 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Code Explained]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[My Functions]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[coordinates]]></category>
		<category><![CDATA[google api]]></category>
		<category><![CDATA[google maps]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=535</guid>
		<description><![CDATA[During the last couple of days I&#8217;ve needed a decent way of getting coordinates of an address from a string and in the process of getting that problem solved I...]]></description>
			<content:encoded><![CDATA[<p>During the last couple of days I&#8217;ve needed a decent way of getting coordinates of an address from a string and in the process of getting that problem solved I started working with the Google Maps API again. For the process of reproducing the same results as I did you will need a developers key, you can get one <a href="http://code.google.com/intl/en_EN/apis/maps/signup.html" target='_blank'>here</a>.</p>
<p>The first thing our class needs to do when initialized is to be able to build a valid request url for google to authenticate, to do that we define a key variable. Also in addition to that we will get the data from our location.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">class</span> GMaps<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> __construct <span style="color: #009900;">&#40;</span><span style="color: #000088;">$from</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #990000;">key</span> &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YourKey&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">baseUrl</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://maps.google.com/maps/geo?output=xml&amp;key=&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #990000;">key</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">distance</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">here</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getInfoFrom</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$from</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Of course at this point in time we havent declared the <code class="codecolorer text dawn"><span class="text">getInfoFrom()</span></code> function so there is nothing to call. We will declare this right now. This function will be in charge of fetching the data that corresponds to a certain location specified by a string. The more concrete search, the most acurate data it will fetch.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getInfoFrom<span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$request_url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">baseUrl</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;oe=utf-8&amp;q=&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <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;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Response</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$point</span><span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Placemark</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Point</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<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;">$point</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: #000088;">$coordinatesSplit</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$point</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">coordinates</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'latitude'</span><span style="color: #009900;">&#93;</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$coordinatesSplit</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'longitude'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$coordinatesSplit</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'address'</span><span style="color: #009900;">&#93;</span> &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Placemark</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">address</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$details</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Placemark</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AddressDetails</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'countryName'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$details</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Country</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CountryName</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'countryCode'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$details</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Country</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CountryNameCode</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'areaName'</span><span style="color: #009900;">&#93;</span> &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$details</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Country</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AdministrativeArea</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AdministrativeAreaName</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'adminArea'</span><span style="color: #009900;">&#93;</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #000088;">$details</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Country</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AdministrativeArea</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <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;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'adminArea'</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><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'postalCode'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'adminArea'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SubAdministrativeArea</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Locality</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">PostalCode</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">PostalCodeNumber</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</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;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'adminArea'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Locality</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'postalCode'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'adminArea'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Locality</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">PostalCode</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">PostalCodeNumber</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$value</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Last but not least a function that will return the nearest point to a given target from a set of posible points in an array. Of course that if you wanted to do this regularly you should just get the data once and store it in the database, then just compare latitude and longitude instead of fetching it every time.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> nearestPoint<span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$to</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$point</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$point_data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getInfoFrom</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$point</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$latitude</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$point_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'latitute'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">here</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'latitude'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$longitude</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$point_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'longitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">here</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'longitude'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$distance_temp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$latitude</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> &nbsp; <span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$latitude</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$longitude</span><span style="color: #009900;">&#41;</span>&nbsp; &nbsp; <span style="color: #339933;">-</span> &nbsp; <span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$longitude</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$distance_temp</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">distance</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: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$point</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">distance</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$distance_temp</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$return</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Basically it just checks if the distance between the source point and the current candidate is smaller that the previous one, if it is, we replace the value of <code class="codecolorer text dawn"><span class="text">$distance</span></code> with the new one and assign <code class="codecolorer text dawn"><span class="text">$return</span></code> to that target. In the end we return the closest point.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>So if for example I create a new instance of the class specifying my base location to &#8220;Madrid&#8221; and then get the closest city from the following list: &#8220;Tokyo&#8221;,&#8221;Paris&#8221;,&#8221;London&#8221; and &#8220;Moscu&#8221; like this:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$GMap</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GMaps<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Madrid&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$GMap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nearestPoint</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tokyo&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Paris&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;London&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Moscu&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>I get &#8220;Paris&#8221; as my output which is what we would expect from this. However, this class isn&#8217;t limited to this kind of comparison. You can do all kinds of crazy stuff with it, like getting the closest point, with real addresses like &#8220;4th Street, 15 New York, USA&#8221;. It works since Google&#8217;s API is smart enough to translate that into workable data.</p>
<p>I haven&#8217;t mentioned it, but you have coordinates, country, city and all sorts of interesting data available if you want it. Stay tuned for updates since I&#8217;ll be working with this little fellow for a while.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/google-maps-api-class-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Codeigniter Mod – Builder &#8211; Part 2</title>
		<link>http://www.thismayhem.com/php/codeigniter-mod-builder-part-2/</link>
		<comments>http://www.thismayhem.com/php/codeigniter-mod-builder-part-2/#comments</comments>
		<pubDate>Thu, 13 Jan 2011 23:19:06 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Code Explained]]></category>
		<category><![CDATA[codeIgniter]]></category>
		<category><![CDATA[My Functions]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[builder library]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[session variables]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=516</guid>
		<description><![CDATA[Continuing with the code from Codeigniter Mod – Builder I&#8217;ve extended the functionality of the library and added in-line comments to it. So basically this is just an update of...]]></description>
			<content:encoded><![CDATA[<p>Continuing with the code from <a href="http://www.thismayhem.com/php/codeigniter-mod-builder/">Codeigniter Mod – Builder</a> I&#8217;ve extended the functionality of the library and added in-line comments to it. So basically this is just an update of the previous code. I made the following session variables readily available for your code, change their names to the ones that suit your project <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'loggedIn'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'user_id'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'flash'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'error'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'notice'</span><span style="color: #009900;">&#41;</span></span></code>. The changelog would be something like this:</p>
<pre>- Session variable are grabbed on demand, while keeping some as default.
- Default session variables may be removed if desired.
</pre>
<p style="text-align: center;"><a href='http://www.thismayhem.com/wp-content/uploads/2011/01/Builder.php_1.txt'>Download the library</a></p>
<p>In essence what I did is added method called <code class="codecolorer text dawn"><span class="text">setSessionData($vars)</span></code> which will take an array of elements (single element will also be accepted) and add those elements from the session variables to the data variables on output. If an element is already present in the default ones and we call the function with it&#8217;s name inside the array, that element will be <strong>removed</strong> from the data.</p>
<p>If you are uncertain if an element is already in the <code class="codecolorer text dawn"><span class="text">$sessionData</span></code> array from the Builder object and want to make sure it is present on output, preppend a plus sign to the element name, so element <code class="codecolorer text dawn"><span class="text">edgar</span></code> would be force-added like this <code class="codecolorer text dawn"><span class="text">+edgar</span></code>.<br />
In the same line of thought, an element can be force removed, that means, removed if present by preppending a minus sign to the elements name, so element edgar would be force-removed like this <code class="codecolorer text dawn"><span class="text">-edgar</span></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/codeigniter-mod-builder-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Codeigniter Mod &#8211; Builder</title>
		<link>http://www.thismayhem.com/php/codeigniter-mod-builder/</link>
		<comments>http://www.thismayhem.com/php/codeigniter-mod-builder/#comments</comments>
		<pubDate>Mon, 10 Jan 2011 19:18:55 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Code Explained]]></category>
		<category><![CDATA[codeIgniter]]></category>
		<category><![CDATA[My Functions]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[breadcrumbs]]></category>
		<category><![CDATA[builder library]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[crumbs]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[mod]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=505</guid>
		<description><![CDATA[After finishing my work with the opencart platform for the last project I decided to take up some of my own adventures in code. To my surprise, I didn&#8217;t like...]]></description>
			<content:encoded><![CDATA[<p>After finishing my work with the opencart platform for the last project I decided to take up some of my own adventures in code. To my surprise, I didn&#8217;t like the way <a href="http://www.codeigniter.com">codeIgniter</a> loaded views anymore. I got used to the way opencart does it, by using breadcrumbs, a list of views you want to load, in an array, instead of<br />
individual calls to the <code class="codecolorer text dawn"><span class="text">$this-&gt;load-&gt;view(&quot;viewname&quot;)</span></code> method.</p>
<p>That is not the only problem I found. I also like to automatically load the language files of the views I&#8217;m using and pass them the data array I created on my controller and model calls. <a href="http://www.codeigniter.com">CodeIgniter</a>, loads the view at the moment you call it, granted you can load it to a variable instead of outputting it but, if I later modify a variable used in those views, they keep the old value. This may be the expected behaviour for you, or may not.</p>
<p>I want to be able to tell my code which views I want to load, what data I want to pass it and when to start the output.<br />
To solve this, I coded a very simple library that I called <strong>builder</strong> and while it&#8217;s still in development, since I&#8217;m still working on those projects, it saves me some time and makes my code more readable by doing some stuff for me in the background. </p>
<p>Not going to explain the code, just posting it to see if someone finds it useful.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<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: #000000; font-weight: bold;">&lt;?php</span> &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'BASEPATH'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No direct script access allowed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Builder<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$breadcrumbs</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; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$view</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CI</span> <span style="color: #339933;">=&amp;</span> get_instance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/*<br />
&nbsp; &nbsp; * This function can be called with a string or an array as it's attribute<br />
&nbsp; &nbsp; * it will add the views that are not present in the $breadcrumbs array<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> addCrumbs<span style="color: #009900;">&#40;</span><span style="color: #000088;">$crumbs</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$crumbs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$crumbs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$crumbs</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$crumbs</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$crumb</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$crumb</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">breadcrumbs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">breadcrumbs</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$crumb</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/*<br />
&nbsp; &nbsp; * It's a setter for the main view, granted we could just use<br />
&nbsp; &nbsp; * an equal sign, but I like this approach better since I could be picky<br />
&nbsp; &nbsp; * on what data can $view be set to<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> setView<span style="color: #009900;">&#40;</span><span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$view</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/*<br />
&nbsp; &nbsp; * Starts the output, explained in detail below<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> output<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</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;">CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span>? <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//I get some data (if present) from the session variables and add it to</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//the $data array for easy access all over my views</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'loggedIn'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">userdata</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'isLoggedIn'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">userdata</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flash'</span><span style="color: #009900;">&#93;</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">flashdata</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flash'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#93;</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;p&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">flashdata</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'notice'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">flashdata</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notice'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <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;">breadcrumbs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//For each breadcrumb (view) I load it's language and make it available by</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//merging the terms with the $data array once again.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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><span style="color: #004000;">breadcrumbs</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span> &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lang</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lang</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">language</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//For each breadcrumb (view) I load it's view as data and store it in the $data</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//array so it can be accessed from the main view, the str_replace solves the nested</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//controllers inside folder.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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><span style="color: #004000;">breadcrumbs</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;_&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$view</span><span style="color: #339933;">,</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">,</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>Granted some of the code will not be useful for you, since you don&#8217;t have the models I&#8217;m using, but the core functionality is an improved (for me) version of the view loading class native to codeIgniter. A sample usage, written just for the two followers I have could be something like this.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">class</span> test <span style="color: #000000; font-weight: bold;">extends</span> Controller<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// call the function test if php4</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; parent<span style="color: #339933;">::</span><span style="color: #004000;">Controller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">builder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addCrumbs</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'menu'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'userpanel'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'header'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'footer'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* The previous line tells builder, I want to load all those views &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in that particular order */</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">builder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setView</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">builder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">output</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>My view <i>test</i> would have all the breadcrumbs available as direct variables inside, so to echo my header I would just call <code class="codecolorer text dawn"><span class="text">echo $header;</span></code> from inside my code. Since I loaded the language files before loading the views, my header will contain all the terms it needed and all parsed variables as well. It&#8217;s just convention to add language files for each view (in my case) so if you don&#8217;t have terms just have an empty file.</p>
<p>Note that the order of the views added to the breadcrumbs is important. All breadcrumbs have the previous ones available to use inside of them but not the other way around. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/codeigniter-mod-builder/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Customer group emails on Opencart</title>
		<link>http://www.thismayhem.com/php/opencart/customer-group-emails-on-opencart/</link>
		<comments>http://www.thismayhem.com/php/opencart/customer-group-emails-on-opencart/#comments</comments>
		<pubDate>Mon, 13 Dec 2010 23:52:03 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Mods]]></category>
		<category><![CDATA[OpenCart]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[contact]]></category>
		<category><![CDATA[customer groups]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[improvement]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=494</guid>
		<description><![CDATA[Technically this should be implemented in the core of Opencart. Let me elaborate about it. You have your clients ordered in groups and you want to send a newsletter to...]]></description>
			<content:encoded><![CDATA[<p>Technically this should be implemented in the core of Opencart. Let me elaborate about it. You have your clients ordered in groups and you want to send a newsletter to those in one of the groups. Out of the box, Opencart lets you target clients by the products they&#8217;ve purchased and when you click on groups, instead of getting a list of customer groups as you would expect, you get two options: Newsletter customers and All customers.</p>
<p>So if I&#8217;ve got a group of clients that has all my friends and want to email them at the same time I have to manually search for them and add the to the list every single time I want to send this newsletter to them. Of course this is in no way optimal and I made a few changes to the system to allow a more intuitive group mailing system.</p>
<p>In this article I will show you how to enable group selection in the Mail page from the control panel.</p>
<p>Add the following lines to file: <code class="codecolorer text dawn"><span class="text">admin/controller/sale/contact.php</span></code></p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">model</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sale/customer_group'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'customer_group'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">model_sale_customer_group</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCustomerGroups</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>That way we have the customer group list available for the view. Remember that <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'variable'</span><span style="color: #009900;">&#93;</span></span></code> is transformed to <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$variable</span></span></code> in the view. That said, we open the <code class="codecolorer text dawn"><span class="text">admin/view/template/sale/contact.tpl</span></code> file and we enter the following code just after the blank option around line 34:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PD9waHAgZm9yZWFjaCAoJGN1c3RvbWVyX2dyb3VwIGFzICRncm91cCk6ID8<span style="color: #339933;">+</span><br />
<br />
<span style="color: #0000ff;">&quot;&gt;<br />
PD9waHAgZWNobyAkZ3JvdXBbJ25hbWUnXTsgPz4=<br />
<br />
PD9waHAgZW5kZm9yZWFjaCA/Pg==</span></div></td></tr></tbody></table></div>
<p>By doing that we now have the list of customer groups available in the contact form, but those elements are pretty much useless since we can&#8217;t work with them yet. Doing that will take a little tweaking still since we need to create a function in the customer model that allows us to get the users who are part of a certain group.</p>
<p>Open file: <code class="codecolorer text dawn"><span class="text">admin/model/sale/customer.php</span></code> and add the following function at the end:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCustomerGroupCustomers<span style="color: #009900;">&#40;</span><span style="color: #000088;">$customer_group_id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT *<br />
FROM &quot;</span> <span style="color: #339933;">.</span> DB_PREFIX <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;customer<br />
WHERE customer_group_id = '&quot;</span> <span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$customer_group_id</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rows</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The last thing we need to do is handle the posting of the message. We do that by locating the switch statement around line 29 and adding a default case scenario, that means that if the value of the dropdown is not one of the ones coded on the system, it&#8217;s probably a customer group. If it&#8217;s a customer group, we will call the function we declared earlier and fetch the emails from the results. It&#8217;s as simple said as done:</p>
<p><code class="codecolorer text dawn"><span class="text">admin/controller/sale/contact.php</span></code></p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">//Around line 30 inside the switch block, add the default case:</span><br />
<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span><br />
<span style="color: #000088;">$customers_info</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">model_sale_customer_group</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCustomerGroupCustomers</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'group'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$customers_info</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$customer_info</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$emails</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$customer_info</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Would this break if there are no clients in the group? It would likely return an error saying, hey! you gave me an invalid target for my foreach loop! So to avoid that we will put the loop inside a conditional statement that will check if there are any matches:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">//Around line 30 inside the switch block, add the default case:</span><br />
<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span><br />
<span style="color: #000088;">$customers_info</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">model_sale_customer_group</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCustomerGroupCustomers</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'group'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<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;">$customers_info</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$customers_info</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$customer_info</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$emails</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$customer_info</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>If we didn&#8217;t miss anything, we should now have a working improved version of the mailing system for Opencart. Make sure you do some testing, and if you&#8217;re happy with the results, consider using it on a working site at your own risk. As a disclaimer I can assure you I&#8217;m using it and it works, make sure to ask any questions as comments here and I&#8217;ll help you out <img src='http://www.thismayhem.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/opencart/customer-group-emails-on-opencart/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Specials&#8217; time on Opencart</title>
		<link>http://www.thismayhem.com/php/opencart/specials-time-on-opencart/</link>
		<comments>http://www.thismayhem.com/php/opencart/specials-time-on-opencart/#comments</comments>
		<pubDate>Mon, 06 Dec 2010 13:24:48 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Code Explained]]></category>
		<category><![CDATA[Mods]]></category>
		<category><![CDATA[OpenCart]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[modification]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[specials]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=480</guid>
		<description><![CDATA[Today I was adding some special offers to some products on an e-commerce I happen to be taking care of and I noticed that the system (Opencart) didn&#8217;t give me...]]></description>
			<content:encoded><![CDATA[<p>Today I was adding some special offers to some products on an e-commerce I happen to be taking care of and I noticed that the system (Opencart) didn&#8217;t give me the change to decide what timezone I wanted to use for the dates I set on the special prices, discounts and so on. This was something I had never taken into consideration since usually I don&#8217;t really care about server times. But this is different, I need it to work on the same timezone as the main client population (Spain). So I once again had to modify Opencart to achieve this.</p>
<p>You could build a front end in the admin settings area from the control panel with a nice drop down list to select the time zone the shop will be in, but since everyone reading this blog knows at least a little about programming and I&#8217;m going to assume you <strong>like</strong> playing with code, and server time zone&#8217;s are not something that gets changed normally all I&#8217;m going to do is tell you how to do the code version. Maybe if enough people request it, I will code the visual interface for this.</p>
<p>To tell the server we want to use a specific time zone, we must first know what the server is using as it&#8217;s time. The easiest way I can think of, is getting the difference from the timezone the server is using to the UTC. The <code class="codecolorer text dawn"><span class="text">date()</span></code> function has some not too known tricks up its sleeve. If we call it like this:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Z'</span><span style="color: #339933;">,</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This will return the difference in seconds between your servers&#8217; time and the UTC timezone. So to get the UTC time, we just have to deduct the value that we just received from the date function with the value that&#8217;s returned by the time() function.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">$utc_time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Z'</span><span style="color: #339933;">,</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This is taking for granted that the time zone you want to use is UTC. As you&#8217;ve probably guessed by now, all we have to do is add or remove the number of hours that differ from UTC multiplied by 3600 (seconds per hour) to the $utc_time variable to get the time we are after. As an example I&#8217;m going to use UTC+1</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">$utc_time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Z'</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">3600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Just replace the '1' with the offset to UTC</span></div></td></tr></tbody></table></div>
<p>Now for the Opencart specific part, I defined a new constant in the config.php file with the following code:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SYSTEM_NOW'</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Z'</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">3600</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The date format used there is the one used all over the system for date comparison and such. In particular if you go to the model/catalog/product.php file and look for the <code class="codecolorer text dawn"><span class="text">public function getProductSpecial()</span></code> you&#8217;ll see it builds a query that uses the <code class="codecolorer text dawn"><span class="text">NOW()</span></code> mysql function that will return the server time. To fix this, we just have to change the query to this:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #0000ff;">&quot;SELECT price FROM &quot;</span> <span style="color: #339933;">.</span> DB_PREFIX <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;product_special <br />
WHERE product_id = '&quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$product_id</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;' <br />
AND customer_group_id = '&quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$customer_group_id</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'<br />
AND ((date_start = '0000-00-00' OR date_start &lt;= &quot;</span> <span style="color: #339933;">.</span> SYSTEM_NOW <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;) <br />
AND (date_end = '0000-00-00' OR date_end &gt; &quot;</span> <span style="color: #339933;">.</span> SYSTEM_NOW <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;)) <br />
ORDER BY priority ASC, price ASC LIMIT 1&quot;</span></div></td></tr></tbody></table></div>
<p>Note that I added an &#8216;=&#8217; sign to the first comparison, but not to the second one. Doing so would result on the dates being inclusive when we set a special. You&#8217;ll have to modify all those queries to the new format if you want a system wide upgrade. All those queries are located (I believe) inside the same file, but I could (and probably will) be wrong.</p>
<p>So, that&#8217;s about it! No demo this time since it&#8217;s not something interactive, sorry.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/opencart/specials-time-on-opencart/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reordering a list with jQuery + Php &#8211; Part I</title>
		<link>http://www.thismayhem.com/jquery/reordering-a-list-with-jquery-php-part-i/</link>
		<comments>http://www.thismayhem.com/jquery/reordering-a-list-with-jquery-php-part-i/#comments</comments>
		<pubDate>Sat, 27 Nov 2010 20:07:30 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[control panel]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery ui]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=446</guid>
		<description><![CDATA[I&#8217;ve started to work on a control panel for one of my projects and wanted to liven it up a little by adding some jQuery magic into the mix. Are...]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started to work on a control panel for one of my projects and wanted to liven it up a little by adding some jQuery magic into the mix. Are you sick of manually ordering a list of items? There must be a better way of ordering items than either setting the new order position of each, or moving them individually up or down. Those seem to be the mainstream situations in the control panels I&#8217;ve worked with.</p>
<p>You&#8217;re either prompted to enter the new position in the list or you can move them up and down one at a time. I did <strong>not</strong> like this. Not at all. In this two part article I&#8217;m going to build a jQuery + Php solution for this. I want a system that can order items taking advantage of the sortable method from the jQuery UI, and doesn&#8217;t ask me to set a new position. I want to say, this goes here, and just do it. And so do you!</p>
<p>The jQuery part which we&#8217;ll be covering in this first part is quite straight forward, we create a list, and tell jQuery that we want to be able to sort it.</p>
<div class="codecolorer-container html4strict dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'reorderable-list'</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>Item 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>Item 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>Item 3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>Item 4<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>Item 5<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span></div></td></tr></tbody></table></div>
<p>You will need both jQuery and jQuery UI for this, so make sure you get them <a target='_blank' href="http://docs.jquery.com/Downloading_jQuery">here </a>and <a target='_blank' href="http://jqueryui.com/download">here</a>.</p>
<p>I&#8217;m going to assume you already know the basics of jQuery and can more or less select items from the DOM and manipulate them. So, inside <code class="codecolorer javascript dawn"><span class="javascript">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></span></code> we tell jQuery that the <code class="codecolorer css dawn"><span class="css">reorderable-list</span></code> class is to be sortable:</p>
<div class="codecolorer-container jquery dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="jquery codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #000066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.reorderable-list'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">sortable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This is pretty awesome on it&#8217;s own. A simple line has made a boring list into an amazing interactive and fancy one! Ok, maybe I&#8217;m exaggerating a little here.<br />
The first thing that hits me when I look at it is that I&#8217;m able to move the items horizontally as well as vertically. While you may want to leave it like this on some other scenarios, I particularly wanted to constrain the movement of my items to the Y axis, this means vertically. We do that the following way:</p>
<div class="codecolorer-container jquery dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="jquery codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #000066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">$</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.reorderable-list'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">sortable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; axis<span style="color: #339933;">:</span> <span style="color: #3366CC;">'y'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Of course you can leave the movement unconstrained of constrain it to be horizontal in case it&#8217;s another type of list, feel free to mess about with it. The last thing I didn&#8217;t feel too comfortable with, was the pointer, I wanted something that at least looked like it was being dragged. To achieve this feel I used the &#8216;move&#8217; cursor.</p>
<p>To see a working demo of what we&#8217;ve achieved so far, click on the demo link below.</p>
<p><a target='_blank' href="http://labs.thismayhem.com/post/jquery/reordering-jquery/"><img src="http://www.thismayhem.com/wp-content/uploads/2010/11/viewdemo1.gif" alt="" title="viewdemo" width="250" height="55" class="aligncenter size-full wp-image-460" /></a></p>
<p>Note how, if you refresh the page, the items return to the position they originally had, we will be handling that issue on the second part of this article where we will mix jQuery with php and some ajax for the communication between them two.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/jquery/reordering-a-list-with-jquery-php-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Events, targets, parents &amp; children</title>
		<link>http://www.thismayhem.com/jquery/events-targets-parents-children/</link>
		<comments>http://www.thismayhem.com/jquery/events-targets-parents-children/#comments</comments>
		<pubDate>Tue, 23 Nov 2010 17:08:20 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tip of the day]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=427</guid>
		<description><![CDATA[jQuery events can be triggered by clicking on the element we want, but also on any children elements that it may have. This may be the correct behaviour most of...]]></description>
			<content:encoded><![CDATA[<p>jQuery events can be triggered by clicking on the element we want, but also on any children elements that it may have. This may be the correct behaviour most of the time but sometimes, we want to trigger an event  <strong>just</strong> when the parent is clicked (replace click with the action you want).</p>
<p>This can be done easily with jQuery using the <code class="codecolorer javascript dawn"><span class="javascript">event.<span style="color: #660066;">target</span></span></code> property. A common solution for ensuring that the element who targeted the event was the one we binded the event with and not one of it&#8217;s children is simply asking this:</p>
<div class="codecolorer-container javascript dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.parent'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<span style="color: #006600; font-style: italic;">// the click even is binded to .parent</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span> <span style="color: #339933;">===</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// is .parent the one who triggered this?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// your actions</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>For example, you have a parent <code class="codecolorer php dawn"><span class="php"><span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span></span></code> that you want to turn blue when it&#8217;s clicked on, but not when an element that&#8217;s <strong>inside</strong> of it is clicked, you would use this. If you don&#8217;t, then action executed on a children would trigger this color change too.<br />
You can check who is calling an event by using <code class="codecolorer javascript dawn"><span class="javascript"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span></span></code> but make sure you declare the even attribute in your function for this to work.</p>
<p>(:</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/jquery/events-targets-parents-children/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Imageless images</title>
		<link>http://www.thismayhem.com/php/imageless-images/</link>
		<comments>http://www.thismayhem.com/php/imageless-images/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 11:50:24 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Images]]></category>
		<category><![CDATA[My Functions]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[imageless images]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=403</guid>
		<description><![CDATA[Imagine the following scenario. A user comes to your site with images disabled and css enabled (ok, I know it&#8217;s not a common scenario but it may happen). This got...]]></description>
			<content:encoded><![CDATA[<p>Imagine the following scenario. A user comes to your site with images disabled and css enabled (ok, I know it&#8217;s not a common scenario but it may happen). This got me worried, since I wouldn&#8217;t be able to show my wonderful logo to them, or would I? If you stop to think of it, images are just a grid of pixels with different colors that represents something. So, using a little php magic and GD library, I made this simple function that can <em>convert</em> an image into a huge set of 1px div&#8217;s that have a background-color property of the corresponding color from the image.</p>
<p>You would need this class set in your stylesheet for this to work, since we need to ensure that the div elements are 1px wide for 1px high. Feel free to play around with this for interesting results, like making each pixel from the image be 4x bigger in your imageless image.</p>
<div class="codecolorer-container html4strict dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text/css'</span>&gt;</span><br />
.pixel{<br />
&nbsp; &nbsp; width:1px;<br />
&nbsp; &nbsp; height:1px;<br />
&nbsp; &nbsp; float:left;<br />
}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span></div></td></tr></tbody></table></div>
<p>The function is pretty straight-forward, it downloads the image fromthe given url, maps every pixel and stored their rgb information into an array, then it outputs a set of floating divs inside a container with the size of the actual image. Note that I&#8217;m not checking if the image was loaded correctly, nor do I believe this to be a useful function for the mainstream programmers.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">function</span> imageNot<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_img_url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$image</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">ImageCreateFromString</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_img_url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$width</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$width</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$y</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$height</span><span style="color: #339933;">;</span><span style="color: #000088;">$y</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$rgb</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span><span style="color: #000088;">$y</span><span style="color: #339933;">,</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$r</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rgb</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$g</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rgb</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$b</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #000088;">$rgb</span> <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$colormap</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$r</span>,<span style="color: #006699; font-weight: bold;">$g</span>,<span style="color: #006699; font-weight: bold;">$b</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;div style='width:&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$width</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;px;height:&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$height</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;px'&gt;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$colormap</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;div class='pixel' &nbsp;style='background-color:rgb(<span style="color: #006699; font-weight: bold;">$color</span>);'&gt;&lt;/div&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;/div&gt;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$return</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Since putting the actual result as in DOM elements in the post would be a crazy idea, I want to use an image to represent the result of the function that converts images into non-images (irony intended):</p>
<blockquote><p>
<img src="http://www.thismayhem.com/wp-content/uploads/2010/11/imagesnot.jpg" alt="" title="imagesnot" style='border:none;background-color:white;' width="102" height="102" class="aligncenter size-full wp-image-411" />
</p></blockquote>
<p>The result is actually quite impressive on the eye-candy side of the formula, but the resulting file size for a simple image would be enormously huge, something on the range of 300+ times the size of the actual image. So, consider this whole function something fun that I came up with, but think three times before using it somewhere other than your playground site (you <strong>do</strong> have one, right?). Maybe it can be used for something else, like captchas where those trying to break it wouldn&#8217;t have an actual image to break but a set of pixels to parse, or maybe that makes it easier for them? I seriously don&#8217;t know but I had fun coding this.</p>
<p>You can see a working example of this code, and the actual result (700kb+) for this image by clicking <a href="http://www.thismayhem.com/posts/imagesnot.php">here</a>.</p>
<p>Have fun, and see you next time!</p>
<p><a target='_blank' href="http://labs.thismayhem.com/post/php/imageless-images/"><img src="http://www.thismayhem.com/wp-content/uploads/2010/11/viewdemo1.gif" alt="" title="viewdemo" width="250" height="55" class="aligncenter size-full wp-image-460" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/imageless-images/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The rainbow gradient experiment</title>
		<link>http://www.thismayhem.com/php/the-rainbow-gradient-experiment/</link>
		<comments>http://www.thismayhem.com/php/the-rainbow-gradient-experiment/#comments</comments>
		<pubDate>Sun, 21 Nov 2010 14:13:12 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Code Explained]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[My Functions]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[color-picker]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[gradients]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[rainbow]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=387</guid>
		<description><![CDATA[This one has a little bit of a story. I was messing around with an image editing software (no bran names here), and while looking at the color picker palete...]]></description>
			<content:encoded><![CDATA[<p>This one has a little bit of a story. I was messing around with an image editing software (no bran names here), and while looking at the color picker palete I decided to see if I could do the same with my good old php and some css. What I noticed is that you don&#8217;t have to provide an hexadecimal value for colors on css, you can also provide a <strong>rgb </strong>value in the following format <code class="codecolorer php dawn"><span class="php">rgb<span style="color: #009900;">&#40;</span>red<span style="color: #339933;">,</span>green<span style="color: #339933;">,</span>blue<span style="color: #009900;">&#41;</span></span></code>, the values of those variables can range from 0 to 255.</p>
<p>I built this simple function that would give you a gradient between hue&#8217;s. That is a transition between all the colors of the rainbow, with the possibility to control the saturation of that gradient, and the number of items you&#8217;d like to output. This is easily customizable and I&#8217;m sure I&#8217;ll make a more advanced function out of it some day soon.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">function</span> rainbowGradient<span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span><span style="color: #000088;">$sat</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$top</span> &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$r</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$top</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$g</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$sat</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$step</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$top</span><span style="color: #339933;">-</span><span style="color: #000088;">$sat</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$sat</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> &nbsp;<span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$top</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">+</span><span style="color: #000088;">$step</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$r</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$g</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$b</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$top</span><span style="color: #339933;">;</span> <span style="color: #000088;">$r</span> &nbsp;<span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$sat</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;">$step</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$r</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$g</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$b</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$g</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$sat</span><span style="color: #339933;">;</span> <span style="color: #000088;">$g</span> &nbsp;<span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$top</span><span style="color: #339933;">;</span> <span style="color: #000088;">$g</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$g</span> <span style="color: #339933;">+</span><span style="color: #000088;">$step</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$r</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$g</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$b</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$top</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> &nbsp;<span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$sat</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">-</span><span style="color: #000088;">$step</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$r</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$g</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$b</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$sat</span><span style="color: #339933;">;</span> <span style="color: #000088;">$r</span> &nbsp;<span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$top</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;">$step</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$r</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$g</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$b</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$g</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$top</span><span style="color: #339933;">;</span> <span style="color: #000088;">$g</span> &nbsp;<span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$sat</span><span style="color: #339933;">;</span> <span style="color: #000088;">$g</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$g</span> <span style="color: #339933;">-</span><span style="color: #000088;">$step</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$colors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$r</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$g</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$b</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$colors</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$return</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;span style='padding:1px;background-color:rgb(<span style="color: #006699; font-weight: bold;">$color</span>)'&gt;&lt;/span&gt;&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$return</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Examples of this function at use are as follows:<br />
Calling <code class="codecolorer php dawn"><span class="php"><span style="color: #b1b100;">echo</span> rainbowGradient<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span></span></code> which means &#8220;return 40 elements with a saturation of 200&#8243; would return this:<br />
<img src="http://www.thismayhem.com/wp-content/uploads/2010/11/gradient-03.jpg" alt="" title="gradient-03" width="624" height="21" class="aligncenter size-full wp-image-400" /><br />
Calling <code class="codecolorer php dawn"><span class="php"><span style="color: #b1b100;">echo</span> rainbowGradient<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span></span></code> which means &#8220;return 40 elements with a saturation of 100&#8243; would return this:<br />
<img src="http://www.thismayhem.com/wp-content/uploads/2010/11/gradient-01.jpg" alt="" title="gradient-01" width="624" height="21" class="aligncenter size-full wp-image-395" /><br />
Calling <code class="codecolorer php dawn"><span class="php"><span style="color: #b1b100;">echo</span> rainbowGradient<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span></span></code> which means &#8220;return 40 elements with full saturation&#8221; would return this:<br />
<img src="http://www.thismayhem.com/wp-content/uploads/2010/11/gradient-02.jpg" alt="" title="gradient-02" width="624" height="21" class="aligncenter size-full wp-image-396" /></p>
<p>As you can see, you could modify the <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$return</span></span></code> variable so that the output is different, like doing it vertical by using divs instead of spans, show it on a grid by floating those elements inside a container, who knows. The possibilities are endless and while this only gives you a rainbow gradient, it&#8217;s easily extrapolated to more defined gradients without the use of images.</p>
<p>However, knowing that css3 promises to deliver gradient capabilities to the browser without the use of images, I&#8217;m not sure for how long, or if this will be useful for the designing part of your site. On the other hand, for web applications that need a color picker, this may come on handy. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/the-rainbow-gradient-experiment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Simple HTML DOM Parser</title>
		<link>http://www.thismayhem.com/php/php-simple-html-dom-parser/</link>
		<comments>http://www.thismayhem.com/php/php-simple-html-dom-parser/#comments</comments>
		<pubDate>Thu, 18 Nov 2010 23:11:22 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Tip of the day]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=297</guid>
		<description><![CDATA[It comes a time on any coders life where we need to parse the content of an html file, albeit you can do this with regular expressions or even using...]]></description>
			<content:encoded><![CDATA[<p>It comes a time on any coders life where we need to parse the content of an html file, albeit you can do this with regular expressions or even using <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">strpos</span></span></code> <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">substr</span></span></code> and a little <strong>creative thinking</strong>, those approaches are for sure not the best and most efficient.</p>
<p>Some time ago I came across this parser named <a href="http://simplehtmldom.sourceforge.net/">&#8220;PHP Simple HTML DOM Parser</a>&#8221; that, in a nutshell, lets you find elements from the source code of any page with ease. If you&#8217;re familiar with the jQuery selectors you will be happy to see lots of similarities in the way it works. Search by class, by id, by tag, you name it! It can even modify the html it&#8217;s parsing with ease. You want to change the content of your welcome message, located on a div element with &#8220;welcome&#8221; as an id?</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'div[id=welcome]'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">innertext</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'New welcome message'</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Easy enough for you?<br />
<del datetime="2010-11-18T23:12:58+00:00">It has a small documentation but it&#8217;s enough to get you started.</del> This is not true anymore, the documentation is quite extensive with lots of examples and guides on how get things done with ease.</p>
<p>For me, it has been of great help in multiple occasions, you fetch the html from another website and extract the content you want from it in just a few line, without worrying about invalid html, or small changes in the code that would break non bullet proof regular expressions. You have to keep in mind, that regular expression are not meant to be used to parse html, there are lots of answers regarding regular expression&#8217;s limits to parse html and xml. In essence, the main problem is that xml and html are not regular languages in the essence that we can&#8217;t fully predict patters for them for there are way too many factors to take into account.</p>
<p>I particularly found useful this question from stackoverflow titled:<br />
&#8220;<a href="http://stackoverflow.com/questions/701166">Can you provide some examples of why it is hard to parse XML and HTML with a regex?</a>&#8220;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/php-simple-html-dom-parser/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Redirect Traffic to least loaded Server</title>
		<link>http://www.thismayhem.com/php/redirect-traffic-to-least-loaded-server/</link>
		<comments>http://www.thismayhem.com/php/redirect-traffic-to-least-loaded-server/#comments</comments>
		<pubDate>Sat, 13 Nov 2010 09:36:39 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[advanced]]></category>
		<category><![CDATA[load]]></category>
		<category><![CDATA[loadbalance]]></category>
		<category><![CDATA[redirect]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[traffic]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=349</guid>
		<description><![CDATA[This article was inspired by a question I answered at StackOverflow where a user asked something similar to the following: Assuming I&#8217;ve got a multi-server website that contains a main...]]></description>
			<content:encoded><![CDATA[<p>This article was inspired by a question I answered at <a href="http://stackoverflow.com/questions/4170215/redirect-users-based-on-server-traffic-with-php/4170280#4170280" target="_blank">StackOverflow</a> where a user asked something similar to the following:</p>
<p><strong>Assuming I&#8217;ve got a multi-server website that contains a main server and five slave servers.<br />
How can I redirect the visitor to the slave server with the lowest memory usage at that particular time?</strong></p>
<p>The answer is quite clear. Get a way of numerically measuring the server&#8217;s memory load and send traffic to the least used one. But of course things are not always as easy as they might seem and now I had to find a function that measured the memory load. Luckily for us, php (linux only) counts with a function called  <code class="codecolorer php dawn"><span class="php">sys_getloadavg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> that does exactly that. It returns an array with three elements containing the average memory usage in the past minute, five minute, and 15 minute intervals.</p>
<p><a href="http://www.thismayhem.com/wp-content/uploads/2010/11/TEST.jpg"><img class="aligncenter size-full wp-image-354" title="layout" src="http://www.thismayhem.com/wp-content/uploads/2010/11/TEST.jpg" alt="" width="617" height="236" /></a></p>
<p>Once we know that, all there is left is to, every X seconds, store/update the numeric value representing the memory usage of that particular server in a database. The database&#8217;s table that we will be using should contain at least the following fields: a timestamp to be updated only if the time is newer than it was, an id to identify which server it&#8217;s the least loaded one, a percentage of use (0-100). We could also add other things like the ip address to which we want to redirect the user or so on.</p>
<p>Then, when the user accesses the main server, this server queries the common database for the lowest value of the table that&#8217;s storing those usage indexes and redirects the user the correct one.<br />
Easy, right?</p>
<p>The user who asked this in <strong>SO </strong>wanted to do this with php, that&#8217;s why I blindly looked for a solution in the specified language. It appears that there are specialized <em>loadbalancing </em>software and even hardware that do exactly that, redirect the user to the least loaded server. Anyway, it helped some of us finding a new function and with any luck it may be useful some time in the future.</p>
<p>Feel free to comment and let me know about some of those loadbalancing software/hardware solutions for, even though I actually wouldn&#8217;t be able to put them to good use, I&#8217;m sure some other reader may do so.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/redirect-traffic-to-least-loaded-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Discount per Item &#8211; Opencart</title>
		<link>http://www.thismayhem.com/php/opencart/discount-per-item-opencart/</link>
		<comments>http://www.thismayhem.com/php/opencart/discount-per-item-opencart/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 18:08:03 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Mods]]></category>
		<category><![CDATA[OpenCart]]></category>
		<category><![CDATA[coupon]]></category>
		<category><![CDATA[discount]]></category>
		<category><![CDATA[ecommerce]]></category>
		<category><![CDATA[fixed discount]]></category>
		<category><![CDATA[modification]]></category>
		<category><![CDATA[per item]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=325</guid>
		<description><![CDATA[Some time ago I wanted to add the functionality to the discounts system on opencart. I asked around on the forums and it seems like I didn&#8217;t make myself clear...]]></description>
			<content:encoded><![CDATA[<p>Some time ago I wanted to add the functionality to the discounts system on opencart. I asked around on the forums and it seems like I didn&#8217;t make myself clear enough because I was told that the functionality was already there, present in the two type of discounts available. Being discount per order, which doesn&#8217;t match, and percentage, which is almost ok if all the items affected have the same price and I don&#8217;t care about decimal discounts, which I did.</p>
<p>So I wrote a simple modification of the code, call it improvement.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">//In file catalog/model/total/coupon.php</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$coupon</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'F'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$discount</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$coupon</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'discount'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$product</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'total'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$coupon_total</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$coupon</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'P'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$discount</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$product</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'total'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">100</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$coupon</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'discount'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$coupon</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'R'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$discount</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$coupon</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'discount'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$product</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'quantity'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The last three lines, that being the last conditional of the coupon type value, is the course of action if the coupon type is &#8216;R&#8217;. For all we care, &#8216;R&#8217; stands for &#8216;per item&#8217;, why? Well, because I chose it when I wrote it, and now I can&#8217;t be bothered to change it. This is basically all you need to change in regards of php code. The only missing part is adding the <em>&#8220;Per Item</em>&#8221; option on the drop-down list of the coupon form located at: <em>admin/view/template/sale/coupon_form.tpl</em></p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;!-- file admin/view/template/sale/coupon_form.tpl --&gt;<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'R'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&lt;option value=&quot;R&quot; selected=&quot;selected&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$text_recursive</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/option&gt;<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&lt;option value=&quot;R&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$text_recursive</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/option&gt;<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>So that&#8217;s where the &#8216;R&#8217; come from!, recursive, for the discount is applied recursively to each item affected by the discount coupon. We don&#8217;t need to modify anything else. Just remember to declare the <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$test_recursive</span></span></code> variable, or you&#8217;ll get an error for an undefined variable. </p>
<p>Hope it was helpful, it works for 1.4.8++, it also works with the current version. (Maybe even earlier version, who knows)</p>
<p>After following those two simple steps, once you add/edit a coupon, you will have the option of selecting a &#8220;per item&#8221; mode, meaning that, as we wanted, the discount would be applied to each item affected by it, that&#8217;s in the cart. For example, if you had three cans of bear at 1$ each and the discount per item was 0.20, each can would increase the discount by 0.20, ending up in a grand total of 0.60 discount.</p>
<p>Granted this could also be done with a percentage discount of 20%, but if I say I want a discount of 30 cents on an item whose price is 0.99, how would I go about the percentage? This way (our recursive way) is faster and easier to understand. A lot easier than understanding my post which I complicated trying to simplify it. Lovely.</p>
<p>Thought, doubts and attacks, as comments please.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/opencart/discount-per-item-opencart/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Php function to translate using Google</title>
		<link>http://www.thismayhem.com/php/php-function-google-translate/</link>
		<comments>http://www.thismayhem.com/php/php-function-google-translate/#comments</comments>
		<pubDate>Mon, 08 Nov 2010 16:11:07 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[My Functions]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[translate]]></category>
		<category><![CDATA[translator]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=315</guid>
		<description><![CDATA[Just like a few months ago I shared with you a function made by &#8211; insert name &#8211; that essentially took an amount, and two currencies and using Google&#8217;s currency...]]></description>
			<content:encoded><![CDATA[<p>Just like a few months ago I shared with you a function made by &#8211; insert name &#8211; that essentially took an amount, and two currencies and using Google&#8217;s currency exchange shortcut (search for &#8220;100 EUR to GBP&#8221; for example) returned the result of converting from one currency to the other one.</p>
<p>Today, I wrote a simple PHP function that, using the same principle of abusing what Google gives us, translates a given string to a target language.<br />
This is particularly useful for multi-lingual sites, not so much for the content of the page, but for short messages and button texts that are generic and that will just consume time being translated one by one. All you need to know when you call the function is, what string you want to translate, and the locales of the languages. At the end of this post there is a list of the locales used by Google translator.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">function</span> googleTranslate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span><span style="color: #000088;">$from</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'en'</span><span style="color: #339933;">,</span><span style="color: #000088;">$to</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'es'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://translate.google.com/translate_a/t?client=t&amp;text=<span style="color: #006699; font-weight: bold;">$string</span>&amp;hl=en&amp;sl=<span style="color: #006699; font-weight: bold;">$from</span>&amp;tl=<span style="color: #006699; font-weight: bold;">$to</span>&amp;multires=1&amp;oc=0&amp;prev=btn&amp;sc=1&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$askGoogle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$askGoogle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$askGoogle</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$askGoogle</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <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;">$askGoogle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$askGoogle</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Admittedly, this is not the fastest way, and will make your page load slower. But combining it with another function that will store those translations in your database has great potential. Think of it as a Google translate on demand on certain parts of your page. To end this, let me show you a working example:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #b1b100;">echo</span> googleTranslate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;es&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Would return:<br />
&#8220;<em>Hola a todos</em>&#8221; which literally means &#8220;<em>Hello everyone</em>&#8221; which is accurate enough.</p>
<p>Last but not least, here&#8217;s the locale list as promised:</p>
<table>
<tr>
<td>
af :  Afrikaans<br />
sq :  Albanian<br />
ar :  Arabic<br />
be :  Belarusian<br />
bg :  Bulgarian<br />
ca :  Catalan<br />
zh-CN :  Chinese<br />
hr :  Croatian<br />
cs :  Czech<br />
da :  Danish
</td>
<td>
nl :  Dutch<br />
en :  English<br />
et :  Estonian<br />
tl :  Filipino<br />
fi :  Finnish<br />
fr :  French<br />
gl :  Galician<br />
de :  German<br />
el :  Greek<br />
ht :  Haitian Creole
</td>
<td>
iw :  Hebrew<br />
hi :  Hindi<br />
hu :  Hungarian<br />
is :  Icelandic<br />
id :  Indonesian<br />
ga :  Irish<br />
it :  Italian<br />
ja :  Japanese<br />
ko :  Korean<br />
lv :  Latvian
</td>
<td>
lt :  Lithuanian<br />
mk :  Macedonian<br />
ms :  Malay<br />
mt :  Maltese<br />
no :  Norwegian<br />
fa :  Persian<br />
pl :  Polish<br />
pt :  Portuguese<br />
ro :  Romanian<br />
ru :  Russian
</td>
<td>
sr :  Serbian<br />
sk :  Slovak<br />
sl :  Slovenian<br />
es :  Spanish<br />
sw :  Swahili<br />
sv :  Swedish<br />
th :  Thai<br />
tr :  Turkish<br />
uk :  Ukrainian<br />
vi :  Vietnamese<br />
cy :  Welsh<br />
yi :  Yiddish
</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/php-function-google-translate/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Leading Zeros</title>
		<link>http://www.thismayhem.com/php/leading-zeros/</link>
		<comments>http://www.thismayhem.com/php/leading-zeros/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 13:42:19 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tip of the day]]></category>
		<category><![CDATA[format numbers]]></category>
		<category><![CDATA[leading zeros]]></category>
		<category><![CDATA[tip-of-the-day]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=296</guid>
		<description><![CDATA[If you&#8217;re familiar with the sprintf function, you may already know this or perhaps, just like for me, it will be a novelty. I was looking for a fast way...]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re familiar with the <a href="http://php.net/manual/en/function.sprintf.php">sprintf </a> function, you may already know this or perhaps, just like for me, it will be a novelty.<br />
I was looking for a fast way of adding leading zeros to a number determined on a loop, for example, I want to call 40 images dynamically and while I know that they follow this format for example <code class="codecolorer text dawn"><span class="text">image_XXX</span></code> where the x&#8217;s are replaced by a number leaded by zeros if it&#8217;s smaller than 3 digits I had no easy way of adding those zeros.</p>
<p>I just dumbly assumed that the only way was checking the string length, and if it equalled to 1 add two leading zeros, if it equalled to 2 add just one and if it was 3 or bigger, don&#8217;t add any. That&#8217;s fast enough, and it can be done like this:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'1'</span><span style="color: #339933;">:</span> <span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;00&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$number</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'2'</span><span style="color: #339933;">:</span> <span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$number</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span> <span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$number</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>But that&#8217;s taking up 5 lines of code just for that small formatting, and imagine the implications of the same example but on a 10 digit number. So I found you can do the following with the same result and in just one line of code:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%03d</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Enjoy!</p>
<p><strong>Update:</strong></p>
<p>It was used for answering <a href="http://stackoverflow.com/questions/4171873/this-php-string-joining-is-driving-me-crazy/4171938" target="_blank">this question</a> on the 13th of Novemeber.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/leading-zeros/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Build from the bottom-up</title>
		<link>http://www.thismayhem.com/html/build-from-the-bottom-up/</link>
		<comments>http://www.thismayhem.com/html/build-from-the-bottom-up/#comments</comments>
		<pubDate>Wed, 23 Jun 2010 23:14:45 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Compatibility]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=287</guid>
		<description><![CDATA[This is more of a suggestion than a programming related article. The fact is that a lot of developers and developer&#8217;s code I&#8217;ve come across in the past few years...]]></description>
			<content:encoded><![CDATA[<p>This is more of a suggestion than a programming related article. </p>
<p>The fact is that a lot of developers and developer&#8217;s code I&#8217;ve come across in the past few years seem to be aimed at a high end idyllic and flawless user who will have a certain configuration, resolutions, browser and plugins available and will experience therefore the same as the developer intended. This is however usually not the case and creating a website that works for some users and not for others is <strong>wrong </strong>. I could say it&#8217;s counterproductive and that it will harm your image as a developer/designer/programmer/professional but it&#8217;s easier to just say it&#8217;s wrong.</p>
<p>The truth is that if your site becomes somewhat popular you will start to receive visitors from Internet Explorer, Firefox, Opera, Safari and some other browsers with totally different configurations that include non-flash-enabled users and non-javascript-enable users, users that don&#8217;t accept cookies, or those who don&#8217;t parse css. My point is that your site should remain browsable and work under all those different scenarios and to do that you just have to keep some simple rules in mind.</p>
<p>What I mean by this is you should design your site for the worst possible scenario and then add the good stuff keeping focused on the fact that it should still work in the worst case scenario. Lets say you want to use one of the multiple lightbox scripts available to show the full sized view of an image. You could set the action with javascript making javascript-less users unable to see the full sized image or you could link the image to the full size and then modify it with javascript.</p>
<p>All javascript calls should be done over a non-javascript code, meaning that if you want to send a form using AJAX you should first make the form work without it and then extend it&#8217;s functionality with javascript. This way it will still work for both js and js-less users.<br />
It&#8217;s true that on 2008 the amount of users that had javascript enabled was over 95% of the total but that leaves a whole 5% of your users seeing some ugly version of your site and probably never coming back.</p>
<p>If you have a small website that has a couple of visitors per day that&#8217;s probably just not worth doing since you will only lose a few visitors per month but for bigger sites that get thousands of visitors per day the loss could be huge.</p>
<p>Thanks for reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/html/build-from-the-bottom-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Currency Exchange with PHP</title>
		<link>http://www.thismayhem.com/php/currency-exchange-with-php/</link>
		<comments>http://www.thismayhem.com/php/currency-exchange-with-php/#comments</comments>
		<pubDate>Sun, 20 Jun 2010 01:30:06 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tip of the day]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[currency exchange]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[tip of the day]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=277</guid>
		<description><![CDATA[I found out this little neat function today while searching for some currency converter or should I say currency rate grabber. What this function does is it performs a google...]]></description>
			<content:encoded><![CDATA[<p>I found out this little neat function today while searching for some currency converter or should I say currency rate grabber. What this function does is it performs a google search and since google&#8217;s calculator handles currency conversions pretty well and up to date in the rates it grabs the information from it with the use of a regular expression. To be honest I would&#8217;ve never though of this but when you think about it it&#8217;s more than obvious. Google provides a service that we are using when we could be abusing it!</p>
<p>Here&#8217;s the code, Enjoy!!</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">function</span> exchangeRate<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$amount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$currency</span><span style="color: #339933;">,</span> <span style="color: #000088;">$exchangeIn</span> <span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$googleQuery</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$amount</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$currency</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' in '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$exchangeIn</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$googleQuery</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urlEncode</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$googleQuery</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$askGoogle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://.google.com/search?q='</span><span style="color: #339933;">.</span><span style="color: #000088;">$googleQuery</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$askGoogle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$askGoogle</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$matches</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; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/Rates provided for information only - see disclaimer./i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$askGoogle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$matches</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; &nbsp; <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'/= (([0-9]|.|,| )*)/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$askGoogle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> ? <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$status</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'google says no'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$status</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #b1b100;">echo</span> exchangeRate<span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'euro'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'dollars'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p><a href="http://blog.motane.lu/2008/12/19/currency-conversion-in-php/#comment-12">Source</a></p>
<p>Aside from the currency converter this gave me the idea of a word translator using google and php which can be found <a target="_blank" href="http://www.thismayhem.com/php/php-function-google-translate/">here</a>, or perhaps definition of words with google define. The possibilities are cute impressive when you put your mind to it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/currency-exchange-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comparing images with PHP + GD</title>
		<link>http://www.thismayhem.com/php/comparing-images-with-php-gd/</link>
		<comments>http://www.thismayhem.com/php/comparing-images-with-php-gd/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 16:20:51 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Code Explained]]></category>
		<category><![CDATA[Images]]></category>
		<category><![CDATA[My Functions]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[dissimilarity]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=210</guid>
		<description><![CDATA[Some time ago I, for some reason, stumbled upon a website that made the visitor choose the image that was duplicated as some sort of human recognition. I&#8217;ve always liked...]]></description>
			<content:encoded><![CDATA[<p>Some time ago I, for some reason, stumbled upon a website that made the visitor choose the image that was duplicated as some sort of human recognition. I&#8217;ve always liked those types of captchas that are not your usual type the text in the image, but this particular one seemed breakable for me. I though, &#8220;<em>Hey, I can do this!</em>&#8221; and in fact I did.</p>
<p>I wrote a function that took two image url&#8217;s as it&#8217;s attributes and returned if they matched or not. In fact it returns a factor I named &#8220;<strong>Dissimilarity Index</strong>&#8221; since the higher the number, the less similar those images are.<br />
For those who are still into basics of PHP this may be a little bit too much, but it&#8217;s worth reading if you&#8217;re interested.</p>
<p>First I&#8217;ll show you the function, the I will dissect it as usual, line by line, avoiding the most basic stuff that hopefully you already know.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<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: #000000; font-weight: bold;">function</span> dissimilarityIndexCalculator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_img_url</span><span style="color: #339933;">,</span><span style="color: #000088;">$str_match_url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">//Try to make images from the urls, on fail return false.</span><br />
<span style="color: #000088;">$img_source</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">ImageCreateFromString</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_img_url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$img_match</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">ImageCreateFromString</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_match_url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$img_source</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000088;">$img_match</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//Get image sizes.</span><br />
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_img_source_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$int_img_source_height</span><span style="color: #009900;">&#41;</span> &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_img_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_img_match_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$int_img_match_height</span><span style="color: #009900;">&#41;</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_match_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//Resample to 16px each</span><br />
<span style="color: #000088;">$img_16source</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$img_16match</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$img_16source</span><span style="color: #339933;">,</span><br />
<span style="color: #000088;">$img_source</span><span style="color: #339933;">,</span><br />
<span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span><br />
<span style="color: #000088;">$int_img_source_width</span><span style="color: #339933;">,</span><br />
<span style="color: #000088;">$int_img_source_width</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$img_16match</span><span style="color: #339933;">,</span><br />
<span style="color: #000088;">$img_match</span><span style="color: #339933;">,</span><br />
<span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span><br />
<span style="color: #000088;">$int_img_match_width</span><span style="color: #339933;">,</span><br />
<span style="color: #000088;">$int_img_match_width</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$difference</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$y</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">;</span><span style="color: #000088;">$y</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//Get the color of the resulting image</span><br />
<span style="color: #000088;">$arr_img_source_color</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span><br />
<span style="color: #990000;">imagecolorsforindex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_16source</span><span style="color: #339933;">,</span><span style="color: #990000;">imagecolorat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_16source</span><span style="color: #339933;">,</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$arr_img_match_color</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#93;</span> &nbsp;<span style="color: #339933;">=</span><br />
<span style="color: #990000;">imagecolorsforindex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_16match</span><span style="color: #339933;">,</span><span style="color: #990000;">imagecolorat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_16match</span><span style="color: #339933;">,</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//Calculate the index</span><br />
<span style="color: #000088;">$difference</span> &nbsp;<span style="color: #339933;">+=</span> <span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr_img_source_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'red'</span><span style="color: #009900;">&#93;</span> &nbsp; <span style="color: #339933;">-</span> <span style="color: #000088;">$arr_img_match_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'red'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span><br />
<span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr_img_source_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'green'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$arr_img_match_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'green'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><br />
<span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr_img_source_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'blue'</span><span style="color: #009900;">&#93;</span> &nbsp;<span style="color: #339933;">-</span> <span style="color: #000088;">$arr_img_match_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'blue'</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><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$difference</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$difference</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">256</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//Return an array with the information</span><br />
<span style="color: #000088;">$arr_return</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;dissimilarityIndex&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$difference</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;sourceImage&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;url&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$str_img_source</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;width&quot;</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$int_img_source_width</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;height&quot;</span>&nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$int_img_source_height</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;colors&quot;</span> &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$arr_img_source_color</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;matchImage&quot;</span> &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> &nbsp; &nbsp;<span style="color: #0000ff;">&quot;url&quot;</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$str_img_match</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;width&quot;</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$int_img_match_width</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;height&quot;</span>&nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$int_img_match_height</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;colors&quot;</span> &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$arr_img_match_color</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$arr_return</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The function takes two attributes, the first one is the url of the first image from the comparison, named <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$str_img_url</span></span></code> and the second one, which is in fact the url of the second image, called <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$str_match_url</span></span></code>. An example call to this function would be <code class="codecolorer php dawn"><span class="php">dissimilarityIndexCalculator<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;imageOne.jpg&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;imageTwo.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> and will return the following information inside an array:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;dissimilarityIndex&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;An index reflecting the combined color differences&quot;</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;sourceImage&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;url&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;First Image Url&quot;</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;width&quot;</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;First Image Width&quot;</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;height&quot;</span>&nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;First Image Height&quot;</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;colors&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;RGB values for each pixel in the 16x16 matrix&quot;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;matchImage&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;url&quot;</span> &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Second Image Url&quot;</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;width&quot;</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Second Image Width&quot;</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;height&quot;</span>&nbsp; &nbsp; &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Second Image Height&quot;</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">&quot;colors&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;RGB values for each pixel in the 16x16 matrix&quot;</span><br />
<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>There&#8217;s a lot going on in this function but in a nutshell what I&#8217;ve done is assured the images existed, if they do, Copy and resize the image with re-sampling to 16 pixels. These pixels with any luck contains the average colour of that image and by calculating the sum of the subtraction of the three colour indexes of each pair of pixels from the first image and the ones from the second image we get a number from 0 to 765 (255+255+255) that will represent the dissimilarity.</p>
<p>Lets get started dissecting the code:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">$img_source</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">ImageCreateFromString</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_img_url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$img_match</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">ImageCreateFromString</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_match_url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$img_source</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000088;">$img_match</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>On these lines we are creating two image objects from the urls declared as the function attributes. the <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">file_get_contents</span></span></code> function when targeted to an image, will return a string that contains the data of the image. I chose this approach instead of building the image directly from the url since it appears that this is less resource hungry for the server. Then using <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">ImageCreateFromString</span></span></code> targeting the newly created string, it will translate the string back into an image object. The <code class="codecolorer text dawn"><span class="text">@</span></code> once again will hide any error messages from the function.</p>
<p>The third line is a conditional that in the even that any of the images failed to create will return false, ceasing the execution of the function. If there are no images, we can&#8217;t compare them can we?.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp;<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_img_source_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$int_img_source_height</span><span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_img_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_img_match_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$int_img_match_height</span><span style="color: #009900;">&#41;</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_match_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>It&#8217;s self explanatory, the function <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">getimagesize</span></span></code> returns an array of information from which the first two elements contain the height and width of the image. By using <code class="codecolorer text dawn"><span class="text">list</span></code> we can declare more than one variable at one time.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">$img_16source</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$img_16match</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>We create the image objects (empty for now) that will contain the resized copy of the original ones.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">imagecopyresampled</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$img_16source</span><span style="color: #339933;">,</span><br />
<span style="color: #000088;">$img_source</span><span style="color: #339933;">,</span><br />
<span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span><br />
<span style="color: #000088;">$int_img_source_width</span><span style="color: #339933;">,</span><br />
<span style="color: #000088;">$int_img_source_width</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$img_16match</span><span style="color: #339933;">,</span><br />
<span style="color: #000088;">$img_match</span><span style="color: #339933;">,</span><br />
<span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span><br />
<span style="color: #000088;">$int_img_match_width</span><span style="color: #339933;">,</span><br />
<span style="color: #000088;">$int_img_match_width</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This is where the magic happens, what we are saying is, &#8220;<em>Resize the image from the original width/heigh to 16px, and store that copy on</em> <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$img_16source</span></span></code>&#8220;. So we now have a 16 pixel image representing the average colour for each of the images we want to compare. All we need to do is a simple calculation and we will have the index, but before that we need to get the colour from those pixel. We do so using the <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">imagecolorsforindex</span></span></code> function inside two nested loops.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #000088;">$difference</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$y</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">;</span><span style="color: #000088;">$y</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//Get the color of the resulting image</span><br />
<span style="color: #000088;">$arr_img_source_color</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span><br />
<span style="color: #990000;">imagecolorsforindex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_16source</span><span style="color: #339933;">,</span><span style="color: #990000;">imagecolorat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_16source</span><span style="color: #339933;">,</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$arr_img_match_color</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#93;</span> &nbsp;<span style="color: #339933;">=</span><br />
<span style="color: #990000;">imagecolorsforindex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_6match</span><span style="color: #339933;">,</span><span style="color: #990000;">imagecolorat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_16match</span><span style="color: #339933;">,</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//Calculate the index</span><br />
<span style="color: #000088;">$difference</span> &nbsp;<span style="color: #339933;">+=</span> <span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr_img_source_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'red'</span><span style="color: #009900;">&#93;</span> &nbsp; <span style="color: #339933;">-</span> <span style="color: #000088;">$arr_img_match_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'red'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> &nbsp; <span style="color: #339933;">+</span><br />
<span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr_img_source_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'green'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$arr_img_match_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'green'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><br />
<span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr_img_source_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'blue'</span><span style="color: #009900;">&#93;</span> &nbsp;<span style="color: #339933;">-</span> <span style="color: #000088;">$arr_img_match_color</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'blue'</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><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$difference</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$difference</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">256</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Last, we calculate the dissimilarity index by subtracting the red, green, and blue integer values of the source image with the one from the match image. For those who don&#8217;t know what RGB is, it stands for Red Green and Blue. Those values will be 0 for the lack of that particular colour and 255 for the maximum value of that colour. So by adding the absolute value of those subtraction, we have the index!!</p>
<p>Lets put it to the test by comparing a few images, the first image will be compared against the two other:
<a href='http://www.thismayhem.com/php/comparing-images-with-php-gd/attachment/imagessource/' title='imagessource'><img width="150" height="150" src="http://www.thismayhem.com/wp-content/uploads/2010/06/imagessource.jpg" class="attachment-thumbnail" alt="imagessource" title="imagessource" /></a>
<a href='http://www.thismayhem.com/php/comparing-images-with-php-gd/attachment/imagematch1/' title='imagematch1'><img width="150" height="150" src="http://www.thismayhem.com/wp-content/uploads/2010/06/imagematch1.jpg" class="attachment-thumbnail" alt="imagematch1" title="imagematch1" /></a>
<a href='http://www.thismayhem.com/php/comparing-images-with-php-gd/attachment/asd/' title='asd'><img width="150" height="150" src="http://www.thismayhem.com/wp-content/uploads/2010/06/asd.jpg" class="attachment-thumbnail" alt="asd" title="asd" /></a>
</p>
<p>When comparing the first image against the first match, we got a result of <code class="codecolorer php dawn"><span class="php"><span style="color: #009900;">&#91;</span>dissimilarityIndex<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">38.640625</span></span></code>.<br />
When comparing the first image against the second match, we got a result of <code class="codecolorer php dawn"><span class="php"><span style="color: #009900;">&#91;</span>dissimilarityIndex<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">57.22265625</span></span></code>.</p>
<p>Note that for comparing images with really similar colours, it may give inaccurate results.  This was thought more like a way of finding a match between different images. Anyway it performs pretty well under most circumstances and will surely do the work. If you read this post right when it was written (not like anyone reads my blog) you might have seen an old version of this function that used a resize to 1px instead of 16px. The result for this improved version is a lot more accurate and even with slight modifications to a source image it finds the closest match.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/comparing-images-with-php-gd/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Extracting a date from a string with PHP</title>
		<link>http://www.thismayhem.com/php/extracting-a-date-from-a-string-with-php/</link>
		<comments>http://www.thismayhem.com/php/extracting-a-date-from-a-string-with-php/#comments</comments>
		<pubDate>Thu, 17 Jun 2010 00:43:01 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[My Functions]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[manipulation]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[strtodate]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=200</guid>
		<description><![CDATA[Hopefully this will be the first of many posts in this category. I was answering a question in Stackoverflow today about extracting dates from twitted messages and after all the...]]></description>
			<content:encoded><![CDATA[<p>Hopefully this will be the first of many posts in this category. I was answering a question in <strong>Stackoverflow</strong> today about extracting dates from twitted messages and after all the debate that went on about what the best procedure was I decided to write my own function that will do exactly that, extract the most complex date from a string. By most complex I mean that it will prioritize something like &#8220;<em>May 2nd 2008</em>&#8221; over &#8220;<em>May 2nd</em>&#8220;. The function, named <code class="codecolorer php dawn"><span class="php">extractDateFromString</span></code> (why make it less intuitive)  returns an array containing the part of the string it decided was the best date, a UNIX datestamp of that date and a formatted date using either a user provided format or the predefined one.</p>
<p>It has some flaws, like the way I say that if the string length of the string that returns a valid datestamp is longer than a previously matching string, it should replace the old one. This could lead to <em>saturday </em>replacing <em>sunday </em>for example, and none of those is more valid that the other. This problem could be solver by storing all of those in an array and showing them in order. But it&#8217;s out of context as far as I&#8217;m concerned, it&#8217;s entirely up to you to do it.</p>
<p>Keep in mind that since <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">strtotime</span></span></code> only parses English strings, we can&#8217;t take advantage of it for other languages. This means, that you will only be able to use this to parse English messages in search of valid dates.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">function</span> extractDateFromString<span style="color: #009900;">&#40;</span><span style="color: #000088;">$datestring</span><span style="color: #339933;">,</span><span style="color: #000088;">$format</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;F j, Y&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$datestring</span> &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$datestring</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$arr_datestring</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$datestring</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$b</span><span style="color: #339933;">&lt;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr_datestring</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr_datestring</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$arr_datestring</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_splice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tmp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_splice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tmp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #339933;">,-</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tmp</span><span style="color: #009900;">&#41;</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><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;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tmp</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'No dates found'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'string'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$result</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'unix'</span> &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">'date'</span> &nbsp; &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$format</span><span style="color: #339933;">,</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&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><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>Examples of the output given by the function to some strings follow:<br />
<strong>Input</strong>: &#8220;I&#8217;m going to play croquet next Friday&#8221;<br />
<strong>Output</strong>: <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">Array</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#91;</span>string<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;next friday&quot;</span> <span style="color: #009900;">&#91;</span>unix<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1276844400</span> <span style="color: #009900;">&#91;</span><span style="color: #990000;">date</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;June 18, 2010&quot;</span> <span style="color: #009900;">&#41;</span></span></code></p>
<p><strong>Input</strong>: &#8220;Gadzooks, is it 17th June already?&#8221;<br />
<strong>Output</strong>: <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">Array</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#91;</span>string<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;17th june&quot;</span> <span style="color: #009900;">&#91;</span>unix<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1276758000</span> <span style="color: #009900;">&#91;</span><span style="color: #990000;">date</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;June 17, 2010&quot;</span> <span style="color: #009900;">&#41;</span></span></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/extracting-a-date-from-a-string-with-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adding watermarks to images with PHP</title>
		<link>http://www.thismayhem.com/php/watermarking-images-part1/</link>
		<comments>http://www.thismayhem.com/php/watermarking-images-part1/#comments</comments>
		<pubDate>Sun, 13 Jun 2010 10:34:20 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Images]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[watermark]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=155</guid>
		<description><![CDATA[After the brief introduction to creating images with GD and PHP done on Text to Image with PHP we will be creating a function that given two attributes will return...]]></description>
			<content:encoded><![CDATA[<p>After the brief introduction to creating images with GD and PHP done on <a href="http://www.thismayhem.com/php/text-to-image-with-php/">Text to Image with PHP</a> we will be creating a function that given two attributes will return the first image with the second one as a watermark. For those of you who don&#8217;t know what a watermark is, it&#8217;s a mark on an image that either states the author of it, or prevents a sample image from being used instead of purchasing it etc. Usually is some text repeated all across the main image, it can also be another image instead of text.</p>
<p>On this introduction post we will create a function called <code class="codecolorer php dawn"><span class="php">watermarkIt</span></code> that will take one argument, in this case the source image and return the same image. This may sound stupid but it&#8217;s going to be quite interesting since we will be checking multiple things like integrity of the, file type and valid file name:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> watermarkIt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_src_img</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$img_source</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">ImageCreateFromString</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_src_img</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_source</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Broken or invalid image&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>The first thing we&#8217;ve done is try accessing the image at the route specified at <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$str_source_img</span></span></code>, and if there is no image or it&#8217;s corrupt/invalid we will return an error message. By using <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">ImageCreateFromString</span></span></code> and <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">file_get_contents</span></span></code> combined to get the source image we don&#8217;t have to worry about the file type as long as it&#8217;s valid. Since we will be using this function to return an image on the fly, we don&#8217;t want text error messages so let&#8217;s deal with it:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> watermarkIt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_src_img</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: image/jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$str_error_message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;The image is corrupt&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$img_source</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">ImageCreateFromString</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_src_img</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_source</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$img_error</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$color_black</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_error</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #990000;">imagestring</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_error</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str_error_message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color_black</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #990000;">imagejpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>Well, that&#8217;s a step forward isn&#8217;t it? Right now, we check if the file we wanted to use as a source image is valid and if not, we create a 200px by 30px image containing the value of the <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$str_error_message</span></span></code> variable. That way if we call the function to an invalid image, it will still return an image. This of course can be changed to anything you want, a blank image or a pre-set error image. In the meantime I want to explain some lines of the code. I will assume you have some basic php knowledge so I&#8217;ll avoid the most obvious ones.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: image/jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This sets the content type of the script file to a jpg image. Meaning that we can call it like we did in the introduction to images with php like this <code class="codecolorer html4strict dawn"><span class="html4strict"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'http://yoursite.com/route_to_script.php'</span> <span style="color: #66cc66;">/</span>&gt;</span></span></code> and it will show the image generated by it.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">$img_source</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">ImageCreateFromString</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_src_img</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>There&#8217;s a lot going on in this line! We are creating an image object called <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$img_source</span></span></code> that will contain the image specified when we called the function. The <code class="codecolorer text dawn"><span class="text">@</span></code> will hide errors. The <code class="codecolorer text dawn"><span class="text">ImageCreateFromString</span></code> function will create and image identifier from the image stream created, forgive the repetition, by the <code class="codecolorer text dawn"><span class="text">file_get_contents</span></code>.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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;">$img_error</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$color_black</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_error</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">imagestring</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_error</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str_error_message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color_black</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Those three lines are only used <strong>if</strong> the image we provided is invalid, so we need to create a new one, and we called it <code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$img_error</span></span></code>. The <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">imagecreatetruecolor</span></span></code> function will create an image identifier just like the <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">ImageCreateFromString</span></span></code> function did before. The moment we create it, it has no colours associated to it, and has a size of 200px by 30px since those are the values we declared when we called it. The next line allocates a black colour to the <code class="codecolorer text dawn"><span class="text">$img_error</span></code> image, meaning that it will be available for use. The attributes it takes are, image identifier, red, blue and green. <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">imagestring</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_error</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str_error_message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color_black</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> The <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">imagestring</span></span></code> funtion will add a string of text to the image specified at the first attribute, with a font specified at the second attribute, located at 10 pixels from the left side and 7 pixels from the top. More information on this function <a target='_blank' href="http://www.php.net/manual/en/function.imagestring.php">here</a>.</p>
<p>The last two lines create and destroy the image, meaning we will output the image on the first one and once the output is completed, we will destroy it.<br />
Why would be destroy it? Because creating and modifying images with PHP takes memory of course and this function frees any memory associated with it.</p>
<p>We wanted this function to return the same image if everything else was correct so lets do it!:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> watermarkIt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_src_img</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: image/jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$str_error_message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;The image is corrupt&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$img_source</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">ImageCreateFromString</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_src_img</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_source</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$img_error</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$color_black</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_error</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #990000;">imagestring</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_error</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str_error_message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color_black</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #990000;">imagejpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="display:block;background-color:#ffff66">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span><br /></span><span style="display:block;background-color:#ffff66">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$arr_src_img_data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_source_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /></span><span style="display:block;background-color:#ffff66">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$int_img_type_start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr_src_img_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mime'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br /></span><span style="display:block;background-color:#ffff66">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$str_src_img_type</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;image/&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$arr_src_img_data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mime'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /></span><span style="display:block;background-color:#ffff66">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$img_output</span> <span style="color: #339933;">=</span> <span style="color: #990000;">call_user_func</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;imagecreatefrom<span style="color: #006699; font-weight: bold;">$str_src_img_type</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str_src_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br /></span>&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #990000;">imagejpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_output</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_output</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p><code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$arr_source_img_data</span></span></code> is an array created by the <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">getimagesize</span></span></code> function that contains the file type, the image size, and some other interesting values. For us, we&#8217;ll be using the &#8216;mime&#8217; element of that array which contains the file type. Unfortunately, the format of the string contains both the file type and the extension. Since we just want the extension for now we use the <code class="codecolorer php dawn"><span class="php"><span style="color: #990000;">str_replace</span></span></code> function to remove the unnecessary parts.</p>
<p><code class="codecolorer php dawn"><span class="php"><span style="color: #000088;">$img_output</span> <span style="color: #339933;">=</span> <span style="color: #990000;">call_user_func</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;imagecreatefrom<span style="color: #006699; font-weight: bold;">$str_src_img_type</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str_src_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> This line prevents us from having to use conditionals for creating the image object depending on the file extension. Instead, we use the [cci lang='php']call_user_func[/php] function to dynamically call the appropriate function to create the image. We do this because it&#8217;s not the same to create an image from a jpg a gif or a png, they use different functions.</p>
<p>On the next part, we will start getting into the actual watermarking. I apologize for anyone who was disappointed by this, but we need to set a strong base for our code to be bulletproof.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/watermarking-images-part1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validation with PHP – Part 3 &#8211; Explaining the class</title>
		<link>http://www.thismayhem.com/php/validation-with-php-%e2%80%93-part-3-explaining-the-class/</link>
		<comments>http://www.thismayhem.com/php/validation-with-php-%e2%80%93-part-3-explaining-the-class/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 22:07:40 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[alphanumeric]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=128</guid>
		<description><![CDATA[1234&#60;?php class validate&#123; &#160; public function fieldValidate&#40;$field,$str_validate_rules&#41;&#123; &#160; $this-&#62;int_errors = 0; On these three lines we declare a new class called validate. It has a public function called fieldValidate that...]]></description>
			<content:encoded><![CDATA[<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">class</span> validate<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fieldValidate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #339933;">,</span><span style="color: #000088;">$str_validate_rules</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">int_errors</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>On these three lines we declare a new <a target='_blank' href="http://en.wikipedia.org/wiki/Class_%28computer_science%29">class</a> called <strong>validate</strong>.  It has a public function called <strong>fieldValidate </strong>that takes two attributes. The first one is the variable that contains the field data we want to validate and the second one is a string containing the rules we want to use for the validation. This function is in charge of calling the appropriate private functions that will do the actual validation. We provide a string, and this function extracts and processes the rules.</p>
<p><strong>$this->int_errors</strong> is a variable that will contain the number of errors after the validation process is done. In future posts we will add custom error messages, for now, it&#8217;s just a binary situation. It either validates or it doesn&#8217;t, we don&#8217;t know which rule failed.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_validate_rules</span><span style="color: #339933;">,</span><span style="color: #0000ff;">','</span><span style="color: #009900;">&#41;</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><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000088;">$arr_rules</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$str_validate_rules</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000088;">$arr_rules</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span><span style="color: #000088;">$str_validate_rules</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>These lines ensure that even if we provide only one rule, it will be stored on an array so the next part of the code works. It may be doable in a more efficient way, but mind that I&#8217;m coding this on the fly just to help you understand this. If there is more than one rule, they will be separated by commas, so what we do is store them in an array by using the <a target="_blank" href="http://www.php.net/manual/en/function.explode.php">explode </a>function.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr_rules</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$str_rule</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;required&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">int_errors</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;numeric&quot;</span><span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkIsNumeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">int_errors</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;alpha&quot;</span><span style="color: #009900;">&#41;</span> &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkIsAlpha</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> &nbsp; &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">int_errors</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;length&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$int_min</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;to&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$int_max</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span><span style="color: #339933;">,</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;to&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkLength</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_max</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_min</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">int_errors</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>This part of the code is very important. It&#8217;s a loop that will decide which functions to call and for each particular function which attributes to use. The format I&#8217;m using for setting multiple validation rules as the second attribute when calling the <strong>fieldValidate </strong>function is the following:</p>
<ul>
<li>&#8220;validationRule&#8221; For rules with no arguments</li>
<li>&#8220;validationRule{argument}&#8221; For rules with one argument</li>
<li>&#8220;validationRule{argument_one<strong>to</strong>argument_two}&#8221; For rules with two arguments</li>
</ul>
<p>Once again, multiple validation rules will be separated by commas. The first three rules, (required, numeric and alpha) require no arguments and therefore can be declared on a single line. Those read something like this: &#8220;<em>If the value of variable <strong>$str_rule</strong> is equal to this keyword then if it doesn&#8217;t pass the rule linked to this keyword increase the value of<strong> $this->int_errors</strong> by one.</em>&#8221; On the other hand, the &#8220;length check requires two attributes so we need to extract them before we can call the <strong>checkLength </strong>function. To do so, we use the function <a target="_blank" href="http://www.php.net/manual/en/function.substr.php">substr</a> to locate the keyword &#8216;to&#8217; that works as a separator between those two arguments. If the function doesn&#8217;t return true, we increase the value of the error count on <strong>$int_errors</strong>.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <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;">int_errors</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>This basically means &#8220;<em>If there the error count stored in <strong>$int_errors</strong> is higher than zero, return false!</em>&#8221; otherwise, return true.<br />
The following code consists in all the private functions that are available for the public <strong>fieldValidate </strong>function to call. Each of those private functions will return true or false depending on whether the data is correct or not. Those are self-explanatory but if you have any doubts feel free to comment asking about it.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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">&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkEmpty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</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; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkLength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_max</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">99</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_min</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$int_min</span> <span style="color: #339933;">||</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$int_max</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> &nbsp; <br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkNumericValue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_field</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_max</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_min</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_field</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$int_min</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$int_field</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$int_max</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkIsNumeric<span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkIsAlpha<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^[a-zA-Z ]{1,}$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$validate</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> validate<span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$validate</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fieldValidate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;required,length{2to6},alpha&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;valid field&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$validate</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">int_errors</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">/*<br />
The output of this will be &quot;Valid field - 0&quot; since &quot;test&quot; doesn't break any rules. <br />
$int_errors is still 0 since we didn't break the rules!<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>Now, using this class we can validate a single field against multiple rules. On the next part of this series we will go on and introduce an error output system that will allow us to locate the point where the validation failed and show us a message explaining what happened. Then on further parts we will introduce the ability to choose the language of the messages and move those to a separate file that will something like a language file.</p>
<p>Stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/validation-with-php-%e2%80%93-part-3-explaining-the-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validation with PHP – Part 2 &#8211; The Core</title>
		<link>http://www.thismayhem.com/php/validation-with-php-%e2%80%93-part-2-the-core/</link>
		<comments>http://www.thismayhem.com/php/validation-with-php-%e2%80%93-part-2-the-core/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 15:07:03 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[preg_match]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=109</guid>
		<description><![CDATA[The previous post &#8220;Validation with PHP – Introduction&#8221; merely scratched the surface of what can be done in regards of validating data with PHP. In this part I will be...]]></description>
			<content:encoded><![CDATA[<p>The previous post &#8220;<a href="http://www.thismayhem.com/php/validation-with-php-introduction/">Validation with PHP – Introduction</a>&#8221; merely scratched the surface of what can be done in regards of validating data with PHP. In this part I will be creating a basic validation class that will include a more customizable version of what we seen in the previous post and extend it with another function.</p>
<p>Going back to our first example, we had a form that contained a text field in which the user had to specify his/her year of birth. The rules we used for validating that data were two: make sure that the data is numeric with the <a target='_blank' href="http://www.php.net/manual/en/function.is-numeric.php">is_numeric()</a> function and that it was between 1950 and 1985 with simple operators. </p>
<p>Today, our form has two new fields. One that asks for the users favourite colour and another one that asks for a telephone number without spaces or any other character. To validate those three fields we could create two other functions so we end up with one function that validates each particular case or we can elaborate the function we built on the previous post so that it can validate both the year of birth and the telephone number. As for the favourite colour, we will write another one.</p>
<p>Let me show you what I mean by elaborate the actual function:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">function</span> validateNumeric<span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_field</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_max</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_min</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <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;">$int_min</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$int_field</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$int_min</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <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;">$int_max</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$int_field</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$int_max</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$int_year</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #cc66cc;">1980</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$int_phone</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">622671864</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>validateNumeric<span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_year</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1985</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1950</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;The year is valid&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>validateNumeric<span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_phone</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;The phone is valid&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>The extended function allows you to specify a minimum and/or a maximum number as a validation rule. Note that when validating the <strong>$int_year</strong> variable we specify the maximum year as the first argument and the minimum year as the second argument but when validating the phone number we just ask the function to verify if it&#8217;s a number or not. I like to put the maximum value as the second argument, right before the minimum because I find it more often situations where you don&#8217;t really care about the minimum but you do about the maximum, this is strictly something personal so feel free to change the order if you believe it&#8217;s better for your particular case.</p>
<p>This is ok if we just had one type of validation rules, in this case numeric values within a given range but, as I mentioned at the beginning of the post, we also want to know the users favourite colour. Since validating a colour is a completely different matter and there are hundreds of different colours out there, all we are going to do is make sure that whatever the user enters contains only letters and maybe white-spaces for the possibility of something like &#8220;pale green&#8221; as an input. Once again we will be assuming that we already have the users input in variables called &#8220;<strong>$type_field</strong>&#8220;.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">function</span> validateAlphaStr<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$int_max</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span> <span style="color: #000088;">$int_min</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000088;">$regex_alpha</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/^[a-zA-Z ]{&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$int_min</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$int_max</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;}$/&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$regex_alpha</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str_field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arr_matches</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$str_field</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;red&quot;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>validateAlphaStr<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;The colour is valid&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>Lets explain the code a little bit. We created another function called <strong>validateAlphaStr</strong> that takes three arguments. The first one is the data we want to validate, the second and third ones are the minimum and maximum number of characters for that data. Those two arguments are not mandatory and have default values just like on the previous function. That means that if we don&#8217;t specify them it will use those default values instead.</p>
<p>The validation is done through a regular expression using the <strong>preg_match</strong> function. In common words the regular expression reads:<br />
&#8220;<em>This will match if the only thing on <strong>$str_field</strong> is a string made with lowercase and uppercase letters and white-spaces with a length ranging between <strong>$int_min</strong> and <strong>$int_max</strong>, all matches will be stored on an array called <strong>$arr_matches</strong></em>&#8220;.</p>
<p>This means that if we try to validate &#8220;<strong>pink</strong>&#8221; it will work but if we try to validate &#8220;<strong>colour name longer than twentyfive characters&#8221;</strong> or &#8220;<strong>red 2</strong>&#8221; it will fail. Note that we could make the very long colour name succeed if we specify the maximum length to a higher number.</p>
<p>Last thing we need to do is to place those functions inside a class so they can share private functions for common validations like length and duplicates.<br />
And this is where the magic happens, we will be transitioning from individual calls to a validation function to multiple calls from a validation class, lets do this!<br />
That means that in one single call we will be able to validate against multiple rules:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<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: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">class</span> validate<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startValidate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #339933;">,</span><span style="color: #000088;">$str_validate_rules</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">int_errors</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_validate_rules</span><span style="color: #339933;">,</span><span style="color: #0000ff;">','</span><span style="color: #009900;">&#41;</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><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000088;">$arr_rules</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$str_validate_rules</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000088;">$arr_rules</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span><span style="color: #000088;">$str_validate_rules</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr_rules</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$str_rule</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;required&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> &nbsp; &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">int_errors</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;numeric&quot;</span><span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkIsNumeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">int_errors</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;alpha&quot;</span><span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkIsAlpha</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">int_errors</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;length&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$int_min</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;to&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$int_max</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span><span style="color: #339933;">,</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_rule</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;to&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkLength</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_max</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_min</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">int_errors</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; <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;">error_count</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span> <br />
<br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkEmpty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</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; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkLength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_max</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">99</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_min</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$int_min</span> <span style="color: #339933;">||</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$int_max</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> &nbsp; <br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkNumericValue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_field</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_max</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_min</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_field</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$int_min</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$int_field</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$int_max</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkIsNumeric<span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkIsAlpha<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^[a-zA-Z ]{1,}$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$validate</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> validate<span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$validate</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">startValidate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;required,length{2to6},alpha&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;valid field&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$validate</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">int_errors</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">/*<br />
The output of this will be &quot;Valid field - 0&quot; since &quot;test&quot; doesn't break any rules. <br />
$int_errors is still 0 since we didn't break the rules!<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>Don&#8217;t worry, I will explain this line by line on the next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/validation-with-php-%e2%80%93-part-2-the-core/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validation with PHP &#8211; Introduction</title>
		<link>http://www.thismayhem.com/php/validation-with-php-introduction/</link>
		<comments>http://www.thismayhem.com/php/validation-with-php-introduction/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 00:28:26 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[introduction]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=102</guid>
		<description><![CDATA[Validation doesn&#8217;t necessarily mean just form validation. We can also validate any variable, in fact any data against a set of rules. Whether the data passes or not those rules...]]></description>
			<content:encoded><![CDATA[<p>Validation doesn&#8217;t necessarily mean just form validation. We can also validate any variable, in fact any data against a set of rules. Whether the data passes or not those rules is what makes it valid or invalid. I will be making a series of posts explaining specific validation rules and extending those with multiple examples.<br />
In the end, we will have a full featured form validation system with error messages, customization and a lot more features.</p>
<p>Today I will show you a very basic introduction to validation.<br />
Lets say I have a form with only one field which asks the user his/her year of birth. To validate the user input we will run a function that will return <em>TRUE </em>if it&#8217;s valid and <em>FALSE </em>if it&#8217;s not valid. That will be it. We will be assuming that we already have the user input stored in a variable named <strong>$int_year</strong>.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">function</span> validateYear<span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_year</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_year</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$int_year</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1987</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>validateYear<span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_year</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;It's valid&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>This will validate as correct and therefore output the success message since 1987 is a number.<br />
Okay, I admit it. This example is overly simplified and does not need a custom function since <strong>is_numeric</strong> already provides the functionality we were after but you grasp the concept, so lets make it a little bit harder. Lets imagine that we only want to consider valid inputs, those who are between 1950 and 1985. The previous example will look like this:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">function</span> validateYear<span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_year</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_year</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_year</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1950</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$int_year</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1985</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$int_year</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1987</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>validateYear<span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_year</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;It's valid&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>This example will not validate as correct and therefore will not output the success message because, even though 1987 is a number (first condition), 1987 is not between 1950 and 1985 (second condition). If we changed the value of <strong>$int_year</strong> to 1980 for example, it will be perfectly valid.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/validation-with-php-introduction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Text to Image with PHP</title>
		<link>http://www.thismayhem.com/php/text-to-image-with-php/</link>
		<comments>http://www.thismayhem.com/php/text-to-image-with-php/#comments</comments>
		<pubDate>Sun, 06 Jun 2010 12:43:59 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Code Explained]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[processing]]></category>

		<guid isPermaLink="false">http://www.thismayhem.com/?p=76</guid>
		<description><![CDATA[If you have at least some experience with PHP you probably know that there are several image engines available for you to work with. Today, I&#8217;ll be showing you a...]]></description>
			<content:encoded><![CDATA[<p>If you have at least some experience with PHP you probably know that there are several image engines available for you to work with.<br />
Today, I&#8217;ll be showing you a simple way of generating an image from a user provided string using the GD library. The goal of this code will be to generate a simple image with white background and black text centred inside of it.</p>
<p>I will be using my naming rules as seen at &#8220;<a href="http://www.thismayhem.com/php/naming-conventions-and-practices-in-php/">Naming conventions and practices in PHP</a>&#8220;.As time goes by I will be extending this code to provide additional functionality, but for now, lets get started:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> textToImage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str_text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Default text'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></td></tr></tbody></table></div>
<p>We open PHP tags and declare a new function called <strong>textToImage</strong>. This function takes one argument called <strong>$str_text</strong>, note that I prepended <strong>str_</strong> to the variable name, so we know it should be a string. If no argument is provided when we call the function it will use it&#8217;s default value, in this case &#8220;<em>Default text</em>&#8220;.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #990000;">header</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: image/png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$int_image_width</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$int_image_height</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$obj_image</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int_image_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$int_image_height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Inside the function, we first specify that the document type is a png image. Keep in mind that you will not be able to write this function on a file and include that file into your code. This means that you will have a file specifically reserved for the <strong>textToImage </strong>function and will call it from the other files. I will get into that later. Afterwards, we specify the height and width of the image and create a new image with the <strong><a target="_blank" href="http://www.php.net/manual/en/function.imagecreate.php">imagecreate </a></strong>function.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #000088;">$color_black</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj_image</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$color_white</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj_image</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>That right, you can&#8217;t use colors out-of-the-box with the gd library, you first need to declare a variable that will contain a colour identifier for the image object using the <a href="http://www.php.net/manual/en/function.imagecolorallocate.php">imagecolorallocate</a> function and then use it when needed.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #000088;">$int_font_id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$int_x</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$int_y</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #990000;">imagestring</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj_image</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_font_id</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_x</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_y</span><span style="color: #339933;">,</span><span style="color: #000088;">$str_text</span><span style="color: #339933;">,</span><span style="color: #000088;">$color_white</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The variable <strong>$int_font_id</strong> contains a font identifier from 1 to 5 (can be higher if you declared more fonts using the loadfont function), $int_x and $int_y are both coordinates of the upper left corner of the text, relative to the container image. This means that if the values of both variables are 0, the text will start at the top-left corner of the image.<br />
The <a target='_blank' href="http://www.php.net/manual/en/function.imagestring.php">imagestring </a>function, places the value of <strong>$str_text</strong> on the <strong>$obj_image</strong> image at the given X-Y coordinates and with the specified font.</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj_image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj_image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Those two lines output the image and destroy the image object to free memory after it&#8217;s generated.<br />
Last but not least you want to call the function to see it working, do that like this:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">textToImage<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Test string&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>If you want to specify the text variable from another file, you can change &#8220;Test string&#8221; to &#8220;$_GET['str_text']&#8221; and wherever you want to use that image, just do the following:</p>
<div class="codecolorer-container html4strict dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'http://site.com/route/texToString.php?str_text=StringHere'</span> <span style="color: #66cc66;">/</span>&gt;</span></div></td></tr></tbody></table></div>
<p>Thus generating an image on the fly with the use of the simple php function we created together.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/text-to-image-with-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Naming conventions and practices in PHP</title>
		<link>http://www.thismayhem.com/php/naming-conventions-and-practices-in-php/</link>
		<comments>http://www.thismayhem.com/php/naming-conventions-and-practices-in-php/#comments</comments>
		<pubDate>Sun, 06 Jun 2010 02:02:14 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tip of the day]]></category>
		<category><![CDATA[naming]]></category>
		<category><![CDATA[php-tips]]></category>
		<category><![CDATA[suggestions]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://thismayhem.com/wp/?p=54</guid>
		<description><![CDATA[It&#8217;s inevitable that while programming we tend to use short names for variables, functions, classes etc and while this may seem harmless in the short run, it will dramatically slow...]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s inevitable that while programming we tend to use short names for variables, functions, classes etc and while this may seem harmless in the short run, it will dramatically slow down your work if you have to expand or modify that code afterwards. You will find variables that have non-descriptive names and end up spending your time trying to understand who they are and what they do.</p>
<p>A solution for this is establishing a set of rules for when you name variables and any other custom defined code. For example, let&#8217;s say I wan to define a function that will return me a random number between 1 and 10.  Here is the code I would do if I was just messing around and didn&#8217;t care about naming conventions and rules:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">function</span> random<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #b1b100;">echo</span> random<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>Yes, the code works, and it&#8217;s fast and simple but keep in mind that this is an overly simplified example. Lets say I want to extend that function so that it returns a randomly chosen element of a given array. Following the same style as the previous example, the code would look something like this:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">function</span> random<span style="color: #009900;">&#40;</span><span style="color: #000088;">$att</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$att</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$att</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: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'element1'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'element2'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'element3'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> random<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>Once again, it works, of course it does. And you&#8217;ve written it so you have no problem understanding it. But if you, or someone else had to come along at some point and try to understand it they will surely lose some time, and this is just a small and very basic function, just imagine what could happen on a big scale project.</p>
<p>The rules I use for programming in PHP are very basic and I&#8217;m pretty sure that there are lots of other styles out there. You just need to find the one that suits you and you feel more comfortable working with. Keep in mind that at first it may slow you down, but in the long run you will be thankful. There are just a few of them:</p>
<ul>
<li>Use <em>camelCase </em>for functions and classes: This means that the first word on the name will be all under-case and the following words will start with a capital. Note that I use no underscores here.</li>
<li>Use <em>underscores </em>for variables and variables only: This includes arrays too of course. Another little trick I use is to specify the data type of the variable if possible before the actual name. For example a variable that will contain a random number will be named $int_random. That way I can easily see that it&#8217;s a number, and it&#8217;s random! (This doesn&#8217;t lock down the data type of the variable at all, it&#8217;s merely descriptive).</li>
<li>Don&#8217;t re-use variable names if possible.</li>
</ul>
<p>My approach to this function using my own rules would be the following:</p>
<div class="codecolorer-container php dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:430px;"><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 /></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: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> randomElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$int_array_top</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$int_random</span> &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$int_array_top</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$element</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$int_random</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$element</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'element1'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'element2'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'element3'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> randomElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>Now, anyone who reads the code can see at first glance that the function returns a random element from a given array. But not only that. They know that the random element is selected by a random number between zero and the number of elements in the given array minus one (Array elements start at 0).<br />
It may look uglier than the previous example, but it&#8217;s cleaner and easier to understand and maintain.</p>
<p>For anyone concerned about performance issues, I believe there is virtually no difference whatsoever between the optimized example and the one preceding it. This makes your code understandable, maintainable, and readable &#8211; in the long run, that far outweighs any slight performance benefit.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/php/naming-conventions-and-practices-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Notepad++</title>
		<link>http://www.thismayhem.com/general/notepad/</link>
		<comments>http://www.thismayhem.com/general/notepad/#comments</comments>
		<pubDate>Sat, 05 Jun 2010 22:19:15 +0000</pubDate>
		<dc:creator>Juan</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[notepad++]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[trade]]></category>

		<guid isPermaLink="false">http://thismayhem.com/wp/?p=38</guid>
		<description><![CDATA[Notepad++ is a must have for any programmer that wants a fast and efficient coding environment that will not be resource hungry and that provides lots of functionality. Personally I...]]></description>
			<content:encoded><![CDATA[<p>Notepad++ is a must have for any programmer that wants a fast and efficient coding environment that will not be resource hungry and that provides lots of functionality. Personally I think that it&#8217;s aimed at non-compiled programming languages and particularly to web and web-based application development. I use it mostly when I work on html, php, xml, javascript and asp but it has syntax highlighting for lots of other languages aswell.</p>
<p>In essence, Np++ is a text editor on steroids. It has all the features that you will want, from code highlighting and multiple-selection of text to quick-texts and macros. Add to the list an incredible versatility in means of customization of the way the code you&#8217;re viewing is displayed, allowing you to change the colour of backgrounds and text as well as the font, size and style of these. Not to forget it&#8217;s<strong> free</strong>.</p>
<p>I&#8217;m not going to say that it&#8217;s the best solution for your particular case since there are countless options available at the time and some of them are incredible. For example, while coding in Php I&#8217;m sometimes drawn to Phped, for it&#8217;s code error highlighter. It&#8217;s good to be able to locate syntax errors without having to actually run the script on the server (we all hate those missing semi-colon or unexpected braces errors).</p>
<p>Most of the functionality is available out-of-the-box but for the ones that aren&#8217;t there are plenty of plugins at your disposal.</p>
<p>One of the functions I like the most or at least one of the most useful in my particular case, is the ability to compare different files using multiple views.  The following image shows how notepad looks with two separate views of a two documents while comparing them. Notice how each line that doesn&#8217;t match is highlighted and within that particular line, each dissimilarity is also highlighted. There is also a navigation bar which locates the differences for ease of access. Lines that don&#8217;t match are marked with an icon that will represent a general difference, an extra line, or a missing line. Those icons are easily recognizable and will make your experience even easier.</p>
<div id="attachment_43" class="wp-caption aligncenter" style="width: 288px"><a href="http://thismayhem.com/wp-content/uploads/2010/06/compare-notepad.gif"><img class="size-medium wp-image-43" title="compare-notepad" src="http://thismayhem.com/wp-content/uploads/2010/06/compare-notepad-278x300.gif" alt="" width="278" height="300" /></a><p class="wp-caption-text">Screenshot of the comparison tool of Np++</p></div>
<p>Here is the link to their site and a direct link to the currently latest download:<br />
- <a href="http://notepad-plus.sourceforge.net/uk/site.htm" target="_blank">Site</a><br />
- <a href="http://downloads.sourceforge.net/project/notepad-plus/notepad%2B%2B%20releases%20binary/npp%205.6.8%20bin/npp.5.6.8.Installer.exe?use_mirror=switch" target="_blank">Download</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thismayhem.com/general/notepad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

