<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users337</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/337" rel="self"/>
  <title>Eineki</title>
  <updated>Tue Apr 27 11:45:23 -0700 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor498855</id>
    <published>2010-04-27T11:45:23-07:00</published>
    <title>[JavaScript] On how i make it work on Firefox?</title>
    <content type="html">&lt;p&gt;Is my previous post still pending approval?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1271-use-to-work-on-ie6/refactors/498855" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor422053</id>
    <published>2010-01-21T12:58:45-08:00</published>
    <title>[JavaScript] On pb resize image</title>
    <content type="html">&lt;p&gt;How is the function misbehaving?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1150-pb-resize-image/refactors/422053" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor418979</id>
    <published>2010-01-19T23:32:29-08:00</published>
    <title>[JavaScript] On pb resize image</title>
    <content type="html">&lt;p&gt;How call you the method? 
&lt;br /&gt;Why do you use getElementById(&amp;quot;apodimg&amp;quot;) with the &amp;quot;&amp;quot; instead that 
&lt;br /&gt;getElementById(apodimg)? 
&lt;br /&gt;Have your image id=&amp;quot;apodimg&amp;quot;?
&lt;br /&gt;I've tested the function with firebug on this page (redim_img(document.getElementsByTagName(&amp;quot;img&amp;quot;)[2])) and it worked well (after reverting the max size to 50px)
&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1150-pb-resize-image/refactors/418979" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor418778</id>
    <published>2010-01-19T20:58:39-08:00</published>
    <title>[JavaScript] On pb resize image</title>
    <content type="html">&lt;p&gt;The code is quite good to me.
&lt;br /&gt;Just some glitches to be corrected:
&lt;br /&gt; -- var apodimg  = document.getElementById(&amp;quot;apodimg&amp;quot;); 
&lt;br /&gt;    you use an hardcoded string in place of the parameters you passed to the function, I think it was an oversight.
&lt;br /&gt; -- your calculations need to be rounded as width and height properties need integer values.
&lt;br /&gt; -- I introduced a test on the type of the input parameter in order to let you choice to pass 
&lt;br /&gt;    an element id or a DOM element.
&lt;/p&gt;

&lt;pre&gt;function redim_img(apodimg)
{
   // you can pass a string with the id of the
   // image you want to resize or a reference 
   // to the image
   if (typeof(apodimg) == &amp;quot;string&amp;quot;) {
    apodimg  = document.getElementById(apodimg);
   }

    var w=apodimg.width;
    var h=apodimg.height;

    if (w&amp;gt;=h &amp;amp;&amp;amp; w&amp;gt;=200)	{
       h=Math.round(h*200/w);
       w=200;
    }
    if (h&amp;gt;w &amp;amp;&amp;amp; h&amp;gt;=160) {
       w=Math.round(w*200/h);
       h=160;
    }

    apodimg.style.width=w+'px';
    apodimg.style.height=h+'px';
}
&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1150-pb-resize-image/refactors/418778" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor418777</id>
    <published>2010-01-19T20:57:39-08:00</published>
    <title>[JavaScript] On pb resize image</title>
    <content type="html">&lt;p&gt;The code is quite good to me.
&lt;br /&gt;Just some glitches to be corrected:
&lt;br /&gt; -- var apodimg  = document.getElementById(&amp;quot;apodimg&amp;quot;); 
&lt;br /&gt;    you use an hardcoded string in place of the parameters you passed to the function, I think it was an oversight.
&lt;br /&gt; -- your calculations need to be rounded as width and height properties need integer values.
&lt;br /&gt; -- I introduced a test on the type of the input parameter in order to let you choice to pass 
&lt;br /&gt;    an element id or a DOM element.
&lt;/p&gt;

