Saturday, March 17, 2012

Bash(shell prompt) server name, time and other options

Edit ~/.bashrc

Change bash(shell prompt) hostname and add a custom server name:
 #export PS1='\h:\w\$ '
export PS1='my-server:\w\$ '

Add bash(shell prompt) time:
export PS1='[\t] my-server:\w\$ '

Apply changes:
Logout ( CTRL + D ) and login again.

Other available options:
\a ASCII bell char
\d current date
\e ASCII escape char
\h hostname (until first ".")
\H hostname
\n new line
\r carriage return
\s shell name
\t current time 24H
\T current time 12H
\@ current time 12H AM/PM
\u username
\v bash version
\V bash version + patch level
\w current working directory
\W basename of the current directory
\! history number of this command
\# number of the command
\$ "$" (if UID is not 0), "#" (if UID is 0)
\\ "\"
\[ begin sequence nonprinting chars
\] end sequence nonprinting chars

Friday, March 16, 2012

Javascript objects and classes; private and public methods and properties

1) Object literals

var Object = {
   
    speed : 0,
   
    setSpeed : function(speed) {
        this.speed = speed;
    },
   
    getSpeed : function() {
        return this.speed;
    }
   
}

Example:
Object.setSpeed(150);
var speed = Object.getSpeed();

All properties and methods of the object literal are public.

2) Classes

function Class1() {
   
    // private property example
    var name = 'Random name here';
   
    // public property example
    this.speed = 0;


    // private method example
    function getName() {
        return name;
    }
   
    // public method example
    this.setSpeed = function(speed) {
        this.speed = speed;
    }
   
    // public method example
    this.getSpeed = function() {
        return this.speed;
    }
   
   
}

Example:
var Object1 = new Class1();
Object1.setSpeed(150);
var speed = Object1.getSpeed();

MySQL Error: Got a packet bigger than 'max_allowed_packet' bytes

Command:
mysql -u root -ppassw db_name < db.sql

Error:
MySQL ERROR 1153 (08S01) at line 35977: Got a packet bigger than 'max_allowed_packet' bytes

FIX/SOLUTION:
1) Edit db.sql and add:
set global net_buffer_length=1000000;
set global max_allowed_packet=100000000;

2) Run:
mysql --max_allowed_packet=100000000 -u root -ppassw db_name < db.sql

Tuesday, February 21, 2012

jQuery document ready

$(document).ready(function() {
    // ... code to run after the page loads
});

PHP - number formatting using letters

Number formatting using letters, in PHP:

define('PR_NFORMAT_DEC_SEPARATOR', '.'); // decimals separator
define('PR_NFORMAT_THS_SEPARATOR', ','); // thousands separator

/**
 *
 * Formatting numbers using letters; ex: 1066345732 becomes 1.07B
 * @param float $fValue
 * @param int $nSupportedDecimals
 * @return formatted string
 */
function customNumberFormatLetters($fValue, $nSupportedDecimals = 2) {
   
    if(!preg_match('`^[0-9\.]+$`', $fValue)) {
        return $fValue;
    }
   
    // get rid of decimals
    $fValue = round($fValue);
   
    // supported cases !!! order is IMPORTANT !!! the cases are teste in this order
    $aCases = array(
        array('no_chars' => 9, 'letter' => 'B'), // billions
        array('no_chars' => 6, 'letter' => 'M'), // millions
    );
   
    $sNumberNice = $fValue;
   
    // test suppoort cases
    foreach($aCases as $aCase) {
        if($fValue > (int)str_repeat('9', $aCase['no_chars'])) {
            $fValue = $fValue / pow(10, $aCase['no_chars']);
            if($nSupportedDecimals > 0) {
                $fValue = $fValue * pow(10, $nSupportedDecimals);
                $fValue = round($fValue);
                $fValue = $fValue / (pow(10, $nSupportedDecimals));
            } else {
                $fValue = round($fValue);
            }
            $sNumberNice = number_format($fValue, $nSupportedDecimals, PR_NFORMAT_DEC_SEPARATOR, PR_NFORMAT_THS_SEPARATOR);
            $sNumberNice = eraseEndingZeros($sNumberNice);
            $sNumberNice .= $aCase['letter'];
            break;
        }
    }
   
    return $sNumberNice;
}

function eraseEndingZeros($fValueFormatted) {
    // erase all ending zeros:
    $aNparts = explode(PR_NFORMAT_DEC_SEPARATOR, $fValueFormatted);
    if(count($aNparts) == 2) {
        if(strlen($aNparts[1]) === 0 || preg_match('`^0+$`', $aNparts[1])) {
            $fValueFormatted = $aNparts[0];
        } elseif(strlen($aNparts[1]) === 1 && (string)$aNparts[1] !== '0') {
            $fValueFormatted = $aNparts[0] . PR_NFORMAT_DEC_SEPARATOR . $aNparts[1];
        } else {
            $aNparts[1] = preg_replace('`^([0-9]+)([1-9])(0+)$`', '$1$2', $aNparts[1]);
            $fValueFormatted = $aNparts[0] . PR_NFORMAT_DEC_SEPARATOR . $aNparts[1];
        }
    }
    return $fValueFormatted;
}

Saturday, February 11, 2012

Eclipse - adjust indentation when pasting

Fix Eclipse indentation problem:

Preferences -> Java -> Editor -> Typing -> When pasting: Adjust indentation

PHP: singleton pattern implementation

PHP Singleton implementation:

class MyClass {

static private $instance = null;

public function __construct() {
self::$instance = $this;
}

public function getInstance() {
if(!self::$instance) {
$class_name = __CLASS__;
self::$instance = new $class_name;
}
return self::$instance;
}
}