问题
I have the following codes, I added the debug and this is the data that I am able to retrieve from the database. I wish to display the ['User']['firstName']
and also the ['Passion']['passion_tag']
in the view page, how do I do it? I am facing error such as:
Notice (8): Undefined index: passion_tag [APP\views\searches\simple_search.ctp, line 127]
[5] => Array
(
[User] => Array
(
[id] => 41
[password] => 2b4a3cf55ddf9b15e161aeba3540a75ddd6ea872
[firstName] => Ming Xin
[lastName] => Toh
[email] => mingxin@xconnect.com
[locale] => eng
[displayName] =>
[timezone] => 0
[gender] => f
[dob] => 1989-08-25
[aboutme] =>
[country_home] =>
[country_current] => Singapore
[city_current] =>
[currentLogin] =>
[lastLogin] =>
[registeredDate] =>
[profileImgBig] =>
[image_id] =>
[authProvider] =>
[token] =>
[authId] =>
)
[Image] => Array
(
[id] =>
[name] =>
[img_file] =>
)
[Passion] => Array
(
[0] => Array
(
[id] => 46
[tag] => acting
[dateCreated] =>
[userDefined] => 1
[PassionsUser] => Array
(
[id] => 64
[user_id] => 41
[passion_tag] => acting
[description] => Dont be shy
[datejoined] => 2011-09-07 07:06:54
[type] => professional
[passionImg] => UserPassion/Acting.jpg
)
)
[1] => Array
(
[id] => 44
[tag] => cake baking
[dateCreated] =>
[userDefined] => 1
[PassionsUser] => Array
(
[id] => 62
[user_id] => 41
[passion_tag] => cake baking
[description] => Bakey Bakey
[datejoined] => 2011-09-07 09:04:10
[type] => professional
[passionImg] => UserPassion/Cake Baking.jpg
)
)
[2] => Array
(
[id] => 42
[tag] => hip hop dancing
[dateCreated] =>
[userDefined] => 1
[PassionsUser] => Array
(
[id] => 60
[user_id] => 41
[passion_tag] => hip hop dancing
[description] => Hopping is Good
[datejoined] => 2011-08-23 12:41:55
[type] => personal
[passionImg] => UserPassion/Hiphop-Dance.jpg
)
)
[3] => Array
(
[id] => 45
[tag] => sky diving
[dateCreated] =>
[userDefined] => 1
[PassionsUser] => Array
(
[id] => 63
[user_id] => 41
[passion_tag] => sky diving
[description] => Dont splat the Earth
[datejoined] => 2011-09-07 06:59:43
[type] => personal
[passionImg] => UserPassion/Sky Diving.jpg
)
)
[4] => Array
(
[id] => 47
[tag] => tamil movies
[dateCreated] =>
[userDefined] => 1
[PassionsUser] => Array
(
[id] => 65
[user_id] => 41
[passion_tag] => tamil movies
[description] => Really interesting
[datejoined] => 2011-08-18 16:17:00
[type] => personal
[passionImg] => UserPassion/Tamil Movies.jpg
)
)
[5] => Array
(
[id] => 43
[tag] => teaching
[dateCreated] =>
[userDefined] => 1
[PassionsUser] => Array
(
[id] => 61
[user_id] => 41
[passion_tag] => teaching
[description] => Nothing beats this experience
[datejoined] => 2011-09-09 07:52:12
[type] => professional
[passionImg] => UserPassion/Teaching.jpg
)
)
)
)
回答1:
It looks like that this is part of a larger result set retrieved with $this->Model->find( 'all' )
, so I assume you want to display all names from those results. You have to loop through all results to get individual names. Also, each user has several Passions so again you have to loop through them if you want to display them all.
echo 'Our users and their passions are: <ul>';
foreach( $this->data as $user ) {
echo '<li>';
echo $user[ 'User' ][ 'firstName' ];
echo ' who is passionate about: ';
foreach( $user[ 'Passion' ] as $passion ) {
echo $passion[ 'PassionsUser' ][ 'passion_tag' ];
echo ' ';
}
echo '</li>';
}
echo '</ul>';
回答2:
Passion does not have a passion_tag
key, passion_tag
is in PassionsUser
.
If $data
is where all the data is stored,
echo $data['User']['Passion'][0]['PassionsUser']['passion_tag'];//echo first result
If you want to list all, you'll have to iterate through the PassionUser
array.
来源:https://stackoverflow.com/questions/7819178/how-to-display-results-from-a-result-set-in-cakephp