&lt;pre&gt;function redim_img(apodimg)
{
   // you can pass a string with the id of the
   // image you want to resize or a reference 
   // to the image
   if (typeof(apodimg) == &amp;quot;string&amp;quot;) {
    apodimg  = document.getElementById(apodimg);
   }

    var w=apodimg.width;
    var h=apodimg.height;

    if (w&amp;gt;=h &amp;amp;&amp;amp; w&amp;gt;=200)	{
       h=Math.round(h*200/w);
       w=200;
    }
    if (h&amp;gt;w &amp;amp;&amp;amp; h&amp;gt;=160) {
       w=Math.round(w*200/h);
       h=160;
    }

    apodimg.style.width=w+'px';
    apodimg.style.height=h+'px';
}
&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1150-pb-resize-image/refactors/418777" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor405677</id>
    <published>2010-01-06T14:01:29-08:00</published>
    <title>[PHP] On Need help decoding ZEND php file</title>
    <content type="html">&lt;p&gt;It is not a matter of nice/very nice/unpleasant peoples.&lt;/p&gt;

&lt;p&gt;By the way, I want to be clear: I'm not pointing at you in any way,
&lt;br /&gt;just puntualizing...&lt;/p&gt;

&lt;p&gt;Posting the code on this site, although encoded, you distribute
&lt;br /&gt;it and doing this you are breaking the license you have to agree
&lt;br /&gt;with before downloading the software.&lt;/p&gt;

&lt;p&gt;I'm wondering if it is the case of modify the post in order to prevent
&lt;br /&gt;problems to the site owner.&lt;/p&gt;

&lt;p&gt;You can have the source code simply by paying an adeguate
&lt;br /&gt;(for the seller at least) license fee as clearly stated into
&lt;br /&gt;the download page. &lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.phpgrid.com/grid/license.php" target="_blank"&gt;http://www.phpgrid.com/grid/license.php&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1135-need-help-decoding-zend-php-file/refactors/405677" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor379730</id>
    <published>2009-12-07T19:42:33-08:00</published>
    <title>[PHP] On array to csv</title>
    <content type="html">&lt;p&gt;I think there $arraydata is an array of hashes. 
&lt;br /&gt;Here there is the code, assuming that the $arraydata fields names 
&lt;br /&gt;don't contain the enclosure character.&lt;/p&gt;

&lt;p&gt;reference: &lt;a href="http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm" target="_blank"&gt;http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm&lt;/a&gt;
&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php

function array_to_csv($arraydata, $delimeter=',', $linebreak=&amp;quot;\n&amp;quot;, $enclosure='&amp;quot;') {

  if (!is_array($arraydata)) {
    trigger_error('You must submit a valid array of csv data', E_USER_ERROR);
  }

  $innerSeparator = $enclosure.$enclosure;
  $fieldSeparator = $enclosure. $delimiter . $enclosure; //&amp;quot;,&amp;quot;
  $rowSeparator = $enclosure. $linebreak . $enclosure;   //&amp;quot;\n&amp;quot;
  $csv=array();

  // Format the header line
  $csv[] = join($fieldSeparator, array_keys($arraydata[0]));

  foreach ($arraydata as $row) {
    $foreach ($row as $k=&amp;gt;$v) {
       $row[$k]=str_replace($enclosure, $innerSeparator, $v);
    }
    $csv[] = join($fieldSeparator, $array_values($row));
  }

  return $enclosure . join($rowSeparator, $csv) . $enclosure;
}&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1120-array-to-csv/refactors/379730" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor370715</id>
    <published>2009-11-26T05:02:39-08:00</published>
    <title>[JavaScript] On Money formatting</title>
    <content type="html">&lt;p&gt;Just a divertissement.
&lt;br /&gt;It should be a good starting point, if not a solution.&lt;/p&gt;

&lt;p&gt;If I had more confidence with the regular expressions ....
&lt;br /&gt;Maybe there is a chance to avoid the initial padding.&lt;/p&gt;

&lt;pre&gt;function format_money(n) {
   var str, padlen;
 
   str = parseFloat(n).toFixed(2).toString(),
   
   padlen = str.length % 3;
   if (0==padlen) padlen=3;

   return (&amp;quot;000&amp;quot;.substr(padlen)+str).match(/(?:\d{3}\.\d{2}$)|(?:\d{3})/g)
        .join(',')
        .substr(3-padlen);
}


