Have you ever wanted to rebuild Magento's indexes programmatically, in one of your custom codes?

Here I will post a small code snippet which helps you to accomplish this functionality.

try {
    for ($i = 1; $i <= 9; $i++) {       
      $process = Mage::getModel('index/process')->load($i);
      $process->reindexAll();
      //echo $process->getIndexerCode() . 'Successfully Indexed';
    }
} catch (Exception $e) {
    Mage::log($e->getMessage());
}

 

Have fun coding ;)