开发者

Showing first element of the array

How ca开发者_StackOverflow社区n I find and show Name of first element in a table?

I'm guessing that I have to use something like

$this->data['lastInvioce'] = $this->Invoice->find('all', array('limit' => 1));

... in controller to find it...


Your code is almost correct. The first parameter of Model::find() indicates how many records to retrieve. Model::find('all') retrieves all matching records. What you want is Model::find('first'):

// Retrieve most recent Invoice record
$lastInvoice = $this->Invoice->find('first', array(
    'fields' => array( 'Invoice.name' ),
    'order' => array( 'Invoice.created' => 'desc' )
);
// Make this Invoice record available in the view
$this->set( compact('lastInvoice'));

This will retrieve the most recently created Invoice record, and only the name field. The $this->set() call makes the data in $lastInvoice available to your view. So:

echo $lastInvoice['Invoice']['name'];

Or alternatively:

extract($lastInvoice);
echo $Invoice['name'];


Try $this->Invoice->find( 'first', ... ). You can use other conditions and/or sorting to manipulate which record is the first, of course.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