print(format_money(125.99))
print(format_money(1.99))
print(format_money(15000.99))
print(format_money(1500000.99))
print(format_money(15000000000000.99))
&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1111-money-formatting/refactors/370715" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor333318</id>
    <published>2009-10-05T18:33:17-07:00</published>
    <title>[C] On How to find max, min of three numbers?</title>
    <content type="html">&lt;p&gt;Hi Denis,
&lt;br /&gt;  I'd to perform some other tests before posting, bolow you will find the correct version,
&lt;br /&gt;  there is an additional test and swap in order to permit the &amp;quot;max&amp;quot; value to travel two 
&lt;br /&gt;  position down.
&lt;br /&gt;  
&lt;br /&gt;  You know, maybe the algorithm is not the best one you can write since it count on swapping
&lt;br /&gt;  only thre variables but I think, given the original limitation, is one of the more efficient.&lt;/p&gt;

&lt;p&gt;  Thanks for your annotations,
&lt;br /&gt;  Eineki&lt;/p&gt;

&lt;pre&gt;int max = rand(); // or any init value
int min = rand(); // or any init value
int tmp = rand(); // or any init value


if (max &amp;lt; min) {
  min ^= max ^= min ^= max;
}

if (tmp &amp;lt; min) {
  min ^= tmp ^= min ^= tmp;
}


if (max &amp;lt; tmp) {
  max ^= tmp ^= max ^= tmp;
}

// now min , tmp and max are ordered
&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/606-how-to-find-max-min-of-three-numbers/refactors/333318" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor219836</id>
    <published>2009-07-24T12:36:13-07:00</published>
    <title>[PHP] On Generate table from array</title>
    <content type="html">&lt;p&gt;Just for exercise, I've rewritten the two loops. The function become.
&lt;br /&gt;I don't know if it is more efficient, you should try (I can't do it now).&lt;/p&gt;

&lt;pre&gt;&amp;lt;?
/**
 * Generates a multi-column table from an array 
 *
 * @param array		Array to process 
 * @param columns	Number of columns to return  
 * @param id		(Optional) ID attribute to attach to the table
 * @param class		(Optional) CSS class attribute to attach to the table 
 */
function generate_table($array, $columns, $table_id='', $table_class='') {
	$table = &amp;quot;&amp;lt;table&amp;quot;;
	$table .= !empty($table_id) ? &amp;quot; id='$table_id'&amp;quot; : '';
	$table .= !empty($table_class) ? &amp;quot; class='$table_class'&amp;quot; : '';
	$table .= &amp;quot;&amp;gt;\n&amp;quot;;
	
	while (count($array)) {
                $row=array_pad(array_splice($array,0,$columns),$columns,'&amp;amp;nbsp');
		$table .='&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;'.join($row,'&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;').&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;\n&amp;quot;;
        }
	$table .= &amp;quot;&amp;lt;/table&amp;gt;\n&amp;quot;;	
	return $table;
}
?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/965-generate-table-from-array/refactors/219836" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor160344</id>
    <published>2009-06-02T10:41:03-07:00</published>
    <title>[PHP] On Std PHP Output + Variable</title>
    <content type="html">&lt;p&gt;maybe you can use the comma (,) instead of dot (.) you save the unnecessary concatenation (somewhere I read it is somewhat quicker)&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php

$name = &amp;quot;test&amp;quot;;

echo 'Die ist ein : ' , $name;

?&amp;gt;
&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/893-std-php-output-variable/refactors/160344" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor157995</id>
    <published>2009-05-17T13:57:06-07:00</published>
    <title>[PHP] On Retrieve username by user ID from two different tables.</title>
    <content type="html">&lt;p&gt;I'm not sure that the following code resolve your problems but I need more hints to better refactor your solution:
