这是我的代码,
$collection = Mage::getModel('catalog/product')
->getCollection()
->setStoreId($storeId)
->addStoreFilter($storeId)
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4)
->addCategoryFilter(Mage::getModel('catalog/category')->load(500))
->addAttributeToSort('created_at', 'desc');
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
$collection->setPageSize(20);
因为这个我得到随机20产品,但我想它应该从前20个产品中提供一个产品,从第二个20个产品中提供一个产品...... 20个产品。
我该怎么做?
任何想法?
答案 0 :(得分:2)
我相信这样的事情可以帮助你,祝你好运= D
<?php
$collection = Mage::getModel('catalog/product')
->getCollection()
->setStoreId($storeId)
->addStoreFilter($storeId)
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4)
->addCategoryFilter(Mage::getModel('catalog/category')->load(500))
->addAttributeToSort('created_at', 'desc');
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
$collection->setPageSize(20);
$array = array();
foreach ($collection as $product) {
$array[] = $product->getId();
}
$rand = rand(0,19);
do {
$rand2 = rand(0,19);
} while ($rand2 == $rand);
$produc1 = Mage::getModel('catalog/product')->load($array[$rand]);
$produc2 = Mage::getModel('catalog/product')->load($array[$rand2]);
?>