-5

I have a database and I want to output the last 5 rows on the database. The model is called 'text'.

<?php for ($j = 0; $j < 5; $j++) { 

    ?>
    <tr>
        <td><?php echo $text['Text']['id']; ?></td>
        <td>
            <?php echo $text['Text']['expiration']; ?>
        </td>
        <td>
            <?php echo $text['Text']['title'] . " - " . $text['Text']['body']; ?>
        </td>
        <td><?php echo $text['Text']['created']; ?></td>
    </tr>
    <?php } ?>

That should give you an idea of where I'm at.

tereško
  • 58,060
  • 25
  • 98
  • 150
DCo
  • 69
  • 2
  • 10

1 Answers1

1

You have to use find()

Other way is to use query()

$alltext = $this->Text->find('all', array('limit' => 5,'order'=>array('id DESC')));
<?php foreach ($alltextext as $text): ?>
 // format as necessary
<td><?php echo $text['Text']['id']; ?></td> 
// add others here
<?php endforeach; ?>
Vamsi Krishna B
  • 11,377
  • 15
  • 68
  • 94
  • The closest I can get is this: $results = $this->Text->find('first', array('conditions' => array('id' => $id), 'order' => 'id DESC' )); – DCo Apr 14 '12 at 03:49
  • I put this in to try it out, and it didn't work: Text->find('all', array('limit' => 5,'order'=>array('id DESC'))); foreach ($alltext as $text): ?> – DCo Apr 14 '12 at 04:09
  • @DCo Make the `find()` in controller and [set](http://book.cakephp.org/1.3/view/979/set) it in controller. This set variable can be accessed in view(ctp). – Justin John Apr 14 '12 at 04:15