Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » print array ,another problem

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

Page: 1
Go to End
Author Topic: 444 Views
  • snaip
    Avatar
    Community Member
    180 Posts

    print array ,another problem Link to this post

    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

    ?

  • Silverfish
    Avatar
    Community Member
    59 Posts

    Re: print array ,another problem Link to this post

    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.

    444 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.