<?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>0plus1 Blog</title>
	<atom:link href="http://0plus1.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://0plus1.com/blog</link>
	<description>Code snippets and programming resources</description>
	<lastBuildDate>Sat, 15 Jan 2011 15:09:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Facebook Hacker Cup 2011 Qualification – Studious Student</title>
		<link>http://0plus1.com/blog/2011/01/facebook-hacker-cup-2011-round-1-%e2%80%93-studious-student/</link>
		<comments>http://0plus1.com/blog/2011/01/facebook-hacker-cup-2011-round-1-%e2%80%93-studious-student/#comments</comments>
		<pubDate>Sun, 09 Jan 2011 15:10:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://0plus1.com/blog/?p=71</guid>
		<description><![CDATA[Here we go with another problem. Here&#8217;s the test Studious Student You&#8217;ve been given a list of words to study and memorize. Being a diligent student of language and the arts, you&#8217;ve decided to not study them at all and instead make up pointless games based on them. One game you&#8217;ve come up with is [...]]]></description>
			<content:encoded><![CDATA[<p>Here we go with another problem. Here&#8217;s the test</p>
<blockquote>
<div>Studious Student</div>
<p>You&#8217;ve  been given a list of words to study and memorize.  Being a diligent  student of language and the arts, you&#8217;ve decided to not study them at  all and instead make up pointless games based on them.  One game you&#8217;ve  come up with is to see how you can concatenate the words to generate the  lexicographically lowest possible string.</p>
<h3>Input</h3>
<p>As input for playing this game you will receive a text file containing an integer <strong>N</strong>, the number of word sets you need to play your game against.  This will be followed by <strong>N</strong> word sets, each starting with an integer <strong>M</strong>, the number of words in the set, followed by <strong>M</strong> words.  All tokens in the input will be separated by some whitespace and, aside from <strong>N</strong> and <strong>M</strong>, will consist entirely of lowercase letters.</p>
<h3>Output</h3>
<p>Your submission should contain the lexicographically shortest strings  for each corresponding word set, one per line and in order.</p>
<h3>Constraints</h3>
<p>1 &lt;= <strong>N</strong> &lt;= 100<br />
1 &lt;= <strong>M</strong> &lt;= 9<br />
1 &lt;= all word lengths &lt;= 10</p></blockquote>
<p>Basically we have to find the composed string that is the <a href="http://en.wikipedia.org/wiki/Lexicographical_order">lexicographically</a>&#8216;s shortest. So where is the issue here? The issue is that the problem states that you have to find the shortest possible composed string, so order each of the words cannot be simply ordered and combined because that may or may not be the correct solution (we will go there later). The easiest way to do this is to recursively test each of the possible permutations of the words against each other, this is the bruteforcing method. Let&#8217;s see.</p>
<p><strong>Bruteforcing Method</strong></p>
<p>First we have to parse the input:</p>
<pre class="brush: php; title: ;">
function fromTxtToArray($filename){
    $fh = fopen($filename, &quot;rb&quot;);
    $data = fread($fh, filesize($filename));
    fclose($fh);

    $array = explode(&quot;\n&quot;,$data);
    $rows = $array[0];
    unset($array[0]);

	$i = 0;
	$return = array();
	foreach ($array as $k =&gt; $v){
		$return[$i] = explode(' ',trim($v));
		unset($return[$i][0]);
		$i++;
	}
    return array('rows' =&gt; $rows, 'data' =&gt; $return);
}
</pre>
<p>Then we have to permutate each of the words we have been given and then sort them (php <a href="http://php.net/manual/en/function.sort.php">sort</a> luckily automagically sorts strings lexicographically) returning the first one.</p>
<pre class="brush: php; title: ;">
//source of this function -&gt; http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm
function permute($items, $perms = array()) {

	global $permutations;

    if (empty($items)) {
        $permutations[] = join('', $perms);
    }  else {
		for ($i = count($items)-1; $i &gt;= 0; --$i) {
		     $newitems = $items;
             $newperms = $perms;
             list($foo) = array_splice($newitems, $i, 1);
             array_unshift($newperms, $foo);
             permute($newitems, $newperms);
         }
		 return $permutations;
    }
}

function sortAndKill($array){
	sort($array,SORT_STRING);
	return $array[0];
}

$data = fromTxtToArray('input.txt');

set_time_limit(0);

foreach ($data['data'] as $k =&gt; $v){
	$permutations = array();
   	$combinations = permute($v);
	echo sortAndKill($combinations).&quot;\n&quot;;
	unset($permutations);
}
</pre>
<p>This works perfectly with the example, sadly with the real output it will take too long (I let it run for more than 20 minutes with no result). If you wanna test it just copy and paste the test input into a txt file.</p>
<blockquote><p>
5<br />
6 facebook hacker cup for studious students<br />
5 k duz q rc lvraw<br />
5 mybea zdr yubx xe dyroiy<br />
5 jibw ji jp bw jibw<br />
5 uiuy hopji li j dcyi
</p></blockquote>
<p>It&#8217;s obvious that we have to switch to <a href="http://en.wikipedia.org/wiki/Dynamic_programming">dp</a>.</p>
<p><strong>Dynamic Programming</strong></p>
<p>What is the problem here? Is that we have too many cases to test, we have to find a way to exclude obviously wrong cases. We can start by ordering the words we have been given and see the results.</p>
<pre class="brush: php; title: ;">
function lexiOrder($array){
	sort($array,SORT_STRING);
    $string = false;
    foreach ($array as $k =&gt; $v){
        $string .= trim($v);
    }
    return $string;
}

$data = fromTxtToArray('input.txt');

$result = false;
foreach ($data['data'] as $k =&gt; $v){
    $result .= lexiOrder($v).&quot;\n&quot;;
}
echo $result;
</pre>
<p>It works! No wait a minute..</p>
<p>The string: bw<strong>ji</strong>jibwjibwjp is wrong it should be: bwjibwjibw<strong>ji</strong>jp this is because while ji comes before jibw merging them togheter will return jijibw while the correct one is jibwji (b comes before j) the standard sorting will not work anymore.. we have to use usort.</p>
<pre class="brush: php; title: ;">
function letterwalk($a,$b){

    if ($a == $b) {
        return 0;
    }
    for ($i=0;$i&lt;10;$i++){

        $lettera = substr($a, $i, 1);
        $letterb = substr($b, $i, 1);

		if ($lettera &lt; $letterb){
			return -1;
		} elseif($lettera &gt; $letterb) {
			return 1;
		}
    }
}

//usort($array, &quot;letterwalk&quot;);
</pre>
<p>So what is this? It&#8217;s a function to walk and compare sequentially every single letter of the word against each other. But wait we still get the same result as before! This is because comparing ji agains jibw will fail because b will get compared to null. To avoid this we have to edit the function:</p>
<pre class="brush: php; title: ;">
function letterwalk($a,$b){

    if ($a == $b) {
        return 0;
    }
    for ($i=0;$i&lt;10;$i++){

        $lettera = substr($a, $i, 1);
        $letterb = substr($b, $i, 1);

        if($lettera &amp;&amp; $letterb) {
            if ($lettera &lt; $letterb){
                return -1;
            } elseif($lettera &gt; $letterb) {
                return 1;
            }
        } else {
            if($lettera==$letterb){
                return 0;
            } elseif ($lettera &lt; $letterb) {
                return 1;
            } else {
                return -1;
            }
        }
    }
}
</pre>
<p>Finally the example works! But we are not finished yet. The problem is that there may be case where a shorter string must go AFTER the longest one.<br />
to clarify:</p>
<blockquote><p>ji / jibw</p>
<p>jibwji is the correct answer, so ji must go after jibw.</p></blockquote>
<p>but</p>
<blockquote><p>aa / aabb</p>
<p>aaaabb is the correct answer, aa must go before aabb</p></blockquote>
<p>How do we test this? We can sort them in two different ways! So here we have the final solution:</p>
<pre class="brush: php; title: ;">
function lexiOrder($array,$mode){

	if ($mode == 'letter'){
		usort($array, &quot;letterwalk&quot;);
	} elseif ($mode == 'word'){
		sort($array,SORT_STRING);
	}

    $string = false;
    foreach ($array as $k =&gt; $v){
        $string .= trim($v);
    }
    return $string;
}

$data = fromTxtToArray('input.txt');

$result = false;
foreach ($data['data'] as $k =&gt; $v){

	$order_l = lexiOrder($v,'letter');
	$order_w = lexiOrder($v,'word');

	$order_f = array($order_l,$order_w);
	sort($order_f,SORT_STRING);

	$result .= $order_f[0].&quot;\n&quot;;
}
echo $result;
</pre>
<p>BUT!! What about if we find a case with two couples like this:</p>
<blockquote><p>ji jibw<br />
aa aabb</p></blockquote>
<p>The correct order would be:</p>
<blockquote><p>aaaabb jibwji</p></blockquote>
<p>Since our code now can only test two cases (the shortest before and the shortest after) should we find such a case the algotithm will fail since it will only have:</p>
<blockquote><p>aaaabb jijibw<br />
aabbaa jibwji</p></blockquote>
<p>to test, neither of them are correct. We are takling the problem from the wrong perspective. We need to be smart. Let&#8217;s analyse the strings.</p>
<blockquote><p>
ji jibw -> jibwji Because the j comes after the b<br />
aa aabb -> aaaabb because the a comes before the b</p></blockquote>
<p>So..</p>
<blockquote><p>
ji jibw -> ji<strong>J</strong> jibw<br />
aa aabb -> aa<strong>A</strong> aabb</p></blockquote>
<p>We also know that words are between 1 and 10 chars so we can safely pad them to</p>
<blockquote><p>
ji jibw -> ji<strong>JJJJJJJJ</strong> jibw<strong>JJJJJJ</strong><br />
aa aabb -> aa<strong>AAAAAAAA</strong> aabb<strong><br />
AAAAAA</strong></p></blockquote>
<blockquote><p>
aaaabb jijibw<br />
aabbaa jibwji
</p></blockquote>
<p>This way the strings can be sorted with a simple php sort!</p>
<pre class="brush: php; title: ;">
function padAndOrder($array){
	foreach ($array as $k =&gt; $v) {
        echo $strings[$k] = str_pad($v, 10 + strlen($v), substr($v,0,1));
    }

	sort($strings, SORT_STRING);

	$return = false;
    foreach ($strings as $k =&gt; $v) {
        $return .= substr($v, 0, -10);
    }
	return $return;
}
</pre>
<p>Once again enjoy! I&#8217;m doing this to share my method (and the way I&#8217;ve come to it) so that it may inspire other, there are maybe thousand of better ways to do this, it&#8217;s just my take. Peace.</p>
]]></content:encoded>
			<wfw:commentRss>http://0plus1.com/blog/2011/01/facebook-hacker-cup-2011-round-1-%e2%80%93-studious-student/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook Hacker Cup 2011 Qualification &#8211; Double Squares</title>
		<link>http://0plus1.com/blog/2011/01/facebook-hacker-cup-2011-round-1-double-squares/</link>
		<comments>http://0plus1.com/blog/2011/01/facebook-hacker-cup-2011-round-1-double-squares/#comments</comments>
		<pubDate>Sat, 08 Jan 2011 23:10:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[hacker challenge]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://0plus1.com/blog/?p=50</guid>
		<description><![CDATA[Most of the programmers now about the Hacker Challenge from Facebook. I will now explain my take on it using php. The reason I&#8217;m doing this is because this contest is an epic fail (read here the reasons why) full of bug, with really confused rules badly explained (I&#8217;m looking at you 6 minutes time [...]]]></description>
			<content:encoded><![CDATA[<p>Most of the programmers now about the <a href="http://www.facebook.com/event.php?eid=148059031890072">Hacker Challenge</a> from Facebook. I will now explain my take on it using php.<br />
The reason I&#8217;m doing this is because this contest is an epic fail (<a href="http://www.quora.com/Andrew-Brown-2/Facebook-Hacker-Cup-Resounding-Failure">read here the reasons why</a>) full of bug, with really confused rules badly explained (I&#8217;m looking at you 6 minutes time limit with no warning and the code/no-code in the reply question). Anyway it&#8217;s still an interesting programming puzzle..</p>
<p>Let us start with the text of the problem:</p>
<blockquote><p>Double Squares<br />
A double-square number is an integer X which can be expressed as the sum of two perfect squares. For example, 10 is a double-square because 10 = 3<sup>2</sup> + 1<sup>2</sup>. Your task in this problem is, given X, determine the number of ways in which it can be written as the sum of two squares. For example, 10 can only be written as 3<sup>2</sup> + 1<sup>2</sup> (we don&#8217;t count 1<sup>2</sup> + 3<sup>2</sup> as being different). On the other hand, 25 can be written as 5<sup>2</sup> + 0<sup>2</sup> or as 4<sup>2</sup> + 3<sup>2</sup>.</p>
<p>Input<br />
You should first read an integer N, the number of test cases. The next N lines will contain N values of X.<br />
Constraints<br />
0  &lt;  X  &lt;  2147483647<br />
1  &lt; N  &lt; 100<br />
Output<br />
For each value of X, you should output the number of ways to write X as the sum of two squares.</p></blockquote>
<p>Facebook right at the top of the page gives you an hint: <strong>Too hard for brute force, switching to dp</strong><br />
This is because the problems that have been given are fairly easy to code using brute force, but hard to do the smart way, since we are using php here the smart way is the only way to go (you&#8217;ll see the reason in a while).</p>
<p>Let&#8217;s start.</p>
<p>First thing is to read the input, easy one.. since I like multidimensional arrays I did it like this:</p>
<pre class="brush: php; title: ;">
function fromTxtToArray($filename){
	$fh = fopen($filename, &quot;rb&quot;);
	$data = fread($fh, filesize($filename));
	fclose($fh);

	$array = explode(&quot;\n&quot;,$data);
	$rows = $array[0];
	unset($array[0]);

	return array('rows' =&gt; $rows, 'data' =&gt; $array);
}
</pre>
<p>Then we have the main cycle (I discarded completely the rows because I like foreach better than for):</p>
<pre class="brush: php; title: ;">
$data = fromTxtToArray('input.txt');
foreach ($data['data'] as $k =&gt; $v){
    //?????
}
</pre>
<p>Now that the main logic is out of the way let&#8217;s work the problem, starting with the:</p>
<p><strong>bruteforcing method</strong></p>
<pre class="brush: php; title: ;">

function scompose($number){
	$array = array();

	if ($number % 2 == 0) {
		$stop = $number/2;
	} else {
		$stop = round($number/2, 0, PHP_ROUND_HALF_DOWN);
	}

	for($i=0;$i&lt;=$number;$i++){

		if($i&gt;$stop){
			break;
		}
		$num1 = $number-$i;
		$num2 = $i;

		if(isPerfect($num1)&amp;&amp;isPerfect($num2)){
			$array[] = array('num1'=&gt;$num1,'num2'=&gt;$num2);
		}
	}

	return $array;
}

$data = fromTxtToArray('input.txt');

set_time_limit(0);

foreach ($data['data'] as $k =&gt; $v){
     echo count(scompose($v)).&quot;\n&quot;;
}
</pre>
<p>Basically what this does is to cycle the number in order to get all of his possible couple of addends, it does this simply by subtracting $i from the number and taking $i as the other addend. Now this will create a real issue because doing so will give you repeated couples (eg: take five you&#8217;ll get 0,5;1,4;2,3;3,2;4,1;5,0) this is wrong since the problem text clearly states that there shouldn&#8217;t be repetition. To avoid this we have to divide the number by two, rounding him down in case it&#8217;s odd, using this value we can break the cycle when we get to that point. After that we check if both of the addends of the numbers are perfect squares if so we put them in an array, when the cycle stops we count the array and we are done. The missing functions are these:</p>
<pre class="brush: php; title: ;">
//Avoids problem with the buggy is_int native php function
function betterIs_int($int){

	if(is_numeric($int) === true){
		if((int)$int == $int){
			return true;
		} else {
			return false;
		}
	}else{
		return false;
	}
}
//Check if the square root of a number is an integer (perfect square)
function isPerfect($int){
	if(betterIs_int(sqrt($int))){
		return true;
	} else {
		return false;
	}
}
</pre>
<p>I actually let this thing run for fun with the input.txt file and I stopped it after 14 minutes! It works with the examples though, just use this:</p>
<pre class="brush: php; title: ;">
$data['data']= array('10','25','3','0','1');
</pre>
<p>Let&#8217;s move to the:</p>
<p><strong>Dynamic programming</strong></p>
<p>If you are not acquainted with this methodology please read <a href="http://en.wikipedia.org/wiki/Dynamic_programming">here</a> and <a href="http://stackoverflow.com/questions/4278188/good-examples-articles-books-for-understanding-dynamic-programming">here</a>.</p>
<p>The problem we had with the brute force was that the for cycle was very very loooong, we have to find a better way to approach this! See we are checking every couple to see if they&#8217;re perfect square  what about a method where we can rule out directly if there are? First we have to find the highest possible square root of the number, since we cannot exceed this number we have less cases to test. Then we cycle $i from 0 to the maxsquare and we check if the square root of the difference between the number and the square of $i is an integer, if it is we have a winning couple! But wait we still have a problem, the couples are once again repeated! We have to divide them by 2.</p>
<pre class="brush: php; title: ;">
function firstDraft($number){

    if($number==0||$number==1){
	        return 1;
	}

	$result=0;

	$maxsquare = floor(sqrt($number));

	for ($i=0; $i&lt;$maxsquare;$i++) {

		$difference = sqrt($number-($i*$i));

		if (betterIs_int($difference)) {
			$result++;
		}
	}
	return ceil($result/2);
}
</pre>
<p>Wait! Divide them by two? Why? We could just calculate the half square root of the number and trim down the cases to test! And so..</p>
<pre class="brush: php; title: ;">
function dp($number){

    if($number==0||$number==1){
	        return 1;
	}

	$result=0;

	$halfmaxsquare = ceil(sqrt($number/2));

	for ($i=0; $i&lt;$halfmaxsquare;$i++) {

		$difference = sqrt($number-($i*$i));

		if (betterIs_int($difference)) {
			$result++;
		}
	}
	return $result;
}

$data = fromTxtToArray('input.txt');

foreach ($data['data'] as $k =&gt; $v){
	echo dp($v).&quot;\n&quot;;
}
</pre>
<p>No need to set time limit here <img src='http://0plus1.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' /><br />
enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://0plus1.com/blog/2011/01/facebook-hacker-cup-2011-round-1-double-squares/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Append value to multiselect with jquery</title>
		<link>http://0plus1.com/blog/2010/08/append-value-to-multiselect-with-jquery/</link>
		<comments>http://0plus1.com/blog/2010/08/append-value-to-multiselect-with-jquery/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 10:24:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://0plus1.com/blog/?p=44</guid>
		<description><![CDATA[This is a simple function to append values to a multiselect. In the comments you&#8217;ll find the explanation of each command. function appendMulti(value,target){ //Get old values from multiselect (they are an array) oldvalues= $(target).val(); //We need to check if the multiselect is empty if(oldvalues){ //It's not empty //Find the lenght of the array, the beauty [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple function to append values to a multiselect. In the comments you&#8217;ll find the explanation of each command.</p>
<pre class="brush: jscript; title: ;">
function appendMulti(value,target){
    //Get old values from multiselect (they are an array)
	oldvalues= $(target).val();
	//We need to check if the multiselect is empty
	if(oldvalues){
        //It's not empty
		//Find the lenght of the array, the beauty of this is that since arrays starts from 0, this number will always be the next available slot
		key = value.length;
	} else {
        //It's empty
		//Initialize a new array and set the key to the first element
		var oldvalues = [];
		key = 0;
	}
	//Append new value to the array
	oldvalues[key] = value;
	//Set the new values
	$(target).val(oldvalues);

	return true;
}
</pre>
<p>You can call the function like this:</p>
<pre class="brush: jscript; title: ;">
appendMulti('value','#multiselect');
</pre>
]]></content:encoded>
			<wfw:commentRss>http://0plus1.com/blog/2010/08/append-value-to-multiselect-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ajax calls vs MSSql</title>
		<link>http://0plus1.com/blog/2010/06/ajax-calls-vs-mssql/</link>
		<comments>http://0plus1.com/blog/2010/06/ajax-calls-vs-mssql/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 13:40:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[utf-8]]></category>
		<category><![CDATA[windows-1252]]></category>

		<guid isPermaLink="false">http://0plus1.com/blog/?p=42</guid>
		<description><![CDATA[Please note that this article can be applied to any non utf-8 encoding. I recently needed to send data using ajax to an MSSql 2000 database (which requires windows-1252). As you may know javascript sends data encoded in utf-8 (utf-16 to be more precise), this must be accepted as a fact thus making the use [...]]]></description>
			<content:encoded><![CDATA[<p>Please note that this article can be applied to any non utf-8 encoding.</p>
<p>I recently needed to send data using ajax to an MSSql 2000 database (which requires windows-1252).<br />
As you may know javascript sends data encoded in utf-8 (<a href="http://interglacial.com/javascript_spec/a-8.html">utf-16 to be more precise</a>), this must be accepted as a fact thus making the use of a php function to convert the encoding before sending to the database. Here is the snippet I use:</p>
<pre class="brush: php; title: ;">
function convertor($array){
	if (is_array($array) &amp;&amp; count($array) &gt; 0){
		foreach ($array as $k =&gt; $v){
			$return[$k] = mb_convert_encoding($v, &quot;iso-8859-1&quot;,&quot;UTF-8&quot;);
		}
	} elseif (!empty($array)) {
		$return = mb_convert_encoding($array, &quot;iso-8859-1&quot;,&quot;UTF-8&quot;);
	} else {
		return false;
	}
	return $return;
}
</pre>
<p>Basically what it does is to check if we have an array or a single value then proceed to convert it to the desired encoding (in my case iso-8859-1), after converting the values these can be safely stored into the database.</p>
<p>I have also to add that I&#8217;ve encountered some jquery plugins that messes with the encoding of the strings. To be on the safe side you may use this line:</p>
<pre class="brush: jscript; title: ;">
unescape(encodeURIComponent(string))
</pre>
]]></content:encoded>
			<wfw:commentRss>http://0plus1.com/blog/2010/06/ajax-calls-vs-mssql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jquery-ui dialog with ajax how to avoid a common error</title>
		<link>http://0plus1.com/blog/2010/05/jquery-ui-dialog-with-ajax-how-to-avoid-a-common-error/</link>
		<comments>http://0plus1.com/blog/2010/05/jquery-ui-dialog-with-ajax-how-to-avoid-a-common-error/#comments</comments>
		<pubDate>Mon, 31 May 2010 14:40:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery-ui]]></category>

		<guid isPermaLink="false">http://0plus1.com/blog/?p=37</guid>
		<description><![CDATA[I&#8217;ve made this post because I got asked why this snippet gave problems: $.ajax({ type: &#34;POST&#34;, url: &#34;somepage.php&#34;, success: function(html){$(&#34;#someDiv&#34;).html(html).dialog();} }); The problem with this code is that it creates a new instance of dialog each time, basically adding a new dialog div in the html for each call. Aside from being unable to reopen [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve made this post because I got asked why this snippet gave problems:</p>
<pre class="brush: jscript; title: ;">
$.ajax({
   type: &quot;POST&quot;, url: &quot;somepage.php&quot;,
   success: function(html){$(&quot;#someDiv&quot;).html(html).dialog();}
});
</pre>
<p>The problem with this code is that it creates a new instance of dialog each time, basically adding a new dialog div in the html for each call.<br />
Aside from being unable to reopen the dialog after closing it, your are replicating your ajax code several times across the webpage creating all sort of problems with the jquery selectors.</p>
<p>The jquery-ui docs do not expalin well this issue, so to avoid headache here is the correct code:</p>
<pre class="brush: jscript; title: ;">
$(function() {
   $(&quot;#someDiv&quot;).dialog({ autoOpen: false });
});
</pre>
<p>then in the ajax call:</p>
<pre class="brush: jscript; title: ;">
$.ajax({
   type: &quot;POST&quot;, url: &quot;somepage.php&quot;,
   success: function(html){$(&quot;#someDiv&quot;).html(html).dialog('open');}
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://0plus1.com/blog/2010/05/jquery-ui-dialog-with-ajax-how-to-avoid-a-common-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>European Countries array</title>
		<link>http://0plus1.com/blog/2009/06/european-countries-array/</link>
		<comments>http://0plus1.com/blog/2009/06/european-countries-array/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 09:05:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[european countries array]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://0plus1.com/blog/?p=31</guid>
		<description><![CDATA[We recently had to create a select reserved only to contries from the european union, here&#8217;s the array with the standard countries codes: $eu_countries = array( &#34;AT&#34; =&#62; &#34;Austria&#34;, &#34;BE&#34; =&#62; &#34;Belgium&#34;, &#34;BG&#34; =&#62; &#34;Bulgaria&#34;, &#34;CY&#34; =&#62; &#34;Cyprus&#34;, &#34;CZ&#34; =&#62; &#34;Czech Republic&#34;, &#34;DK&#34; =&#62; &#34;Denmark&#34;, &#34;EE&#34; =&#62; &#34;Estonia&#34;, &#34;FI&#34; =&#62; &#34;Finland&#34;, &#34;FR&#34; =&#62; &#34;France&#34;, &#34;DE&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>We recently had to create a <em>select</em> reserved only to contries from the european union, here&#8217;s the array with the standard countries codes:</p>
<pre class="brush: php; title: ;">
$eu_countries = array(
  &quot;AT&quot; =&gt; &quot;Austria&quot;,
  &quot;BE&quot; =&gt; &quot;Belgium&quot;,
  &quot;BG&quot; =&gt; &quot;Bulgaria&quot;,
  &quot;CY&quot; =&gt; &quot;Cyprus&quot;,
  &quot;CZ&quot; =&gt; &quot;Czech Republic&quot;,
  &quot;DK&quot; =&gt; &quot;Denmark&quot;,
  &quot;EE&quot; =&gt; &quot;Estonia&quot;,
  &quot;FI&quot; =&gt; &quot;Finland&quot;,
  &quot;FR&quot; =&gt; &quot;France&quot;,
  &quot;DE&quot; =&gt; &quot;Germany&quot;,
  &quot;GR&quot; =&gt; &quot;Greece&quot;,
  &quot;HU&quot; =&gt; &quot;Hungary&quot;,
  &quot;IE&quot; =&gt; &quot;Ireland&quot;,
  &quot;IT&quot; =&gt; &quot;Italy&quot;,
  &quot;LV&quot; =&gt; &quot;Latvia&quot;,
  &quot;LT&quot; =&gt; &quot;Lithuania&quot;,
  &quot;LU&quot; =&gt; &quot;Luxembourg&quot;,
  &quot;MT&quot; =&gt; &quot;Malta&quot;,
  &quot;NL&quot; =&gt; &quot;Netherlands&quot;,
  &quot;PL&quot; =&gt; &quot;Poland&quot;,
  &quot;PT&quot; =&gt; &quot;Portugal&quot;,
  &quot;RO&quot; =&gt; &quot;Romania&quot;,
  &quot;SK&quot; =&gt; &quot;Slovakia (Slovak Republic)&quot;,
  &quot;SI&quot; =&gt; &quot;Slovenia&quot;,
  &quot;ES&quot; =&gt; &quot;Spain&quot;,
  &quot;SE&quot; =&gt; &quot;Sweden&quot;,
  &quot;GB&quot; =&gt; &quot;United Kingdom&quot;
);
</pre>
<p><a href="http://europa.eu/abc/european_countries/index_en.htm">source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://0plus1.com/blog/2009/06/european-countries-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to allow ad-sense bot to crawl phpbb3 private forums</title>
		<link>http://0plus1.com/blog/2009/05/how-to-allow-ad-sense-bot-to-crawl-phpbb3-private-forums/</link>
		<comments>http://0plus1.com/blog/2009/05/how-to-allow-ad-sense-bot-to-crawl-phpbb3-private-forums/#comments</comments>
		<pubDate>Fri, 29 May 2009 10:04:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Communities]]></category>
		<category><![CDATA[ad-sense]]></category>
		<category><![CDATA[phpbb3]]></category>
		<category><![CDATA[private forum]]></category>

		<guid isPermaLink="false">http://0plus1.com/blog/?p=26</guid>
		<description><![CDATA[If you operate a forum with private sections and public section, you&#8217;ll find that the ads that ad-sense pushes on your website are created based solely on the public content. If you want more accurate result you may want to look into the ad-sense&#8217; s &#8220;Site Authentication&#8221; feature. Here&#8217;s a small how-to on how to [...]]]></description>
			<content:encoded><![CDATA[<p>If you operate a forum with private sections and public section, you&#8217;ll find that the ads that ad-sense pushes on your website are created based solely on the public content. If you want more accurate result you may want to look into the ad-sense&#8217; s &#8220;<strong>Site Authentication</strong>&#8221; feature. Here&#8217;s a small how-to on how to achieve this:</p>
<p><strong>phpbb3</strong></p>
<ul>
<li>Create a user in the acp (for this example sake let&#8217;s use: adsense as username and adsense as password).</li>
<li>Give him permission based on your forum structure, we advise to give him read-only access on the whole forum.</li>
</ul>
<p><strong>ad-sense</strong></p>
<ul>
<li>go to the <em>AdSense Setup</em> tab, then click on the <em>Site Authentication</em> section.</li>
<li>click on the <em>add a login</em> link</li>
<li>Set it like this:
<ul>
<li><strong>Restricted Directory or URL:</strong> the forum url (example: http://www.example.com/forum)</li>
<li><strong>Authentication URL:</strong> the path to the ucp.php?mode=login file (example: http://www.example.com/forum/ucp.php?mode=login)</li>
<li><strong>Authentication method:</strong> POST</li>
<li><strong>Parameters:</strong> (you have to add them manually)
<ul>
<li>Attribute: login &#8211; Value: Login</li>
<li>Attribute: password &#8211; Value: userpassword (example: adsense)</li>
<li>Attribute: username &#8211; Value: username (example: adsense)</li>
</ul>
</ul>
</li>
<li>Now click on: Test My Authentication URL to check if everything went ok</li>
<ul>
<p>Hope this will help you</p>
]]></content:encoded>
			<wfw:commentRss>http://0plus1.com/blog/2009/05/how-to-allow-ad-sense-bot-to-crawl-phpbb3-private-forums/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>print_r output beautifier</title>
		<link>http://0plus1.com/blog/2009/05/print_r-output-beautifier/</link>
		<comments>http://0plus1.com/blog/2009/05/print_r-output-beautifier/#comments</comments>
		<pubDate>Wed, 13 May 2009 15:42:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[print_r]]></category>

		<guid isPermaLink="false">http://0plus1.com/blog/?p=9</guid>
		<description><![CDATA[Today I wrote a script that needed to manipulate several multidimensional arrays, some of them pretty deep. The standard php function print_r is an hellish mess, browsing through the marvellous php.net documentation I&#8217;ve found several examples to beautify the output of print_r (just to give credit: &#8220;Bob&#8221; and &#8220;Everett&#8221; were my inspiration) since no one [...]]]></description>
			<content:encoded><![CDATA[<p>Today I wrote a script that needed to manipulate several multidimensional arrays, some of them pretty deep.</p>
<p>The standard php function print_r is an hellish mess, browsing through the marvellous php.net documentation I&#8217;ve found several examples to beautify the output of print_r (just to give credit: &#8220;Bob&#8221; and &#8220;Everett&#8221; were my inspiration) since no one actually did it the way I wanted I made a custom one myself:</p>
<pre class="brush: php; title: ;">
function print_rr($array){
    foreach($array as $key=&gt;$value){
       if(is_array($value)){
          $id = md5(rand());
          echo '[&lt;a href=&quot;#&quot; onclick=&quot;return expandParent(\''.$id.'\')&quot;&gt;'.$key.'&lt;/a&gt;]&lt;br /&gt;';
          echo '&lt;div id=&quot;'.$id.'&quot; style=&quot;display:none;margin:10px;border-left:1px solid; padding-left:5px;&quot;&gt;';
          print_rr($value, $count);
          echo '&lt;/div&gt;';
       } else {
       echo &quot;&lt;b&gt;$key&lt;/b&gt;: &quot;.$value.&quot;&lt;br /&gt;&quot;;
       }
    }
echo '&lt;script language=&quot;Javascript&quot;&gt;
function expandParent(id){toggle=&quot;block&quot;;if(document.getElementById(id).style.display==&quot;block&quot;){toggle=&quot;none&quot;}document.getElementById(id).style.display=toggle};
&lt;/script&gt;';
}
</pre>
<p>Feel free to use it anyway you like it.</p>
]]></content:encoded>
			<wfw:commentRss>http://0plus1.com/blog/2009/05/print_r-output-beautifier/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Welcome</title>
		<link>http://0plus1.com/blog/2009/05/welcome/</link>
		<comments>http://0plus1.com/blog/2009/05/welcome/#comments</comments>
		<pubDate>Wed, 13 May 2009 14:27:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[0plus1]]></category>
		<category><![CDATA[welcome]]></category>

		<guid isPermaLink="false">http://0plus1.com/blog/?p=3</guid>
		<description><![CDATA[Welcome to the official blog from 0plus1.]]></description>
			<content:encoded><![CDATA[<p>Welcome to the official blog from 0plus1.</p>
]]></content:encoded>
			<wfw:commentRss>http://0plus1.com/blog/2009/05/welcome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

