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

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)
Posted: 12:11 PM

[jQuery] How to add a CSS class to external links

 // apply a class to all <a> which respect the filter selection.
  $('a').filter(function() {
    return this.hostname && this.hostname != location.hostname;
  }).addClass('external');

Comments (View)
Posted: 11:05 AM

[MySQL] How to install mytop on CentOS 5.3

For the ones who don’t know what mytop is, it’s a simple perl script that shows mysql’s porcesses list (SHOW PROCESSLIST) like top does for system’s processes. First of all, let’s assure what distro you’re using by typing:

[root@localhost ~]# cat /etc/issue
CentOS release 5.3 (Final)
Kernel \r on an \m
Then let’s import GPG key:
rpm --import http://dries.ulyssis.org/rpm/RPM-GPG-KEY.dries.txt
Then we download mytop through rpm command (the link is shortened through bit.ly, if rpm doesn’t follow redirect, use normal url) and install via yum:
rpm -Uhv http://bit.ly/DVVs1
yum install rpm
Then you can use an alias in your bashrc file for not inserting all the times user and password:
vim ~/.bashrc
alias mytop='mytop -uuser -ppass'
.

Tags: mysql rpm top
Comments (View)
Themed by Hunson. Originally by Josh