Saturday, August 30, 2008

Pastebin Integration

Hello all!

i am proud to announce that in future, a copy of code examples will also be on Pastebin at http://thephpcode.pastebin.com/. This will allow all to post your source codes up there and discuss it here on thephpcode.blogspot.com.

This also means that, you can see the codes clearly and find help directly when you click from the functions in the source code. and with the added usefulness of line numbers, you can look at the code clearly.

hope it helps all.

cheers
sam

Tuesday, August 12, 2008

Getting a recursive file list in PHP: Code Example

Ever wonder how you can get an array of files in a directory. ALL the files, including those in the subfolders? Sure you can do that! an example is shown below! The function below returns an array tree of files. It recursively get files in a folder, then calls itself again for all the sub folders.

function dirfiles($dirname = '.', $relative = false) {
if($dirname == ''){$dirname = '.';}
if (!is_dir($dirname) || !is_readable($dirname)) {
// check whether the directory is valid.
return false;
}

$a = array();

$handle = opendir($dirname);
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..' && is_readable($dirname . DIRECTORY_SEPARATOR . $file)) {
if (is_dir($dirname . DIRECTORY_SEPARATOR . $file)) {
$a[($relative ? $file : $dirname . DIRECTORY_SEPARATOR . $file)] = dirfiles($dirname . DIRECTORY_SEPARATOR . $file, $relative);
}else{
$a[] = ($relative ? $file : $dirname . DIRECTORY_SEPARATOR . $file);
}
}
}
closedir($handle);

return $a;
}


You can specify whether to return each array in an relative manner or not.

$relative = true:
Array
(
[data] => Array
(
[0] => dataone.inc.php
[1] => user.inc.php
[2] => forum.inc.php
)

[0] => constants.inc.php
[1] => functions.inc.php
[2] => header.inc.php
)


$relative = false:

Array
(
[dat/inc/data] => Array
(
[0] => dat/inc/datadataone.inc.php
[1] => dat/inc/datauser.inc.php
[2] => dat/inc/dataforum.inc.php
)

[0] => dat/inc/constants.inc.php
[1] => dat/inc/functions.inc.php
[2] => dat/inc/header.inc.php
)

For ($relative = true), the presentation would be nicer, however in ($relative = false), the array would be more useful for argument of another file access function.

hope it is useful ^^

Link to code example on pastebin: http://thephpcode.pastebin.com/f495b59a1

Sunday, August 3, 2008

Why JSON over XML in AJAX?

Many web applications nowadays use AJAX to help them in decreasing server load and of course speed up the website. AJAX have been part of the web 2.0 technologies and is used widely in many websites.

AJAX have this idea of calling back the server from the client side to accomplish some task, such as posting back data to the server or getting new information from the server. it can been seen as form submitter, tips of the day ticker, tabbed boxes and more.

However, developers have the choice of language of the message that is responded from the server after the postback or request have been made by AJAX from client side. It can either be in the form of Javascript Object Notation (JSON) or Extensible Markup Language (XML).

JSON generally have advantages over XML - firstly because JSON is much lightweight compared to XML. As JSON only contains one character notations, it saves more bandwidth and load time compared to XML which uses tags.

Secondly, the multiple-dimension JSON is also attractive to developers, as JSON's parsing on PHP is easy, with only 2 functions, whereas XML uses multiple functions to parse - which may be confusing.

Thirdly, JSON is directly readable on client side using Javascript, thus you save time parsing it on server AND client side. For XML, you will need to parse the data again in order to use it.

To use JSON directly in Javascript: var myObject = eval('(' + myJSONtext + ')');

For course, the X in AJAX originally refers to XML - asynchronous JavaScript and XML - but as time goes by, developers will find satisfying format for their needs of communication between server and client. Even in ASP.net, AJAX messages in Updatepanels and other controls are split using "|".