如何在CakePHP中显示结果集的结果?

时间:2011-10-19 09:27:56

标签: cakephp cakephp-1.3

我有以下代码,我添加了调试,这是我能够从数据库中检索的数据。我希望在视图页面中显示['User']['firstName']['Passion']['passion_tag'],我该怎么做?我面临的错误如:

  

注意(8):未定义索引:passion_tag [APP \ views \ searching \ simple_search.ctp,第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
                                )

                        )

                )

        )

2 个答案:

答案 0 :(得分:1)

Passion没有passion_tag密钥,passion_tag位于PassionsUser

如果$data是存储所有数据的地方,

echo $data['User']['Passion'][0]['PassionsUser']['passion_tag'];//echo first result

如果要列出所有内容,则必须遍历PassionUser数组。

答案 1 :(得分:1)

看起来这是使用$this->Model->find( '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>';