Archive for the 'Snippets' Category

18th Jan 2008

Get directory size using Linux command

du -hs /var/www/

You may omit the directory path if you’d like to find out the size of the current directory that you are in.
du -hs

Posted in Snippets | No Comments »

19th Sep 2007

Export into excel with formula in PHP

In PHP, exporting data into an excel file (.csv) can be achieved very easily by sending a raw HTTP header. And having a formula in cells is no brainier either (and I only found that out after searching through google and not finding any solution - hehe).

The following is a rough example of how you do it. What I am trying to do here as an example is have an excel formula in Age column to calculate the person’s age automatically.

        header('Content-Type: application/csv');
        header('Content-Disposition: inline; filename="report.csv"');  

        // print column titles
        echo 'Name,Year born,Age' . "n";  

        // a full numeric representation of a year, 4 digits; eg: 2007
        $year = date('Y'); 

        // $count starts from 2 because the first row is reserved for column titles
        $count = 2;  

	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
 	    echo $row['name'] . ',';
            echo $row['year'] . ',';
            // formula = Current year - Bx(Year born)  B is the column position
            echo '=' . $year . '-B' . $count;
            echo "\n";
            // increment $count by 1 (same as $count = $count + 1)
            $count++;
        }
        exit;

Technorati Tags: , , ,

Posted in Snippets, PHP | No Comments »

19th Aug 2007

Removing file extension via .htaccess

Problem:
You have the following URLs for your website:
www.example.com/about-us.html
www.example.com/services.html
www.example.com/contact-us.html

However, you would like to hide file extensions from the end users, and allow them to access to the files using the following URLs:
www.example.com/about-us
www.example.com/services
www.example.com/contact-us

Solution:
The solution can be achieved by using Apache’s mod_rewrite. Create an .htaccess file in your website root directory with the following content.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^.]+)\.html$ $1 [L]
# Replace html with your file extension, eg: php, htm, asp

Benefits:
- Search engine friendly
- Easier to read and remember
- Extension/environment independent; when you change the technology used for your website (for eg: from using asp to php), you can be assured that all the links and bookmarks will still work.

Technorati Tags: , , , ,

Posted in Snippets | 1 Comment »

23rd Jul 2007

Convert to sentence case in MySQL

UPDATE `tablename`
SET `fieldname` =  CONCAT(UCASE(SUBSTRING(`fieldname`,1,1)),'', 
LCASE(SUBSTRING(`fieldname`,2,LENGTH(`fieldname`))))
WHERE `id` = 1

Posted in Snippets | No Comments »

14th Jul 2007

Country drop down menu

Are you building a form where you have a drop down list of countries for users to choose from? If you are, you might find the following snippet handy.

(more…)

Posted in Snippets, xHTML | No Comments »