&lt;br /&gt;I found a lot of glitches in your code (I think it doesn't work) and I've tried to solve them but I was unable to do it.&lt;/p&gt;

&lt;p&gt;on line 26 of your code you call am_getlatestscores with a NULL parameter (value you don't use in your query). 
&lt;br /&gt;I suspect you have to pass to it a value (maybe a category?) but have no clues from where to get it.&lt;/p&gt;

&lt;p&gt;You use numeric values to address the columns of the recordset you obtain from am_getlatestscores so I had 
&lt;br /&gt;to guess the column names in order to obtain a query and retrieve your data.&lt;/p&gt;

&lt;p&gt;In short, consider the refactoring just a hint to put you on the right track. 
&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
    function am_getlatestscores($db, $cat) {
        $sql = &amp;quot;SELECT username, score FROM `AMCMS_highscores` JOIN `AMCMS_users` ON `primkey`=`userkey` WHERE `primkey`='$cat' ORDER BY `score` DESC LIMIT 10;&amp;quot;;
		$res = am_queries($db, $sql);
		return $res;
	}	

	function am_latesthighscores($db = NULL, $wholelinked = NULL, $list = NULL) {
		$numcats = am_numArcademGameCat($db, NULL);
		$res = am_getlatestscores($db, NULL);

		$scoreTemplate = '&amp;lt;a href=&amp;quot;index.php?cat=%s&amp;quot;&amp;gt;%s'
                       . ($wholelinked=='linkall'?'%s&amp;lt;/a&amp;gt;':'&amp;lt;/a&amp;gt;%s');
		
        if ($list == NULL) {
			$scoreTemplate = '&amp;lt;li&amp;gt;'.$coreTemplate.'&amp;lt;/li&amp;gt;';
        }

		foreach ($res as $record) {
			if ($wholelinked!='none') {
               $numgamecat = ' ('.$record[0].')';
            }
            retvalue[]=($list == 'flat')? $record[1]: sprintf($scoreTemplate, $record[1], $record[1], $numgamecat);
	}

        if ($list == 'flat') {
		  return $retvalue;
	}

        if ($list == 'bracket') {
          echo '[ '. implode(' | ', $retvalue) . echo ' ]';
        } else {
		  echo implode('', $retvalue);     
	}
   }
?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/875-retrieve-username-by-user-id-from-two-different-tables/refactors/157995" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor146762</id>
    <published>2009-02-11T09:03:40-08:00</published>
    <title>[Bash] On Unzip large database dump directly into mysql</title>
    <content type="html">&lt;p&gt;To me this solution is quite good, nothing to object :)&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/753-unzip-large-database-dump-directly-into-mysql/refactors/146762" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor146268</id>
    <published>2009-02-08T02:06:04-08:00</published>
    <title>[Bash] On sort | uniq alternative</title>
    <content type="html">&lt;p&gt;Your problem is read. It is a standard utility (not a bash built in command) and calling it consume a lot of time (so your script get slow).&lt;/p&gt;

&lt;p&gt;You should gain a lot of speed redirecting the standard input to awk with a single command: cat - (- stands for standard input).&lt;/p&gt;

&lt;p&gt;Try it and let me know if it helped
&lt;/p&gt;

&lt;pre&gt;#!/bin/bash
cat - | awk '!x[$0]++'

&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/739-sort-uniq-alternative/refactors/146268" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor141115</id>
    <published>2009-01-03T09:13:15-08:00</published>
    <title>[C#] On Which is more readable? Math.* or if (...)</title>
    <content type="html">&lt;p&gt;The first refactoring is, to me, a little confusing, I had to look at it a second time to be sure that x mantain his value if lesser equal than y. Maybe you can use the ternary operator to compact the if into a single liner.&lt;/p&gt;

&lt;p&gt;For the second refactoring, I prefer the version 2.2. It is terse as needed and more direct, clear and intuitive than the original version.&lt;/p&gt;

&lt;p&gt;By the way, this is just my point of view.&lt;/p&gt;

&lt;pre&gt;###Snippet 1.3: if-block proposed refactoring
int x, y;
x = x &amp;gt; y? y : x;
&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/692-which-is-more-readable-math-or-if/refactors/141115" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code684</id>
    <published>2008-12-25T08:59:13-08:00</published>
    <updated>2008-12-30T04:59:19-08:00</updated>
    <title>[Bash] Multi language refactor</title>
    <content type="html">&lt;p&gt;Can you rewrite this oneliner into another language of your choice?&lt;/p&gt;

&lt;p&gt;Thanks, Eineki&lt;/p&gt;

&lt;pre&gt;#!/bin/bash
echo -e &amp;quot;\n I wish you a merry Christmas and an happy new year! \n&amp;quot;
exit(0);&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/684-multi-language-refactor" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor121093</id>
    <published>2008-12-13T15:57:36-08:00</published>
    <title>[PHP] On Login / Registration - be easier?</title>
    <content type="html">&lt;p&gt;I don't think you can simplify the function, it has to perform a lot of simple test. 
&lt;br /&gt;We can just add a bit of syntax sugar using a case instead of the nested if and elseif in the register action.
&lt;br /&gt;For me it is a little more readable.
&lt;br /&gt;Maybe you should separate the view (that html part at the end of the code) from the controller part&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
ob_start();
include_once &amp;quot;include/config.php&amp;quot;;

$login = safe_chars($login);
$pass = safe_chars($pass);

if($action == &amp;quot;register&amp;quot;) {
  switch(true) {
    case ($login == ''):
         $error = &amp;quot;Nie podano loginu&amp;quot;;
         break;
    case (!ereg(&amp;quot;^[a-z0-9_\-]+$&amp;quot;, $login)):
         $error = &amp;quot;Niepoprawny login&amp;quot;;
         break;
    case (strlen($login) &amp;lt;= 2):
         $error = &amp;quot;Ten login jest za kr&#195;&#179;tki&amp;quot;;
         break;
    case (mysql_num_rows(mysql_query(&amp;quot;SELECT `login` FROM `users` WHERE `login`='$login' LIMIT 1&amp;quot;)) == 1):
         $error = &amp;quot;Ten login jest zaj&#196;&#8482;ty&amp;quot;;
         break;
    case ($pass == ''):
         $error = &amp;quot;Nie podano has&#197;&#8218;a&amp;quot;;
         break;
    case ($repass == ''):
         $error = &amp;quot;Nie podano ponownie has&#197;&#8218;a&amp;quot;;
         break;
    case ($pass != $repass):
         $error = &amp;quot;Musisz wpisa&#196;&#8225; identyczne has&#197;&#8218;a&amp;quot;;
         break;
    case ($mail == ''):
         $error = &amp;quot;Nie podano adresu e-mail&amp;quot;;
         break;
    case (!eregi(&amp;quot;^[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*@([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)*(\.[a-zA-Z]{2,4})$&amp;quot;, $mail)):
         $error = &amp;quot;Musisz wpisa&#196;&#8225; prawid&#197;&#8218;owy adres e-mail&amp;quot;;
         break;
    default:
         mysql_query(&amp;quot;INSERT INTO `users` SET `login`='$login', `pass`='&amp;quot;.md5($pass).&amp;quot;', `mail`='$mail', `ip`='$ip', `reg_date`=NOW()&amp;quot;);
}

else if($action == &amp;quot;login&amp;quot;) {
   $query = sprintf(&amp;quot;UPDATE `users` SET `ip`='%s', `last_login`=NOW() WHERE `login`='%s' AND `pass`='%s'&amp;quot;, $ip, $login, md5($pass))
  if(mysql_affected_rows(mysql_query($query)) != 1) // assume there is a single pair login/pass into db
    $error = &amp;quot;Niepoprawne dane&amp;quot;;
  else {
    $logged = 1;

} else if($action == &amp;quot;logout&amp;quot;) {
  $logged = 0;
  header(&amp;quot;Location: /&amp;quot;);
}

else {
?&amp;gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;
td {
	color: #808080;
	width: 150px;
}

span#error_msg {
	font-weight: bold;
	text-align: right;
	float: right;
	color: #ccc;
}
&amp;lt;/style&amp;gt;

&amp;lt;form action=&amp;quot;#&amp;quot; id=&amp;quot;form&amp;quot; method=&amp;quot;post&amp;quot; onsubmit=&amp;quot;loginRegister(); return false;&amp;quot;&amp;gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; id=&amp;quot;action&amp;quot; value=&amp;quot;login&amp;quot; /&amp;gt;
&amp;lt;p&amp;gt;Zaloguj si&#196;&#8482; lub
&amp;lt;a href=&amp;quot;#&amp;quot; onclick=&amp;quot;switchAction()&amp;quot;&amp;gt;zarejestruj&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;br/&amp;gt;

&amp;lt;table&amp;gt;
&amp;lt;tr&amp;gt;
	&amp;lt;td&amp;gt;LOGIN&amp;lt;/td&amp;gt;
	&amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;text&amp;quot; id=&amp;quot;login&amp;quot; maxlength=&amp;quot;15&amp;quot; /&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;

&amp;lt;tr&amp;gt;
	&amp;lt;td&amp;gt;HAS&#197;&#129;O&amp;lt;/td&amp;gt;
	&amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;password&amp;quot; id=&amp;quot;pass&amp;quot; /&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;

&amp;lt;div id=&amp;quot;register&amp;quot; style=&amp;quot;display: none;&amp;quot;&amp;gt;
&amp;lt;table&amp;gt;
&amp;lt;tr&amp;gt;
	&amp;lt;td&amp;gt;POWT&#195;&#8220;RZ HAS&#197;&#129;O&amp;lt;/td&amp;gt;
	&amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;password&amp;quot; id=&amp;quot;repass&amp;quot; /&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;

&amp;lt;tr&amp;gt;
	&amp;lt;td&amp;gt;EMAIL&amp;lt;/td&amp;gt;
	&amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;text&amp;quot; id=&amp;quot;mail&amp;quot; maxlength=&amp;quot;40&amp;quot; /&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;p&amp;gt;&amp;lt;span id=&amp;quot;error_msg&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;br/&amp;gt;
&amp;lt;br/&amp;gt;&amp;lt;input type=&amp;quot;submit&amp;quot; onclick=&amp;quot;loginRegister(); return false;&amp;quot; value=&amp;quot;OK&amp;quot; /&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/table&amp;gt;
&amp;lt;/form&amp;gt;
&amp;lt;? }
if(isset($error)) print $error;
?&amp;gt;
&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/665-login-register-be-easier/refactors/121093" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor88062</id>
    <published>2008-11-26T23:24:45-08:00</published>
    <title>[Java] On Factoring Integers</title>
    <content type="html">&lt;p&gt;The two functions, nextFactor and has nextFactor shared the common pattern I refactored into the findNextFactor() private method.
&lt;br /&gt;You have obfuscated the while conditions a bit but I'm sure you can see that the loop laying after the two sligthy different syntax are the same.&lt;/p&gt;

&lt;p&gt;Once you get this, the two method get straight simple.
&lt;br /&gt;I added a runtime exception to cover the case of a call of nextFactor method without a previous check of hasNextFactor&lt;/p&gt;

&lt;p&gt;I hope you can get through my english to the explanation :)&lt;/p&gt;

&lt;pre&gt;## Quick Fix [Java]

// line 44 while condition have to change to or you don't apply the last division 
//(but return the last factor everytime) =&amp;gt; number don't become 1 =&amp;gt; hasnextfactor misbehave.

while (factor &amp;lt;= number)

## Refactoring [Java]

public class factorGenerator
{
    private int number;
    private int factor;
    /**
     * Creates a FactorGenerator object used to determine the factor of an input value.
     * @param aNum is the input value
     **/
   public factorGenerator(int aNum)
   {
      number = aNum;
      factor = 2;
   }

   /**
    *  Find the next factor of a value.
    *  @return factor the next factor
    **/
   private int findNextFactor()
   {
   	while((factor &amp;lt; number) &amp;amp;&amp;amp; ((number%factor)!=0)) factor++;
   	return factor;
   }
		
   /**
    * Apply the next factor of a value.
    * @return factor the next factor
    **/
   public int nextFactor() throws RuntimeException
   {
       int result = findNextFactor();
       if (result &amp;lt;= number) {
	   number/= result;
   	   return result;
       } else 
	   throw new RuntimeException(); 	   	  
       }

   /**
      Determine whether or not there are more factors.
      @return true there are more factors
   */
   public boolean hasMoreFactors()
   {
	return (findNextFactor()&amp;lt;=number);
   }
}&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/625-factoring-integers/refactors/88062" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor78147</id>
    <published>2008-11-20T00:52:57-08:00</published>
    <title>[C] On How to find max, min of three numbers?</title>
    <content type="html">&lt;p&gt;It is quite simple, once you know.
&lt;br /&gt;All the trick is based upon the XOR operator.
&lt;br /&gt;the table of truth of xor is:
&lt;br /&gt; T ^ T = F
&lt;br /&gt; F ^ F = F
&lt;br /&gt; T ^ F = T
&lt;br /&gt; F ^ T = T&lt;/p&gt;

&lt;p&gt;I hope the explanation will be sufficient. 
&lt;br /&gt;Try by yourself with a piece of paper and a pencil a couple of time and you will find the solution very easy.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre&gt;/* Lets say we have two numbers: 27 and 145 (just two random values) into two separate variable */

int min = 27;  // 00011011 binary 8bit for 27  
int tmp = 145; // 10010001 

/* lets put the result into a third one */

int xored;
xored = min ^ tmp; // now contains 10001010

/* applying the table of xor you will find that starting
 * from xored you can obtain one of the value applying
 * the xor between xored itself and the other value. Let's try */

printf (&amp;quot;apply min (%d) to xored should obtain tmp (%d)\n&amp;quot;, min, xored ^ min); // xored ^ min is 10010001
printf (&amp;quot;apply tmp (%d) to xored should obtain min (%d)\n&amp;quot;, tmp, xored ^ tmp); // xored ^ tmp is 00011011

/* it is a well known property of xor used, for example, in 
 * computer security to share a secret between two or more actors
 * being sure that no guardian can reach the secret without the 
 * intervention of all the other ones */

/* now we can explode the totem: min ^= tmp ^= min ^= tmp
 * remember that you have to read the assignments right to left */

min = min ^ tmp;  // min becomes the xored signature;
tmp = min ^ tmp;  // xoring tmp versus the signature you obtain min original value and put it in tmp
min = min ^ tmp;  // min contains the signature, tmp contains the original value contained in min
                  // if you do the xor of the two values you obtain tmp original value.



&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/606-how-to-find-max-min-of-three-numbers/refactors/78147" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor76492</id>
    <published>2008-11-18T23:55:08-08:00</published>
    <title>[C] On How to find max, min of three numbers?</title>
    <content type="html">&lt;p&gt;Try this code, it was a kind of magic to me since I see it the first time in a programing class eons ago.&lt;/p&gt;

&lt;pre&gt;int max = rand(); // or any init value
int min = rand(); // or any init value
int tmp = rand(); // or any init value


if (tmp &amp;lt; min) {
  min ^= tmp ^= min ^= tmp;
}

if (max &amp;lt; tmp) {
  max ^= tmp ^= max ^= tmp;
}

// now min , tmp and max are ordered&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/606-how-to-find-max-min-of-three-numbers/refactors/76492" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor67535</id>
    <published>2008-11-11T10:49:54-08:00</published>
    <title>[PHP] On remove http from url string</title>
    <content type="html">&lt;p&gt;Hi, you can use preg_replace function to do the job in a single line.&lt;/p&gt;

&lt;p&gt;Reference for further investigations:
&lt;br /&gt;&lt;a href="http://www.php.net/manual/it/function.preg-replace.php" target="_blank"&gt;http://www.php.net/manual/it/function.preg-replace.php&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&amp;lt;?
/*
E.g:
$s = 'http://google.com';
echo remove_http($s);
output: google.com
*/

   function remove_http($url='') 
   {
       return preg_replace(&amp;quot;/^https?:\/\/(.+)$/i&amp;quot;,&amp;quot;\\1&amp;quot;, $url);
   }

?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/598-remove-http-from-url-string/refactors/67535" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor67087</id>
    <published>2008-11-11T00:26:52-08:00</published>
    <title>[PHP] On Get Next Turn</title>
    <content type="html">&lt;p&gt;You are right, $next_seat had a wrong starting value. This version should work. &lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
function get_next_seat($table_id, $start_seat) {
  $player_data=$this-&amp;gt;admin-&amp;gt;get_player_data($table_id);

  $maxint = intval(sprintf(&amp;quot;%u&amp;quot;,-1));
  $first_seat = $start_seat;
  $next_seat  = $maxint;

  foreach ($player_data[&amp;quot;seats&amp;quot;] as $seat_id =&amp;gt; $data) {
    if ($data[&amp;quot;player_id&amp;quot;] !=0) {
      if ($seat_id &amp;lt; $first_seat) $first_seat = $seat_id;
      if (($seat_id &amp;gt; $start_seat) and ($seat_id &amp;lt; $next_seat)) $next_seat = $seat_id;
    }
  }

  return $next_seat &amp;lt; $maxint? $next_seat : $first_seat;
}
?&amp;gt;
&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/550-get-next-turn/refactors/67087" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor63738</id>
    <published>2008-11-08T20:34:06-08:00</published>
    <title>[PHP] On Get Next Turn</title>
    <content type="html">&lt;p&gt;You are right, $next_seat had a wrong starting value. This version should work. &lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
function get_next_seat($table_id, $start_seat) {
  $player_data=$this-&amp;gt;admin-&amp;gt;get_player_data($table_id);

  $first_seat = $start_seat;
  $next_seat  = intval(sprintf(&amp;quot;%u&amp;quot;,-1));

  foreach ($player_data[&amp;quot;seats&amp;quot;] as $seat_id =&amp;gt; $data) {
    if ($data[&amp;quot;player_id&amp;quot;] !=0) {
      if ($seat_id &amp;lt; $first_seat) $first_seat = $seat_id;
      if (($seat_id &amp;gt; $start_seat) and ($seat_id &amp;lt; $next_seat)) $next_seat = $seat_id;
    }
  }

  return $start_seat &amp;lt; $next_seat? $next_seat : $first_seat;
}
?&amp;gt;
&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/550-get-next-turn/refactors/63738" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor56268</id>
    <published>2008-10-26T23:00:22-07:00</published>
    <title>[PHP] On Array To Tablerows</title>
    <content type="html">&lt;p&gt;cool version the one liner one. missed the fifth star just for a mouse glitch&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/559-array-to-tablerows/refactors/56268" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor56267</id>
    <published>2008-10-26T22:57:38-07:00</published>
    <title>[PHP] On Array To Tablerows</title>
    <content type="html">&lt;p&gt;My previous version shortened is only 4 lines. I prefer the other one though&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
function array_to_tablerows($array = array(), $cols = 2) {
   if (!is_array($array) || $cols &amp;lt; 1) return false;
   for ($i=0, $tbl='', $len = count($array), $i&amp;lt;$len; $i+=$cols) 
         $tbl .= '&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;'. implode('&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;',array_slice($array, $i, $cols)) . '&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;';
   return ($len % $cols) ? substr($tbl,0,strlen($tbl)-5).'&amp;lt;td colspan=&amp;quot;'.($cols-($len % $cols)).'&amp;quot;&amp;gt;&amp;amp;nbsp&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;' : $tbl;
}
&lt;/pre&gt;</content>
    <author>
      <name>Eineki</name>
      <email>magisano@cs.unibo.it</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/559-array-to-tablerows/refactors/56267" rel="alternate"/>
  </entry>
</feed>

