PHP: empty() is your friend

The right question is: Is it empty?

It’s easy to miss things that are very obvious, this is the proper way to check that a variable has a value that is not nothing (null, 0, FALSE, “false”, array(), etc) without getting PHP E_NOTICE messages (that I encourage you to show when developing):

// Check that $var is set and is not empty:

// 1. The WRONG way
  // ... 

// 2. The LONG way
if(isset($var) && $var){
  // ...

// 3. The I ROCK way
  // ...

MySQL tip: Update a column with random values

Random string in MySQL
Random string in MySQL

I’m always impressed with what you can do with MySQL (my favorite RDBMS), not long ago I needed to add a column in a table and fill it with some random values. Instead of using a PHP (could be C, Java, perl…) script that goes row by row and calculates a random value, I thought it would be challenging to do it with a single SQL query.

Generate a random character

This query returns a random character from the string chars_str, replace chars_str with a string containing the characters you want to allow:

SELECT SUBSTRING(chars_str , 1+ FLOOR(RAND()*LENGTH(chars_str)) ,1);


SELECT SUBSTRING('01234567' , 1+ FLOOR(RAND()*LENGTH('01234567')) ,1) AS random_octal;
| random_octal |
| 6            |

Generate a random string of characters

In order to generate a random string we need to generate many random characters, thus we need to repeat the previous query, or we can simply create a table and have the query executed on each rows of that table, MySQL will generate for us one random character per table row and we’ll concatenate them (with GROUP_CONCAT [...] SEPARATOR '' )to produce our final random string:

  • This query returns a random string constitud of characters from the string chars_str
  • Replace chars_str with a string containing the allowed characters
  • The string length is the length of dummy_tbl which corresponds to the number of SELECT you have in the FROM
FROM (SELECT 1 /* UNION SELECT 2 ... UNION SELECT n */) as dummy_tbl;

-- Example

| random_binary |
| 0111          |

Update a column with random values

Now need to update the column of our database with random values, we will do so by nesting the query that generates random values into a normal UPDATE statement

-- column_name.tbl_name is the column we want to update

UPDATE tbl_name SET column_name = (
  FROM (SELECT 1 /* UNION SELECT 2 ... UNION SELECT n */) AS dummy_tbl

-- Example

UPDATE tickets SET code = (

There you have it, there is probably a better –and more of less complicated– way to do it, you may want to try doing the same thing with procedures, if you do so, please share your results on the comments section.

What is MVC?

The MVC design pattern separates business logic from user interface

Model–View–Controller is a design pattern or a software engineering concept that separates the model, the view and the controller from each others, thus dividing the labour in your application in such a way that:

The model is the brain, the knowledge, or simply data and means to using it:

  • it knows where data is, how to access it and how to change it
  • it responds to requests about data state
  • it responds to instructions to change data state

The view holds the presentation logic like templating and caching, in a PHP MVC application it may range from a few  html templates with PHP tags for inserting request specific data to a bunch of templates using a templating engine such as Smarty.

The controller acts as the mediator, like a  front desk agent or the guy that takes your order and serves your dish at a McDonald’s:

  • it receives the client (visitor’s browser) input in the form of GET or POST requests
  • it could possibly verify and validate user input
  • it instructs a the model to execute a specific action depending on the input it received

The Key benefits of using MVC

Separation of concerns

By separating the data logic (the model), the presentation logic (the view) and the application logic (the controller) you make sure that there is no interference between the three allowing you to work on and optimize each one dependently from the other two.

Let’s assume a scenario where we have a data driven website that we made with MVC in mind, this website needs a graphical redesign, we’ll simply hire an excellent web designer, which happens to be not so good at programming but we don’t care, all he will see is the website template files (our views).

Now we heard that PostgreSQL offers better performance and we want to switch from X to PostgreSQL, fine, all we have to do is migrate our database and modify our model, of course we will not modify a single line on the controller or view logic.

Code reuse

Data returned by the model  is “pure” data, free from any formatting or presentation, when you need to present data to the user you write a view or more if you are offering the same data in many formats (HTML, XML, file for download…). The model is written only once and is reused by the views, there is no code duplication and you don’t waste your time rewriting code that does the same or almost the same thing.

By using MVC, you will end up having a library of well organized components that you can reuse on the same application and in other applications as well, this will drastically increase your productivity.

Drupal Views: Show count

The view results count is stored in the view object and you can display it by adding a PHP header or footer to your view. In order to have PHP in views you need the module Views PHP, so install it first then add a header or footer with this code.

// Dispaly count
$view = views_get_current_view();
echo '

'.$view->total_rows.' result(s)'.'

'; ?>

Drupal: Send email

This is a quick and easy solution to send an email from Drupal using PHP, i.e a custom module. you will do it in 2 steps (I assume your module is called “mod”):

  • Define mod_mail() (implementing hook_mail() ), this function will prepare the email
  • Send the email using drupal_mail()

Define mod_mail()

 * Implementation of hook_mail.
function mod_mail($key, &$message, $params) {
  switch ($key) {
    case 'basic':
      $message['subject'] = t('Greetings');
      $message['body'] = array(t('I just wanted to say hello!'));
    case 'advanced':
      $message['subject'] = t('Hello @username', array('@username' => $params['username']));
      $message['body'] = array(t('Thank you for joining @site_name!', array('@site_name' => $params['site_name'])));
      ; /* Do nothing */

Sending the email

In order to send the email you need to decide on 2 parameters (optionally, a 3rd):

  1. The recipent (email address)
  2. The key, in this example this would be ‘basic’ or ‘advanced’
  3. The $params to pass to mod_mail(), in our example it’s only needed if we are using the key ‘advanced’
// Basic example ($key = 'basic')
$email = "";
drupal_mail('mod', 'basic', $email, language_default());

// Advanced example ($key = 'advanced')
$email = "";
$params = array(
  'username' = 'Someone',
  'site_name' = variable_get('site_name'),
drupal_mail('mod', 'advanced', $email, language_default(), $params);

Drupal : Get path and verify if it’s an alias

This is how I get the current path and/or alias in Drupal:
After this code has been executed:
$path will hold the internal drupal path
$alias will hold that path alias if the path in the url is actually an alias, otherwise FALSE;

$path_from_url = isset($_GET['q']) ? $_GET['q'] : '';
$path = drupal_get_normal_path($path_from_url);
if($path === $path_from_url) 
  $alias = FALSE;
  $alias = $path_from_url;

Super key / Alt+F2 not working in Ubuntu 11.10

This happened to me in Ubuntu 11.10 (Oneiric Ocelot) when I was playing with some Compiz settings. To have your shortcuts back, go to the CompizConfig Settings Manager, to do that bring up a terminal window with the shortcut ctrl+alt+t the type ccsm:

imame@meta:~$ ccsm

If you are using Unity open Desktop > Ubuntu Unity Plugin but if you are using Gnome shell choose General > Gnome Compatibily – anyways, choose whichever is already checked, one the next window check that the shortcuts are enabled and configured correctly

Compiz > Unity
Compiz > Unity
Compiz > Gnome Compatibility
Compiz > Gnome Compatibility

Classical Gnome in Ubuntu 11.10 (Oneiric Ocelot)

Install necessary packages

In order to have Gnome classical appearance on Ubuntu 11.10 you need to install gnome-shell and gnome-panel, a following reboot -from my experience – is preferable.

imame@meta:~$ sudo apt-get install gnome-shell gnome-panel
imame@meta:~$ # blabla while packages are being installed
imame@meta:~$ sudo reboot

Log in using Gnome Classical

Before opening your session, clear the gear next to your name and choose Gnome Classical.

Tweak with Gnome Tweak Tool

You can personalize you desktop with the Gnome Tweak Tool:

imame@meta:~$ sudo apt-get install gnome-tweak-tool

The launcher is located here: Applications > Others > Advanced Settings

Where is “Add to panel” and others?

Now in order to have the penel related options a simple right click won’t work, you need to Alt+ right click

Thanks for reading!

Age from date of birth in Drupal 7

In this tutorial I’m gonna show you how to calculate and display properly the age from a date CCK field the simplest way. The method I’m about to show you doesn’t need the Views module and if you use Views you’ll find that it’s totally compatible without adding any PHP code.


Before starting I want you to meet the following requirements in order to follow this tutorial:

  • Install and enable the Computed_field module
  • Install and enable the Date module (Date API, Date & Date popup)
  • Install and enable the Rules module
  • Enable the core PHP filter module
  • Add a date field to an existing content type (let’s call it field_dob)

For the purpose of this tutorial I created a very simple content type named Candidate with only 2 fields:

  • I changed the title from Title to Full name and removed the Body field
  • I added a Date field and named it field_dob

Add a computed field

Now let’s add a new field with Age as a label and field_age as a name and Computed as a field type.
In the next screen we will set the field settings:

  • Computed Code (PHP):
    $entity_field[0]['value'] = ( time() - strtotime($entity->field_dob[LANGUAGE_NONE][0]['value']) ) / 86400 / 365.25;
  • Display Code (PHP):
    $display_output = floor($entity_field_item['value']);
  • Data type: Decimal
  • Decimal Scale (decimal): 3

Add content and test

Now let’s add a few items and see if it works as we expected, I added a couple of entries and tested:

  • Old KADIMI (born on 1 Jan 1990) => Age computed 21
  • Young KADIMI (born on 2 Jan 1990) => Age computed 21

Sorting with Views

You may want to use the Views module to accommodate the output to your needs, one of the features of the Views module is that it enable you to sort results.

So what if we wanted to sort our candidates by age, Both KADIMI brothers (Old and Young) are 21 years old but Old is 1 day older that Young, for Drupal they have the same age and the result of the sorting may be inaccurate, actually we already fixed this issue by storing the age as a decimal number with 3 decimal points and displaying it as an integer (by using floor), by looking into the database table field_data_field_age with phpMyAdmin:

  • Young KADIMI is 21.570 years old
  • Old KADIMI is 21.573 yeas old

Update age

In order to recompute a candidate age we must re-save that candidate node, one way to do this is using Rules and Drupal cron

  • Visit /admin/config/workflow/rules/reaction/add to add a rule, give it a descriptive name like “Re-save candidates nodes”
    Set React on event to: Cron maintenance tasks are performed
  • Add a new action
    Set Select the action to add to: Execute custom PHP code
    Set the PHP code to:

    $old   = '1 day';      # Content is considered outdated if older than {$old}
    $type  = 'candidate';  # Apply to nodes of type {$type}
    $limit = 20;           # Update {$limit} nodes per cron
    $old = strtotime("now - $old");
    $sql = '
      SELECT n.nid FROM {node} n
      WHERE n.type = :type
        AND n.changed  < :old
      ORDER BY n.changed ASC
      LIMIT :limit
    $sql_vars = [
      ':type' => $type,
      ':old' => $old,
      ':limit' => $limit,
    $nids = db_query($sql, $sql_vars);
    foreach ($nids as $nid){
       $node = node_load($nid);

    Tweak the 3 parameters in a way that suits your needs and that’s it!

Need help?

If you found this tutorial difficult and couldn’t follow the instructions you can ask for help on the comments section or you can hire me and I will take care of this for you.

PHP: remove an array item by value

It isn’t quite obvious to remove an array item when all you know is the value of that item, – i.e. you don’t now the item index.

Remove items without preserving original indexes

$the_array = array_values(array_diff($the_array,array($the_value)));
// Example
$the_array = array('A', 'B', 'B', 'C');
$the_array = array_values(array_diff($the_array,array('B')));
Original array:
     [0] => A
     [1] => B
     [2] => B
     [3] => C
After removing 'B':
     [0] => A
     [1] => C

Remove items and preserve indexes

Based on Tobias code

while($key = array_search($the_value, $the_array)){unset( $the_array[$key]);}
// Example
$the_array = array('A', 'B', 'B', 'C');
while($key = array_search('B', $the_array)){unset( $the_array[$key]);}
Original array:
     [0] => A
     [1] => B
     [2] => B
     [3] => C
After removing 'B':
     [0] => A
     [3] => C

Drupal : Add classes to $body_class

$body_classes variable and how it’s used

By default, Drupal provides Drupal themes coders with a variable $body_classes which contains a list of space separated keywords that you can use to style your page depending on different factors including the user status (logged in or not), the page he is viewing (frontpage, node, page, story…).

Make use of $body_classes on your theme like this

<body class="<?php print $body_classes; ?>">

Here are a few exmaples of $body_classes for different visitors:

  • $body_styles: front not-logged-in page-node no-sidebars
  • $body_styles: not-front logged-in page-node node-type-blog no-sidebars

You can easily tell that the first line corresponds to a user not logged in, he is viewing the frontpage, the 2nd line is from a user who is logged in and is probably reading a blog post. Both visitors are viewing a page with no sidebars (the admin of the site decided so)

It’s very easy, using these classes to present things differently depending on what suits your needs (user status, current node type, sidebars)

You can find more about the logic behind this variable one the Drupal API Reference

Add more classes to $body_classes

Now what if you need more than those default classes, you can think of many examples; I – as the administrator of a Drupal site – want a big red banner to remind me to logout if I’m using my administrator account instead of my normal account.

The proper way to this is by using the theme hook template_preprocess_page like this (replace theme_name), we add the function theme_name_preprocess_page to our template.php file, Drupal will automatically run that code and add the new classes that we want:

<body class="<?php
function theme_name_preprocess_page(&$variables) {
  $body_classes = array($variables['body_classes']);
  // if the user is the admin add the class is_admin
  if ($variables['is_admin']) {
    $body_classes[] = 'is_admin';
  // ... more magic here if you want to add  more classes ...
  $variables['body_classes'] = implode(' ', $body_classes);

Now you can add more if clauses to the function to have the classes you need.