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

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

本文介绍了如何显示CakePHP结果集中的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:



[5] => Array
        (
            [User] => Array
                (
                    [id] => 41
                    [password] => 2b4a3cf55ddf9b15e161aeba3540a75ddd6ea872
                    [firstName] => Ming Xin
                    [lastName] => Toh
                    [email] => [email protected]
                    [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
                                )

                        )

                )

        )


推荐答案

这似乎是使用 $ this-> Model->检索的较大结果集的一部分。 find('all'),所以我假设您想显示这些结果中的所有名称。您必须遍历所有结果才能获得单个名称。另外,每个用户都有几个激情,因此如果要显示所有激情,则必须再次遍历它们。

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>';

这篇关于如何显示CakePHP结果集中的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 14:17