Saturday, January 30, 2010

Who reads this blog ?

This is the map made by google analytics on this blog.
It's rather foreseeable to see which are the countries more interested in a technical blog.
I'm only surprised to see no visits from China and Norway, as well as same number of visitors from Spain and Ukraine and Romania.



To obtain a better paid job I'm really thinking about moving to Madagascar :D

PHP for file moving / managing

PHP is a technology damned cool for web application but the language doesn't provide an advanced syntax as Python in itself. However, by writing readable and maintenable code (for instance by smartly separating features in function and classes), PHP is satisfying also to make file managing / system scripts, IMHO of course :)

Example
Here is a simple script I'm using to copy the latest mp3 files from a directory to another one (ipod shuffle) skipping the already existing files. Basically it's a kind of one-way-sync script for files filtered by mask and creation date. (To run it from the command line, launch it from the destination directory. e.g.: "php -f script.php" in the root of the ipod, or any USB MP3 player)



define('_SOURCE_PATH_', 'd:/documents/Music/incoming/');
define('_DEST_FOLDER_' , 'Music/'); #ipod shuffle

foreach (new DirectoryIterator(_SOURCE_PATH_) as $f) {

if ( Utils::is_recent_mp3( $f->getPathname() ) &&
Utils::copy_if_not_exists( $f->getPathname(), _DEST_FOLDER_.$f->getFilename() )
)
echo "[COPIED][{$f->getFilename()}]\n";

}




class Utils {

public static function is_recent_mp3($path, $daysOld = 7) {
return ( substr($path,-1)=='3' && filemtime($path) > ( time()-$daysOld*24*3600) );
}

public static function copy_if_not_exists($from, $to){
return ( file_exists($from) && !file_exists($to) ) ? copy($from, $to) : false;
}

}

Tuesday, January 26, 2010

Simple PHP script to edit front end components

I've recently built a very simple web site. The only dynamic requirement was having some parts of the front end editable (WYSIWYG) by an administration area (user and password). Basically, a very simple basic CMS for one user.

Solution
A MySQL database is not needed, saving to files is much more appropriate for these requirements. Also SQLite is not necessary, as we just want to do only basic operations.
To do it, I've used one of my scripts that meets all of those requirements using only one file (the script, to place in any position, better a separate "admin" folder) + a config file (basic options: array with the list of the frontend component and their relative paths, user and password to login) + FCK editor files.

The script (protected with a simple session control + form to login) basically lists the components and allows to click on them and change the text inside using FCK editor (free WYSIWYG html editor). Also a simple backup system is provided, by keeping/restoring the previous version saved.

It's quite easy to implement a script like that.
To whom may be interested in my version (<200 LOC, made long time ago, so PHP4 compliant), let me know.

Wednesday, January 20, 2010

My Zend Certification

Just passed my ZCE PHP5!!



90 tech questions, some of them answerable only by reading some books, some others by knowing the exact behaviour and arguments of PHP5 functions and settings (quite a lot of stuff to remember considering also XML, PDO, STD lib,streams and filters), some other only with work experience, some of them with a code/algorithm comprehension ability and deep OOP skills.

It will may be more useful than my master's degree in my CV :)

Curiosity: how many certified engineer PHP5 are there at the moment in :
Germany: 338, UK: 251, France:222, Italy:53, Spain: 40, Ireland:35
USA:746, India:128, Canada: 114, China: 28


Thursday, January 14, 2010

PDO example


try {
$pdo = new PDO("mysql:host=127.0.0.1;dbname=dev;","root","" );
#$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$pdo->exec("DELETE FROM `User` WHERE id<>293968 ");

#insert
$insertedRows = $pdo->exec("INSERT INTO `User` (username, password) VALUES ('elvis','ciotti') ");
$lastInsertId = $pdo->lastInsertId();
echo "Inserted $insertedRows rows [id = $lastInsertId]\n";
$pdo->exec("DELETE FROM `User` WHERE id=$lastInsertId ");

#fetch
foreach ($pdo->query("SELECT * FROM `User` ", PDO::FETCH_ASSOC) as $row){
echo "{$row['id']}:{$row['username']}\n";
}

#insert con statement ":"
$stmt = $pdo->prepare("INSERT INTO `User` (username, password) VALUES (:username, :password) ");
$stmt->bindParam(':username',$user);
$stmt->bindParam(':password',$pass);
#
$user='aaaa'; $pass='aa'; $stmt->execute();
$user='bbbb'; $pass='bb'; $stmt->execute();

#insert con statement "?"
$stmt = $pdo->prepare("INSERT INTO `User` (username, password) VALUES (?, ?) ");
$stmt->bindParam(1,$user);
$stmt->bindParam(2,$pass);
#
$user='cccc'; $pass='cc'; $stmt->execute();
$user='dddd'; $pass='dd'; $stmt->execute();

#insert con execute ":"
$stmt = $pdo->prepare("INSERT INTO `User` (username, password) VALUES (:username, :password) ");
$stmt->execute(array(":username"=>"eeee",":password"=>"ee"));

#insert con execute "?"
$stmt = $pdo->prepare("INSERT INTO `User` (username, password) VALUES (?, ?) ");
$stmt->execute(array("ffff","ff"));

#fixed val, no vars
$stmt->bindValue(1,"gggg");
$stmt->bindValue(2,"gg");
$stmt->execute();


$stmpt_all = $pdo->prepare("SELECT * FROM `User` LIMIT 10");
$stmpt_all->execute();
#echo "all: ".print_r($stmpt_all->fetchAll(),1);

$stmpt_all = $pdo->prepare("SELECT id, username, password FROM `User` LIMIT 10");
$stmpt_all->bindColumn(1,$id);
$stmpt_all->bindColumn(2,$user);
$stmpt_all->bindColumn(3,$pass);
$stmpt_all->execute();
echo "fetch: {$stmpt_all->rowCount()} rows: \n";
while($row = $stmpt_all->fetch(PDO::FETCH_BOUND)) { #foreach
echo " [$id][$user][$pass]\n";
}


} catch (PDOException $e){
echo $e->getMessage();
}

?>
 

PHP and tips|PHP