<?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>Codigo Manso &#187; manso trick</title>
	<atom:link href="http://www.codigomanso.com/en/tag/truco-manso/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codigomanso.com</link>
	<description>Programación, informática y tecnología</description>
	<lastBuildDate>Sun, 21 Aug 2011 10:54:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Manso Hack: Speedup Google App Engine SDK SQLite Database</title>
		<link>http://www.codigomanso.com/en/2011/08/manso-trick-speedup-sqlite-database-in-google-app-engine-sd/</link>
		<comments>http://www.codigomanso.com/en/2011/08/manso-trick-speedup-sqlite-database-in-google-app-engine-sd/#comments</comments>
		<pubDate>Sun, 21 Aug 2011 10:54:29 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Programacion]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[truco manso]]></category>
		<category><![CDATA[gae]]></category>
		<category><![CDATA[hack manso]]></category>
		<category><![CDATA[mansohack]]></category>
		<category><![CDATA[mansotrick]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=1065</guid>
		<description><![CDATA[I was trying to initialize a local database using the Google App Engine SDK, and I was going crazy.
&#8211;use_sqlite parameter was not even solving my problem. Inserts on the database were really slow, like 10 per second. A nightmare.
OK, ok, it is an SDK, it emulates the server&#8230; it is intended to be used to [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to initialize a local database using the Google App Engine SDK, and I was going crazy.</p>
<p>&#8211;use_sqlite parameter was not even solving my problem. Inserts on the database were really slow, like 10 per second. A nightmare.</p>
<p>OK, ok, it is an SDK, it emulates the server&#8230; it is intended to be used to develop your application, speed is not the most important thing&#8230; I agree. But if you have to wait 10 minutes to insert 1000 data entries, then you can desperate. If they were like 1 million in 10 minutes, then I would understand, it would be still slow, but I will understand, but 1000 inserts? 10 minutes? are we all crazy?</p>
<p>Anyway, assuming SQLite seems to be a good database and works usually great, I though I could get that speed improved. Looking at the SQLite FAQ I found following question:</p>
<p><a href="http://www.sqlite.org/faq.html#q19" target="_blank"><strong>(19) INSERT is really slow &#8211; I can only do few dozen INSERTs per second</strong></a></p>
<p>Basically it seems SQLite should be fine doing 50,000 inserts per second in a normal computer, BUT to guarantee the data integrity it locks inserts until it makes sure they have been written to disk, so if your computer hangs your data remains safe.</p>
<p>These are good news because I don&#8217;t care of my local data in the local develoment environment, so in the event of a crash I can just rebuild it in a minute, instead of rebuild it in an hour.</p>
<p>Finally the solution is to use &#8220;<a href="http://www.sqlite.org/pragma.html#pragma_synchronous" target="_blank">PRAGMA synchronous=OFF</a>&#8220;, and the only way of using this pragma is to edit a line in de Google App Engine SDK, after initializing SQLite database</p>
<p>So you&#8217;ll have to edit following file:</p>
<p>google_appengine/google/appengine/datastore/datastore_sqlite_stub.py</p>
<p>Look for the &#8220;__init__&#8221; constructor and after the connection to the database gets initialized &#8220;self.__connection = sqlite3.connect [...]&#8221; you can place following statment:</p>
<p>self.__connection.execute(&#8216;PRAGMA synchronous=OFF&#8217;)</p>
<p>Changing that line I went from about 10 inserts/second to more than 100 inserts/second. More than a 10x gain for changing a single line. Great!</p>
<p>Now I can continue on the thing I was working on without getting crazy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2011/08/manso-trick-speedup-sqlite-database-in-google-app-engine-sd/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Manso Trick: Easy way to generate random hex string in Python</title>
		<link>http://www.codigomanso.com/en/2011/07/truco-manso-obtener-una-cadena-hexadecimal-aleatoria-en-python/</link>
		<comments>http://www.codigomanso.com/en/2011/07/truco-manso-obtener-una-cadena-hexadecimal-aleatoria-en-python/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 07:22:53 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Programacion]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[truco manso]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=1058</guid>
		<description><![CDATA[Generate a random string in hexadecimal is one of those things that you always have to do, from time to time.
As far as I know, the easiest way to do this in python is to import uuid module and run the following code:
uuid.uuid4().hex
With the call above we have 32 hexadecimal characters string (16 random bytes).
Do [...]]]></description>
			<content:encoded><![CDATA[<p>Generate a random string in hexadecimal is one of those things that you always have to do, from time to time.<br />
As far as I know, the easiest way to do this in python is to import uuid module and run the following code:<br />
<code>uuid.uuid4().hex</code></p>
<p>With the call above we have 32 hexadecimal characters string (16 random bytes).</p>
<p>Do you know any other approach?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2011/07/truco-manso-obtener-una-cadena-hexadecimal-aleatoria-en-python/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Manso Trick: Getting the latitude/longitude from an address</title>
		<link>http://www.codigomanso.com/en/2010/11/truco-manso-obtener-la-latitudlongitud-a-partir-de-una-localizacion-o-direccion/</link>
		<comments>http://www.codigomanso.com/en/2010/11/truco-manso-obtener-la-latitudlongitud-a-partir-de-una-localizacion-o-direccion/#comments</comments>
		<pubDate>Sat, 13 Nov 2010 11:27:32 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Programacion]]></category>
		<category><![CDATA[truco manso]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[geocoding]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[latitude]]></category>
		<category><![CDATA[longitude]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=1038</guid>
		<description><![CDATA[For a project soon to be born (I hope), I&#8217;ve been doing a quick research for a way to obtain the latitude and longitude from a geographical address. For example, to determine the latitude/longitude from a totally random address like &#8220;Emerson Street, Palo Alto, California&#8221;  BTW, Hi Mariano! Thanks for the stay in USA last [...]]]></description>
			<content:encoded><![CDATA[<p>For a project soon to be born (I hope), I&#8217;ve been doing a quick research for a way to obtain the latitude and longitude from a geographical address. For example, to determine the latitude/longitude from a totally random address like &#8220;Emerson Street, Palo Alto, California&#8221;  BTW, Hi Mariano! Thanks for the stay in USA last year <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>This is called geocoding and after spending half a second looking searching on internet I&#8217;ve found two simple REST APIs. Yahoo! and Google provide their own APIs and I&#8217;m sure a lot of companies out there make use of it, because of their accuracy.</p>
<p>If you want to use Yahoo! API you need to sign up so they give you some sort of appid and you can start making queries.</p>
<p>In the other hand, Google let&#8217;s you do this as easily as:</p>
<p><a href="http://maps.googleapis.com/maps/api/geocode/json?address=Emerson+Street,+Palo+Alto,+california&amp;sensor=false">http://maps.googleapis.com/maps/api/geocode/json?address=Emerson+Street,+Palo+Alto,+california&amp;sensor=false</a></p>
<p>This URL returns a JSON response which can be easily parsed in all languages (you can also ask for the XML, but I sort of hate XML so I prefer JSON).</p>
<p>Apart from returning the latitude and longitude, it also returns the formatted address, which can be great to normalize the address in your database, in case you need such thing. For example, for the query before, it says the formatted address is:</p>
<p>&#8220;Emerson St, Palo Alto, CA, USA&#8221;</p>
<p>On top of that, you should always read the Terms Of Service to make sure your app stays aligned with the fair use of the API, but that&#8217;s up to you.</p>
<p>Although more inacurate, another interesting project is GeoNames (see the URL below).</p>
<p>For more information:</p>
<ul>
<li>Google Geocoding API:<br />
<a href="http://code.google.com/apis/maps/documentation/geocoding/index.html">http://code.google.com/apis/maps/documentation/geocoding/index.html</a></li>
<li>Yahoo! Place Finder:<br />
<a href="http://developer.yahoo.com/geo/placefinder/">http://developer.yahoo.com/geo/placefinder/</a></li>
<li>GeoNames geographical database:<br />
<a href="http://www.geonames.org/">http://www.geonames.org/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2010/11/truco-manso-obtener-la-latitudlongitud-a-partir-de-una-localizacion-o-direccion/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Manso Trick: Pad a number with leading zeroes in javascript</title>
		<link>http://www.codigomanso.com/en/2010/07/simple-javascript-formatting-zero-padding/</link>
		<comments>http://www.codigomanso.com/en/2010/07/simple-javascript-formatting-zero-padding/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 17:08:33 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[padding]]></category>
		<category><![CDATA[truco manso]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=907</guid>
		<description><![CDATA[I was missing a simple and elegant method for padding a number with leading zeroes in javascript.
A typical example of leading zeroes is when you want to show the current time and you want the time to be formatted like hh:mm. There is no problem when it&#8217;s 12:40, but when is five minutes past one, [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I was missing a simple and elegant method for padding a number with leading zeroes in javascript.</p>
<p style="text-align: justify;">A typical example of leading zeroes is when you want to show the current time and you want the time to be formatted like hh:mm. There is no problem when it&#8217;s 12:40, but when is five minutes past one, you get 1:5, which is not what you would expect. You want it formatted like 01:05. The same happens with dates and in several other situations.</p>
<p style="text-align: justify;">Yesterday I found the best solution I&#8217;ve seen for this problem. Before that, let&#8217;s see possible solutions.</p>
<p style="text-align: justify;">Usually, needing only one zero, I would do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">h <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;0&quot;</span> <span style="color: #339933;">+</span> h<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> h<span style="color: #339933;">;</span></pre></div></div>

<p style="text-align: justify;">This is not bad at all, but is not elegant. The problem is, what if we want it to be formatted with 3 digits? Then, using the same schema, we can end up with something like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">h <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;0&quot;</span> <span style="color: #339933;">+</span> h<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;00&quot;</span> <span style="color: #339933;">+</span> h<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> h<span style="color: #339933;">;</span></pre></div></div>

<p style="text-align: justify;">Man, this is ugly and unreadable!! Maybe it will improve with an if/else, but it will be bad anyway. Probably if you need to do something like that, you will create a function that will handle this problem and then, you will call that function saying you want h padded with 3 zeros. But there is another solution that does not require you to use or create such a funciton&#8230;</p>
<p style="text-align: justify;">Have a look for two digit and three digit:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;0&quot;</span> <span style="color: #339933;">+</span> h<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// devolverá &quot;01&quot; si h=1; &quot;12&quot; si h=12</span>
<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;00&quot;</span> <span style="color: #339933;">+</span> h<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;001&quot; si h=1; &quot;012&quot; si h=12;&quot;123&quot; si h = 123</span></pre></div></div>

<p style="text-align: justify;">I don&#8217;t know what are you thinking after seeing this solution, but I got flashed when I saw it. Just amazing!</p>
<p style="text-align: justify;">Let&#8217;s explain what&#8217;s going on. The code above only concatenates the &#8220;00&#8243; with the number we want to format (e.g: &#8220;00&#8243; + 12 = &#8220;0012&#8243;) and then, we get the last three digits using the <b>slice</b> string function (that&#8217;s why you have a -3). Guess what? The last three characters are &#8220;012&#8243; which is the string we want.</p>
<p style="text-align: justify;">Of course this solution is not only limited to leading zeroes, you can use it with leading spaces, or whatever character you want.</p>
<p>Sources:</p>
<ul>
<li><a href="http://gugod.org/2007/09/padding-zero-in-javascript.html">Padding zero in Javascript</a></li>
<li><a href="http://dev.enekoalonso.com/2010/07/20/little-tricks-string-padding-in-javascript/">Little tricks: string padding in Javascript</a> of Eneko Alonso blog (originally seen here)</li>
</ul>
<p><b>Update:</b> <a href="http://milan.adamovsky.com/">Milan Adamovsky</a> has created a web page <a href="http://jsperf.com/zero-padding">http://jsperf.com/zero-padding</a> where you can check the performance issues of several functions to pad with zeroes, and you can try it directly in your browser <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2010/07/simple-javascript-formatting-zero-padding/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Manso Trick: Detect the Operating System in PHP</title>
		<link>http://www.codigomanso.com/en/2010/04/truco-manso-detectar-el-sistema-operativo-en-php/</link>
		<comments>http://www.codigomanso.com/en/2010/04/truco-manso-detectar-el-sistema-operativo-en-php/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 09:36:39 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programacion]]></category>
		<category><![CDATA[truco manso]]></category>
		<category><![CDATA[sistema operativo]]></category>
		<category><![CDATA[so]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=779</guid>
		<description><![CDATA[There could be a lot of reasons to detect the OS in PHP, and I&#8217;m pretty aware that these reasons could open a discussion by themselves. Anyway, it could be useful to do some tweaks or optimizations, to run one commands or anothers, or simply  it can be useful as additional information for webmasters, developers [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">There could be a lot of reasons to detect the OS in PHP, and I&#8217;m pretty aware that these reasons could open a discussion by themselves. Anyway, it could be useful to do some tweaks or optimizations, to run one commands or anothers, or simply  it can be useful as additional information for webmasters, developers or operations department.</p>
<p style="text-align: justify;">Anyway, we are all grown ups, so you know what you are doing. Now, imagine you need to know this. There are several ways (the more creative you are, the more ways you will find). The first way, and most simple and complete, is by using the <a href=" http://php.net/manual/en/function.php-uname.php" target="_blank"><em>php_uname</em></a> function. The php_uname is similar to the unix command uname, and with this function we can know the  operating system, the version of the OS, the release number, the host name, and the hardware architecture  (i386, amd64, &#8230;). It has all that you need <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p style="text-align: justify;">On the other hand, if you don&#8217;t want to use this pretty command, you can directly use the PHP_OS constant, which only includes the operating system name, but in most cases will be enough (it is equivalent to call <em><strong>php_uname(&#8217;s&#8217;)</strong></em>).</p>
<p style="text-align: justify;">Now you can try these two examples:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">php_uname</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">PHP_OS</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p style="text-align: justify;">The funny thing is that probably you would never need to know the OS, so you can skip the whole post <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2010/04/truco-manso-detectar-el-sistema-operativo-en-php/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Manso Trick: Updating the order of a row in a table</title>
		<link>http://www.codigomanso.com/en/2009/01/update-order-of-a-row-keeping-the-rest-untouched/</link>
		<comments>http://www.codigomanso.com/en/2009/01/update-order-of-a-row-keeping-the-rest-untouched/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 10:55:24 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programacion]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[fila]]></category>
		<category><![CDATA[orden]]></category>
		<category><![CDATA[tabla]]></category>
		<category><![CDATA[truco manso]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=506</guid>
		<description><![CDATA[This time, the article is not about javascript or PHP, but SQL. The idea of this article is to review different methods for updating the order of a row in a table (trying to touch only the row we want to change, keeping intact the rest of rows).
To start with a simple example, let&#8217;s say [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">This time, the article is not about javascript or PHP, but SQL. The idea of this article is to review different methods for updating the order of a row in a table (trying to touch only the row we want to change, keeping intact the rest of rows).</p>
<p style="text-align: justify;">To start with a simple example, let&#8217;s say I&#8217;m doing a web site related to the motor industry, and as any other good website, I use a database. In that database there are two tables: <strong>brand</strong> and <strong>model</strong>. In the table <strong>brand</strong> there is a row for each car brand (Toyota, Honda, Hyunday, Mercedes, Chrysler, BMW, Audi, Lexus,&#8230;), and in the table <strong>model</strong> there is one row for each car model. For example, some models for Audi will be A6 2.0 TDIe , A5 2.0 TDI, A4, TT &#8230;, and the list grows and grows for each brand.</p>
<p style="text-align: justify;">The think is, that in the front-end of my website I want to list the models of a brand in a specific order (the order I defined).</p>
<p style="text-align: justify;">You will think, well, that&#8217;s easy, it&#8217;s just adding a field called <strong>sort_order</strong> in the <strong>model</strong> table, or if you love to have the tables normalized, then you will create an extra table with two foreign keys to <strong>model</strong> and <strong>brand</strong>, and then a field called <strong>sort_order</strong>.</p>
<p style="text-align: justify;">It does not matter which solution you choose, the final concept is that there is a field name <b>sort_order</b> that is set manually on the backend, which defines the order used to list models in the frontend. Let&#8217;s see an example of these tables:</p>
<table style="float: left; width: auto;">
<tr>
<th>Brand</td>
</tr>
<tr>
<td>name</td>
</tr>
<tr>
<td>description</td>
</tr>
<tr>
<td>image</td>
</tr>
<tr>
<td>&#8230;</td>
</tr>
</table>
<table style="float: left; margin-left: 2em;  width: auto;">
<tr>
<th>model</td>
</tr>
<tr>
<td>name</td>
</tr>
<tr>
<td>description</td>
</tr>
<tr>
<td>image</td>
</tr>
<tr>
<td>&#8230;</td>
</tr>
<tr>
<td><b>sort_order</b></td>
</tr>
<tr>
<td>&#8230;</td>
</tr>
</table>
<div style="clear:both;"></div>
<p style="text-align: justify;">This was just an informal introduction, let&#8217;s get started!</p>
<p style="text-align: justify;">In order to simplify things even more, imagine we are always talking about Audi (just to name one), and imagine we are working only with 4 models in the database: A3, A4, A5 y TT. From now on, we will forget about brands. So at the end, our table <b>model</b> looks like this:</p>
<table style="width: auto;">
<tr>
<th>name</th>
<th>description</th>
<th>&#8230;</th>
<th><b>sort_order</b></th>
<th>&#8230;</th>
</tr>
<tr>
<td>A3</td>
<td>Audi A3 is cool</td>
<td>&#8230;</td>
<td><b>1</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A4</td>
<td>Audi A4 is cooler</td>
<td>&#8230;</td>
<td><b>2</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A5</td>
<td>Audi A5 is even cooler</td>
<td>&#8230;</td>
<td><b>3</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>TT</td>
<td>No doubt this is the best</td>
<td>&#8230;</td>
<td><b>4</b></td>
<td>&#8230;</td>
</tr>
</table>
<p style="text-align: justify;">Now, when I do a SELECT in my website, and I sort using the <b>sort_order</b>, then the models will be listed this way: A3, A4, A5 and finally Audi TT.</p>
<h5>The problem:</h5>
<p style="text-align: justify;"><b>¿What do I do if I want Audi TT to be the first in the website?</b></p>
<p style="text-align: justify;">Please note that I want to move Audi TT to the first position, but I don&#8217;t want the A3, A4 and A5 change their respective orders; also note I said the first position, but I could have said I want TT to be placed between A3 and A4.</p>
<p style="text-align: justify;">
<h5>Solution 1: the easy onel</h5>
<p style="text-align: justify;">Well, so the first that comes to mind  (at least to me) is basically to increment all rows whose sort_order is greater or equal to the position where I want to place the element I want to move, and then update the element I want to move. Let&#8217;s see in SQL:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">UPDATE</span> model <span style="color: #993333; font-weight: bold;">SET</span> sort_order <span style="color: #66cc66;">=</span> sort_order <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">WHERE</span> sort_order <span style="color: #66cc66;">&gt;=</span> <span style="color: #cc66cc;">1</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> model <span style="color: #993333; font-weight: bold;">SET</span> sort_order <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'TT'</span>;</pre></div></div>

<p style="text-align: justify;">Above you can see two queries: the first one has a linear complexity because it has to update every single row in the table whose sort order is greater than the position (but all rows should be checked); the other query is just to update the row of the TT.</p>
<p style="text-align: justify;">The problem with this solution is the part where ALL rows should be checked and lots of them updated. Then I though of another solution.</p>
<p style="text-align: justify;">
<h5>Solution 2: the magic of floats</h5>
<p style="text-align: justify;">An idea came to my mind: <b>¿What if instead of integers I use real numbers?</b></p>
<p style="text-align: justify;">If the field <b>sort_order</b> was a real number, then maybe I could somehow update just the row I want, and leave the rest untouched. Please keep in mind that I use an interface on the backend as well for listing the products and updating the order, so for all elements I know which is the sort_order for the previous and the next elements, as well as for the element itself.</p>
<p style="text-align: justify;">Now, imagine our initial configuration is (A3, A4, A5, TT), and we want to move the TT between A3 and A4. Then I only have to choose a sort_order value between A3 and A4, and magically the TT will be listed between both values. Let&#8217;s see the table after choose the  middle value:</p>
<table style="width: auto;">
<tr>
<th>name</th>
<th>description</th>
<th>&#8230;</th>
<th><b>sort_order</b></th>
<th>&#8230;</th>
</tr>
<tr>
<td>A3</td>
<td>Audi A3 is cool</td>
<td>&#8230;</td>
<td><b>1</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>TT</td>
<td>No doubt this is the best</td>
<td>&#8230;</td>
<td><b>1.5</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A4</td>
<td>Audi A4 is cooler</td>
<td>&#8230;</td>
<td><b>2</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A5</td>
<td>Audi A5 is even cooler</td>
<td>&#8230;</td>
<td><b>3</b></td>
<td>&#8230;</td>
</tr>
</table>
<p style="text-align: justify;">Basically I&#8217;ve done (1.0 + 2.0) / 2 to get the intermediate value between A3 and A4.</p>
<p style="text-align: justify;">Notice you can also do this as many times as you want, and not only the order will always be preserved, but you will <em>&#8220;never find&#8221;</em> two rows having the same sort_order.</p>
<p style="text-align: justify;">Let&#8217;s see another example, imagine now we want the A4 to be placed between A3 and TT, then the sort_order for A4 will be <b>(1.0 + 1.5)/2 = 1.25</b>.</p>
<table style="width: auto;">
<tr>
<th>name</th>
<th>description</th>
<th>&#8230;</th>
<th><b>sort_order</b></th>
<th>&#8230;</th>
</tr>
<tr>
<td>A3</td>
<td>Audi A3 is cool</td>
<td>&#8230;</td>
<td><b>1</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A4</td>
<td>Audi A4 is cooler</td>
<td>&#8230;</td>
<td><b>1.25</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>TT</td>
<td>No doubt this is the best</td>
<td>&#8230;</td>
<td><b>1.5</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A5</td>
<td>Audi A5 is even cooler</td>
<td>&#8230;</td>
<td><b>3</b></td>
<td>&#8230;</td>
</tr>
</table>
<p style="text-align: justify;">It&#8217;s easy, ¿isn&#8217;t it? And you just have to update a single row.</p>
<p style="text-align: justify;">Now there are lots of questions. This is so easy and cool, but <b>¿what happens if you want to place an element in the first position?</b> If we asume the sort_order starts at 1 for the first element, then the previous element should be choosen as 0.00 (and the same rules will work).</p>
<p style="text-align: justify;"><b>¿And what happens if you want to insert the A6 model?</b> As there are 4 elements, the sort_order for A6 will be 5.0 (to avoid colision, just use <b>number of rows + 1</b>).</p>
<p style="text-align: justify;">And the very good question is: &#8220;<i>floating point numbers had limitations, ¿didn&#8217;t they?</i> <b>¿what happens if there is an overflow?</b>&#8221; Yes man! this is the question!</p>
<p style="text-align: justify;">Making sort_order to overflow is as easy as keep moving all elements to the same position, over and over again. For example, if we move the element with sort order 3, between the elements with sort order 1 and 2, then the order will be 1.0, 1.5, 2.0, then if we move the 2.0 between 1 and 1.5, we will have 1.0, 1.25, 1.5, if we do repeat the last operation we will have 1.0, 1.125, 1.25, and if you repeat this several times you will get lots of decimals.</p>
<p style="text-align: justify;">This gets us to the third solution</p>
<h5>Solution 3: the workaround</h5>
<h6>Solution 3.1: the background process</h6>
<p style="text-align: justify;">One solution could be to have a process once a day,week or month to sort all elements and update the sort_order by their index once sorted, that will remove all decimals for all rows. But if you are going to do this, better implement the first solution.</p>
<h6>Solution 3.2: updating to integer when possible</h6>
<p style="text-align: justify;">This solution is the same as the solution 2, but rounding numbers to integers before updating, and checking that the number is between (but not equal) to the previous and the next position where we want to be placed.</p>
<p style="text-align: justify;">Let&#8217;s see an example starting with following set of rows:</p>
<table style="width: auto;">
<tr>
<th>name</th>
<th>description</th>
<th>&#8230;</th>
<th><b>sort_order</b></th>
<th>&#8230;</th>
</tr>
<tr>
<td>A3</td>
<td>Audi A3 is cool</td>
<td>&#8230;</td>
<td><b>1.125</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>TT</td>
<td>No doubt this is the best</td>
<td>&#8230;</td>
<td><b>1.0394</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A4</td>
<td>Audi A4 is cooler</td>
<td>&#8230;</td>
<td><b>1.7628</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A5</td>
<td>Audi A5 is even cooler</td>
<td>&#8230;</td>
<td><b>3.78</b></td>
<td>&#8230;</td>
</tr>
</table>
<p style="text-align: justify;">Then if I move A3, between A4 and A5, using solution two I will do the following: (1.7628 + 3.78)/2 = 2.7714. But, if I round 2.7714 down I get 2, and 2 is still between 1.7628 and 3.78, so instead of updating to 2.7714, it will be updated to 2. This way, if I move TT again between A3 and A5, this time instead of computing <b>(2.7714 + 3.78) / 2</b>, the TT position would be computed as <b>(2 + 3.78) / 2</b>, which contains less decimals.</p>
<p style="text-align: justify;">Just notice that in this solution one only has to check if the number is still between the two numbers around, which are already known, and keeps the performance high. Just an update, as before, and all the computations are made on the client-side</p>
<p style="text-align: justify;">If we assume that the movement of all elements will be almost random, using this way of computing number is less probable (but still possible) that an overflow happens.</p>
<p style="text-align: justify;">By the way, I presented this solution using floating point numbers, but you can use integers (and I don&#8217;t mean using 10, 20, 30, and then getting intermediate values).</p>
<h5>Conclussion</h5>
<p style="text-align: justify;">If you want a robust way, use the first solution; but if you want to update just an element each time, because there will be thousands of elements and thousand of users updating the sort order at the same time, then probably the second solution is better, although there can be problems when dealing with concurrent requests.
<p style="text-align: justify; font-weight: bold;">Finally although I&#8217;ve presented a couple of solutions (with their workarounds), I would be pleased to hear of another solutions or improvements.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2009/01/update-order-of-a-row-keeping-the-rest-untouched/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Manso trick: how to force the browser to reload an image</title>
		<link>http://www.codigomanso.com/en/2008/12/truco-manso-forzar-la-recarga-de-una-imagen/</link>
		<comments>http://www.codigomanso.com/en/2008/12/truco-manso-forzar-la-recarga-de-una-imagen/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 16:47:39 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[recargar imagen]]></category>
		<category><![CDATA[recargar pagina]]></category>
		<category><![CDATA[truco manso]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/?p=287</guid>
		<description><![CDATA[It is not difficult to find yourself programming a beautiful AJAX web page and realizing you have to reload an image, because it has been changed in the server.
As you use AJAX  in your web site,  it is not desirable to make the user reload the page for just viewing an update image (not cool [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">It is not difficult to find yourself programming a beautiful AJAX web page and realizing you have to reload an image, because it has been changed in the server.</p>
<p style="text-align: justify;">As you use AJAX  in your web site,  it is not desirable to make the user reload the page for just viewing an update image (not cool at all).</p>
<p style="text-align: justify;">At this point you do some tests, you try to change the &#8220;src&#8221; attribute of the img tag, you try to remove the previous img and create a new one&#8230; but all your effort is useless. The browser has cached the image the first time that was accessed.</p>
<p style="text-align: justify;">Probably the first idea that comes to your mind is, well, if I cannot reload the image, then I&#8217;ll make the server generates a new one (with a different name each time), and I&#8217;ll load that image with a different name.</p>
<p style="text-align: justify;">You know this is not the best way of doing things, right?</p>
<p style="text-align: justify;">Fortunately there is another easy solution to this: just add a query string at the end of the image src, that changes each time. That would be enough <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p style="text-align: justify;">To make this clear, if your original image has been loaded like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span>  <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;ExampleImage&quot;</span>  <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://server/path/to/image.png&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p style="text-align: justify;">Then you should try to reload the image this way:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span>  <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;ExampleImage&quot;</span>  <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://server/path/to/image.png&lt;strong&gt;</span></span>?nocache=1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/strong<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>&quot; alt=&quot;&quot; /&gt;</pre></div></div>

<p style="text-align: justify;">And then, increment the nocache value on each request.</p>
<p style="text-align: justify;">Using jQuery, the code will look something similar to:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ExampleImage&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span> imageSource <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;?nocache=&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>g_id<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I&#8217;ve seen forums with this kind of question, so I hope this helps someone <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2008/12/truco-manso-forzar-la-recarga-de-una-imagen/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

