PHP Playground: Cycle


  $items = ['Item 1', 'Item 2', 'Item 3'];

  $index = 0 ;
  print "<ul>"; 
  foreach( $items as $item ) { 
    print '<li class="'. ( $index % 2 ? 'even' : 'odd') .'">' . $item . '</li>'; 
  print "</ul>";

You can cleanup the code more or use the array key to detect odd/even. For more than 2 class names or other cases where you want to do some cyclic operation there is no simple way. In Ruby they have cycle function that cycles through items. Twig & Smarty introduced cycle for class names.

With PHP5 closure and Functional programming you can build a reusable solution:


 * Cycle Function that rotates
function cycle( $arr ) {

  return function() use(&$arr){
    $arr[] = array_shift($arr);
    return end($arr);


$items = ['Item 1', 'Item 2', 'Item 3'];
$cycle = cycle(['odd','even']);

print "<ul>"; 
foreach( $items as $item ) { 
  print '<li class="'. $cycle() .'">' . $item . '</li>'; 
print "</ul>";


This time I am cycling through functions, maybe you could find its usage.


function cycle( $arr ) {

  return function( $item ) use(&$arr){
    $arr[] = array_shift($arr);
    $fn = end($arr);
    return $fn( $item );


$items = ['Item 1', 'Item 2', 'Item 3'];
$cycle = cycle([
  function($item){ return strtoupper( $item ); },
  function($item){ return ucfirst( $item );  }

print "<ul>"; 
foreach( $items as $item ) { 
  print '<li>' . $cycle($item) . '</li>'; 
print "</ul>";


If you have a better alternate plz share your thoughts.


Drupal 7 Migrate

Every now and then we came across projects that requires migration. The old website could be a WordPress, CodeIgniter or some other database driven application. Usually we do such migrations using Custom module with batch processing. In last project in Drupal 7 i decided to give a shot to migrate module. After few trial and error i found this to be ultimate solution to migration. I tried with csv data and mysql table; both solution took 25% time to solve compare to my old custom module solution.  I have also seen couple posts that explains how to migrate html raw files.

You can learn more about migrate module from their documentation, their sample migrate_example module also has tons of examples. Do look at migrate_extras that provides support for additional contributed modules.

Here i am collecting some obstacles that i faced and their solution.

I am migrating contents from old website but want to keep their url alias due to SEO factor. I have pathauto module setup but don’t want to trigger that.

// Block pathauto from interfering. This requires migrate_extras
// Lets Put new Path
$this->addFieldMapping('path', 'old_path');

After i migrate body content it seems full_html text format is not placed. Drupal is splitting the html code as plain text. Solution:

$this->addFieldMapping('body', 'content');  
$this->addFieldMapping('body:format')->defaultValue('full_html'); // You can set other formats

In that old system there was this type which is a select option with value : On and off . In D7 i have set this as  a Term Reference field. I had to use prepareRow to map select value to tid.

class MyNodeMigration extends Migration{

 public function __construct( $arguments )
   #.. Other Code 
   $this->addFieldMapping('field_status', 'status');
   #.. Other code

 public function prepareRow($row)
   $row->status = $row->status === 'On' ? 12 : 13;

I am migrating over 65,000 Contents. All my trials were less than 100 entries and i was happy to see the throughput which was over 800/min. Then I start a full run and things start to look horrific. At some point i am getting throughput of 2/min which means to finish all content migration will take 541 Hours . Impossible! . After few tinkering i re-run the test and i able to migrate 11000 posts within 15minutes.  Awesome!

$query = db_select('tbl_pages', 'page');
  #.. Other codes
  #.. Other code
 $this->highwaterField = array(
     'name' => 'id',
     'alias' => 'page',
     'type' => 'int', );

In my case, i had to skip particular rows based on some logic which i could not able to ignore in MySQL. You can put such logics inside prepareRow function.

public function prepareRow($row){
   // This will skip this row 
   if( $row->type == 'blah' and $row->some == 'foo' )  return FALSE; 

Admin_views Module with multiple user

Module URL:  admin_views

We use this module to provide nice filtering features for our client. In few cases we share create different roles based on client request. Such as, This particular user can only edit his contents or specific content types. A good feature of admin_views is that it does considers user permission and renders admin links based on that. But one problem is that if the site has huge list of contents; its hard to find a single content from a list of thousand items.

To make this user intuitive, we want to show only user contents in Content Management but for specific roles we will show them all. To do that follow these steps:

1. Go to Views and open edit form for “Administration: Nodes” .  Admin_views module utilises this module.

2. Add a contextual Filter Author: uid  . Click on “Provide Default Value” and choose PHP code. Note: If you dont see php code in here then enable PHP filter module from Modules.

3. Now put following snippet in the “PHP contextual filter code” :

global $user;

if( in_array( 'administrator', $user->roles) )
  return 'all';
  return $user->uid;

4. Finally Save the views .

Thats it . Now only administrator can view entire list but for other roles only authored contents will be visible to logged in user.


Nope !  i am not gone. Neither i have stopped coding nor i quit from my dev life. I am working my ass off more than any days from the past (bfr 2010). I have become more systematic(really!), more organized(WTF!) and more creative (Oh yeah!).  Don’t think i am not aware with the progress of CI to CI Reactor .. Starting of Drupal 7 or the new layout of Facebook. Though my current job forces me to stick with Drupal mostly but still my interest for CodeIgniter never withered .

So why this post — a modest warning of my coming back. Get ready for  some new posts — mostly drupal ( drush, git, management etc.) ..   BEST OF LUCK TO ME 🙂




Jhoro T-Shirt

At last we ( JhoroTEK ) got our own T-shirt.. . Here is a visual demo of our T-Shirt…

From now on we are going to wear this T-shirt in our office.  Except in some formal meeting or other situation.

Stotic Bliss – ei je ami

“Koto din katea galo ..
tomai na dekha ..
koto raat paar holo
tomai paasha na paea..

majha majahea mona hoai
jodi dekhatea partam..
koto bhalobashi tomakea…

ja dekea takai.. sa dekea dekhi ..
tomar sai proticchobi .
mona porea jai..
sheeter protohom bhalo basha chilam tomai..

adharea gharea amar ai duniay.
alo diya bhorea dilea tumi..
gholatea meghla akashea amar..
rong dhonu hoa alea tumi.

….. ”

The lyrics and thoughts is really nice.. i like it..