roolex
roolex

Reputation: 29

Display last 3 records from database CakePHP

Problem with displaying news from database!

Screenshoot: enter image description here

Database(table) Articals: ID, Title, Content, User

Very simple, i only want display last 3 news from database with foreach in view (my controller send me array of 3 records in $artical, and this is ok), but view cannot display this.. please help me, my dead line is tomorrow.

Upvotes: 3

Views: 2580

Answers (1)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35963

In cakephp if you put into your tables the field created and modified (DATETIME), the mvc automatically insert the date of creation or when you modify it.

So when you retrieve your data you can do in this mode

$articals = $this->Artical->find('all', array(
  'limit' => 3,
  'order' => 'Artical.created DESC' )
));

or in your case if you an increment id you can do in this mode (but I suggest you to use created and insert it into your tables)

    $articals = $this->Artical->find('all', array(
          'limit' => 3, 
          'order' => 'Artical.ID DESC' )
        ));

If you have some problem to print data try to make a var_dump($articals); After try to print out your content like this:

foreach ($articals as $artical) {
     echo '<p>'.$artical['Artical']['Content'].'</p>';
}

Upvotes: 5

Related Questions