Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp pagination with random order?

Ok, I have looked and looked but cannot seem to find anything on this anywhere. I have a display of results that are paginated beautifully, but they currently display in ascending order. I'd like for them to display in random order. Here is my current controller code:

public function condos() {          

$this->paginate['Unit']=array(
        'limit'=>9,
        'contain'=>array(
                'User'=>array(
                    'id', 'user_name', 'area_code', 'exchange', 'sln', 'email'),
                'Complex'=>array('id','complex_name', 'realname', 'photo1','photo2','photo3','photo4','photo5', 'complex_website')
                    ),
        'conditions'=>array(
                'Unit.type'=>array('condo', 'rentalco'),
                'Unit.active'=>1)   
    );
$data = $this->paginate('Unit');
$this->set('allcondos', $data);


}
like image 392
huzzah Avatar asked Jan 11 '12 22:01

huzzah


1 Answers

For anyone else finding this - the actual answer is to generate a seed (a float between 0 and 1), and save it to the session before the RAND() sort is necessary (in the controller's beforeFilter()). Then:

$this->paginate['order'] = sprintf('RAND(%f), $this->Session->read('seed'));

This preserves the RAND() seed between calls to the paginator, and preserves the overall order of the results between requests.

like image 163
b. e. hollenbeck Avatar answered Oct 15 '22 21:10

b. e. hollenbeck