Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

print array ,another problem


Go to End


2 Posts   1475 Views

Avatar
snaip

Community Member, 181 Posts

11 January 2012 at 10:36pm

hi

i cant find any solution for my problem (probably pretty easy)

i have an array

        $array = array();

        $data = DataObject::get('ModulKalendariumRok');
        foreach ($data as $row) {
            $data = DataObject::get('ModulKalendarium', "Rok = '$row->Rok'");
            foreach ($data as $row) {
                $array['Kalendarium'][$row->Rok][] = $row->Wydarzenie;
            }
        }

        return new ArrayData($array);



>>>>>> print_r($array) >>>>>>

Array
(
    [Kalendarium] => Array
        (
            [2011] => Array
                (
                    [0] => test 2011
                    [1] => test v2 2011
                )

            [2010] => Array
                (
                    [0] => test 2010
                )

        )

)

how to display it in the template to getting something like this:

2011
- test 2011
- test v2 2011

2010
- test 2010

?

Avatar
Silverfish

Community Member, 59 Posts

12 January 2012 at 12:56am

Hi snaip,

assuming that you create the array() within an Model extending Page / SiteTree I guess I would create a DataObjectSet (http://api.silverstripe.org/2.4/sapphire/model/DataObjectSet.html) and return it to the Template. There you can loop it as a control.

Besides that I think that you should try to get this DataObjectSet directly from DB, without writing it into an array first and than back. Also I would try to get the Set with just one request. Doing DB-Requests (DataObject::get()) within a loop are not ideal for system performance and can be usually replaced by a single one (that's what databases live for ;)).

Regards
SF.