Question

Is there a way to display 2 banners (without duplicating the same banner?) In other words, I can’t just repeat the "echo" code, because then it can randomly select the same banner twice. Is there a way to make an array of 2 randomly selected banners?:

<?PHP
$collection = Mage::getModel('cms/block')->getCollection()
    ->addFieldToFilter('identifier', array('like'=>'ROMM-RFBanner_%'))
    ->addFieldToFilter('is_active', 1);
$blockCount = $collection->count();

echo ('<div class="footer-banner-boxes row clearfix">');
echo $this->getLayout()->createBlock('cms/block')->setBlockId('ROMM-RFBanner_'.mt_rand(1, $blockCount))->toHtml();
echo $this->getLayout()->createBlock('cms/block')->setBlockId('ROMM-RFBanner_'.mt_rand(1, $blockCount))->toHtml();
echo ('</div>'); ?>
Was it helpful?

Solution

As far as I know you can use

$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
$collection->getSelect()->limit(2);

Or with just PHP (not elegant though) you can read this in order to use $first_id = mt_rand(1,$count) and then exclude $first_id from the second call of mt_rand() by reading How to get a random value from 1~N but excluding several specific values in PHP?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top