views:

51

answers:

3

Hi all,

I am using cakephp 1.26 and doing pagination. could you help me with the following code please? I can't figure out what's wrong in the code.

$this->set('posts', $this->paginate = array('order'=>array('Post.created'=> 'DESC'), 'conditions'=>array('Post.zero'=>'0'), 'limit'='6'
)                                           
                    );

In the .ctp file I have this:

<table>
<tr><td>
       <?php echo $paginator->numbers(); ?>
<?php
    echo $paginator->prev('Previous', null, null);
    echo $paginator->next(' Next', null, null);
?> 

</td></tr>
</table>
A: 

Shouldn't your code be:

$this->set('posts', $this->paginate = array(
    'order' => array('Post.created' => 'DESC'),
    'conditions' => array('Post.zero' => '0'),
    'limit' => '6')
);

?

Anax
Assignment within function call is unnecessary and incorrect. You just need to pass the array for the second parameter or assign the class variable before making the call, when the second parameter would not be used.
Leo
You're right, but I concentrated on the fact that kwokwai's code wasn't correct.
Anax
A: 

You can try to make the process more clear.

$this->paginate['Post'] = array('order'=>array('Post.created'=> 'DESC'), 'conditions'=>array('Post.zero'=>'0'), 'limit'='6'));
$posts = $this->paginate('Post');
$this->set(compact('posts'));
SpawnCxy
And shorter - `$this->set('posts', $this->paginate('Post'));`
bancer
+2  A: 

Your code is bad. You cannot make the assignment within the function call. Either do:

$this->set('posts', $this->paginate('Post',array(
                  'order' => array('Post.created' => 'DESC'),
                  'conditions' => array('Post.zero' => '0'),
                  'limit' => '6'
                  )));

or:

$this->paginate = array(
    'order' => array('Post.created' => 'DESC'),
    'conditions' => array('Post.zero' => '0'),
    'limit' => '6');

$this->set('posts', $this->paginate('Post'));
);
Leo