RSS | Archive | Random | E-mail

About

Hi, I'm Christian Castelli, a 28 years old italian programmer located in Pisa (Italy). Here I post small snippets of code which can be useful in my work.

Links

Codepuzzling main site
Development site
ByteStrike italian blog
Follow me on Twitter

My Life Style

while(passion) {
  try {
    myLife.run();
  }catch(LifeExceptions) {  
    stronger++;
    continue;
   }
}

Following

17 November 09

[Links] Online services and tools for regex testing

Regular expressions are a very powerful weapon for the developer if he can deals with all the pain of testing, debugging and learning the quirks of this matter. I’m collecting some links to be red one day, when I’ll got time to dive more deeply in the argument:

Comments (View)
Posted: 12:07 PM
placidiappunti:


caturday:

LCD (Lot of Cats Display)

placidiappunti:

caturday:

LCD (Lot of Cats Display)

Reblogged: placidiappunti

Comments (View)
21 October 09

[PHP] Resources for programming with PDO

I’ve asked Twitter user what kind of PHP class would have used for a project and I was (and I am) in doubt between MDB2 (a PEAR Abstraction layer class) and mysqli extension. Someone has pointed out PDO, so I’m collecting some links to see what does this extension do:

Comments (View)
5 October 09

[JS] How to get a querystring value given the key

Suppose you have an URL like this: www.host.com?m=5&lang=it. If you want to retrive the value of lang, you could simply use this function:

function parseURL(url,key)
{
  var reQS = new RegExp("[?&]"+key+"=([^&$]*)", "i");
  var offset = url.search(reQS);
  return ( offset >= 0 ) ? RegExp.$1 : null;
}

// usage
var myKey = parseURL("www.host.com?m=5&lang=it","lang")
Via blog.falafel.com

Comments (View)
Posted: 4:54 PM

Reblogged: placidiappunti

Comments (View)
22 September 09

[JS] How to get the time and unixtime

Self explanatory code:

var T = {
 getUnixTime: function() {
   return parseInt(new Date().getTime().toString().substring(0, 10));
 },
	
// format hh:mm:ss
getTime: function() 
{
  var d= new Date();
  var time = new Array();
  var hours = d.getHours();
  time.push(hours);
  var minutes = d.getMinutes().toString().length < 2 ? "0"+d.getMinutes() : d.getMinutes(); 
  time.push(minutes);
  var seconds = d.getSeconds().toString().length < 2 ? "0"+d.getSeconds() : d.getSeconds(); 
  time.push(seconds);
  	
  return time.join(":");
 }
}	
Update: I’ve just discovered that Syntax Highlighter code doesn’t work well with < > symbols in javascript code….

Comments (View)
Posted: 10:15 AM

[JS] How to extend Array object to get the index of an its value

The code is self explanatory: you have an array and you want to know what index has the element “pippo” or “5”. So I extended the array object with prototype keyword to scan the array and to return the index if found, -1 otherwise.

var points = [56,12,36];

Array.prototype.getIndex = function(aVal) 
{
   for(item in this)
     if(aVal == this[item])
       return item;
   
   return -1;
}

console.log(points.getIndex(36));
If you have Firefox or Safari (maybe in IE8 too, I dunno), watch at the console and you’ll see2 as result.

Comments (View)
17 September 09

[Bash] How much memory consume a process

ps -aylC geany |grep "geany" |awk '{print $8'} |sort -n |tail -n 1

Tags: bash memory awk
Comments (View)
15 September 09

[MySQL] Using information_schema instead of SHOW commands

PHP can handle data returned by SHOW commands, but information about all databases and tables are stored in information_schema database. Some useful queries:
1) Getting only columns name:

SELECT `column_name` 
FROM information_schema.columns
WHERE `table_name`="mytable";
The SHOW statement show columns from t5_user; shows extra information that must be filtered with PHP. In example if you want to know type, keys and so on for a particular field, it’s easy to use:
SHOW columns 
FROM table 
WHERE field = "myfield"
2) Getting last ID inserted, the number of records and the engine of a table:
select table_name as tables, engine,            
          table_rows as records, 
         auto_increment-1 as lastId  
FROM information_schema.tables  
WHERE table_name = "t5_user";
SHOW TABLE STATUS can retrieve the same information but from a shell it’s not so easy to read that bunch of data (yes you can use \G instead of ; at the end of the query to make data more readable). Obviously the last id can be retrieved with this simple query;
SELECT id 
FROM  MYDB.MYTABLE 
ORDER BY id DESC LIMIT 1
3) Getting the “heaviest” tables (those over 99.99 MB) in the entire DBMS
SELECT concat( table_schema, '.', table_name ) table_name, 
              concat( round( data_length / ( 1024 *1024 ) , 2 ) , 'M' ) data_length, 
              concat( round( index_length / ( 1024 *1024 ) , 2 ) , 'M' ) index_length, 
              concat( round( round( data_length + index_length,2 ) / ( 1024 *1024 ) , 2 ) , 'M' ) total_size 
FROM information_schema.TABLES 
HAVING data_length LIKE "___.%" 
OR data_length LIKE "____.%" 
ORDER BY data_length DESC ;
Comment to improve this query (Gigabyte consideration, ecc) are very welcomed.

Comments (View)
10 September 09

[jQuery] How to use Prototype or MooTools with jQuery

Since many libraries make use of the $ identifier, we need a way to prevent collisions between these names. jQuery provides a method called .noConflict() to return control of the $ identifier back to other libraries. Typical usage of .noConflict() follows the following pattern:

First, the other library (Prototype in this example) is included. Then, jQuery itself is included, taking over $ for its own use. Next, a call to .noConflict() frees up $, so that control of it reverts to the first included library (Prototype). Now in our custom script we can use both libraries—but whenever we need to use a jQuery method, we need to use jQuery instead of $ as an identifier.

Comments (View)
Themed by Hunson. Originally by Josh