Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Get Random values or sequence from Collection in Magento
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

Hi,
If you need Random values or sequence from Collection in Magento you have to follow below steps

Type 1 :- put orderRand in collection it self that is

public function getrandamProducts()
        
{
                $model 
Mage::getModel("catalog/product")->getCollection();
                
$model->getSelect()->orderRand('main_table.entity_id');
                
$model->getSelect()->limit(4);
                return 
$model;
        
}

Type 2:- in collection create function and call it that is

1. 

public function getrandamProducts()
        
{
                $model 
Mage::getModel("catalog/product")->getCollection();
                
$model->setRandomOrder();
                
$model->getSelect()->limit(4);
                return 
$model;
        
}

go to collection and add this function

public function setRandomOrder()
        
{
            $this
->getSelect()->orderRand('main_table.entity_id');
            return 
$this;
        
}

Enjoy grin

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top