有没有办法显示2个横幅(不重复相同的横幅?) 换句话说,我不能只重复“回声”代码,因为它可以随机选择相同的横幅两次。有没有办法制作2个随机选择的横幅数组?:
<?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>'); ?>
答案 0 :(得分:0)
据我所知你可以使用
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
$collection->getSelect()->limit(2);
或者仅使用PHP(虽然不优雅),您可以阅读此内容以使用$first_id = mt_rand(1,$count)
然后使用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?