views:

41

answers:

2

hi all,

i'm still a beginner, and guess it's a simple CakePHP question...

all i want is to echo retrieved data from database (one row is selected).

i have next code:

$cover_page = $this->Publication->find('list', array('conditions' => array('Publication.id' => $id)));

now, how can i echo field title from selected database row?

tnx in adv!!!

A: 

You access it like this (CakePHP creates an array of the database result):

echo $cover_page['Publication']['title'];

To get it into the view do:

$this->set('cover_page',$cover_page);
Leo
The reason for having to specify the index 'Publication' is that when you are fetching associated models, the others will appear under their respective indices.
Leo
well, that is a problem, i tryed that before, but i'm getting an error:Notice (8): Undefined index: Publication [APP\controllers\publications_controller.php, line 302]
@Leo,`find('list')` doesn't give a model-name index.
SpawnCxy
@SpawnCxy: Woops - I hadn't noticed that.@user198003 - use $this->Publication->read(null,$id); That will return the single record corresponding to that unique id. Variants of find may return an array of records (containing only one record). If you want one result from a unique id, read is the standard way.
Leo
A: 

From the cookbook:

find('list', $params) returns an indexed array, useful for any use where you would want a list such as for populating input select boxes.

It will give a result as below

Array
(
//[id] => 'displayValue',
[1] => 'displayValue1',
[2] => 'displayValue2',
[4] => 'displayValue4',
[5] => 'displayValue5',
[6] => 'displayValue6',
[3] => 'displayValue3',

)

Since in your code you've specified the id to make the result only one record ,you may not really need to use it, though you can access the title with $cover_page[$id] if you've set the right displayfield.A normal way to do your work would be

$cover_page = $this->Publication->find('first', array('conditions' => array('Publication.id' => $id)));

or

$cover_page = $this->Publication->findById($id);

Both of them can get the title by

$cover_page['Publication']['title']
SpawnCxy