Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » print array in template

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

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

    print array in template Link to this post

    HI

    how to foreach this array in template ?

    public function produkty() {
    $produkty_array = new DataObjectSet;

    $produkty = array();
    $filter = "ParentID = $this->ID";
    $data = DataObject::get('SiteTree', $filter);

    foreach ($data as $item) {

    $sqlQuery = new SQLQuery();
    $sqlQuery->select = array(
    'KolorID','Filename'
    );
    $sqlQuery->from = array("ProduktKolory LEFT JOIN File ON ProduktKolory.KolorID = File.ID");
    $sqlQuery->where = array("ProduktID = $item->ID");

    $results = $sqlQuery->execute();

    foreach ($results as $row) {
    $kolor['KolorID'] = $row['KolorID'];
    $kolor['KolorURL'] = $row['Filename'];
    $produkt[$item->ID][] = $kolor;
    }
    }
    // echo '<pre>';
    // print_r($produkt);
    // echo '</pre>';

    $produkty_array->push(new ArrayData($produkt));

    return $produkty_array;
    }

    <pre>Array
    (
    [10] => Array
    (
    [0] => Array
    (
    [KolorID] => 24
    [KolorURL] => assets/kolory/ico-color-blue.png
    )

    [1] => Array
    (
    [KolorID] => 23
    [KolorURL] => assets/kolory/ico-color.png
    )

    )

    [16] => Array
    (
    [0] => Array
    (
    [KolorID] => 23
    [KolorURL] => assets/kolory/ico-color.png
    )

    )

    )
    </pre>

    i need to do

    <ul>
    <% control produkty %>
    <li>
    $KolorID
    <img src="$Filename" />
    </li>
    <% end_control %>
    </ul>

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: print array in template Link to this post

    the end result should be like this:

    <li>
    10
    kolorID: 24
    kolorURL: ....

    kolorID: 23
    kolorURL: ....
    </li>

    <li>
    16
    kolorID: 23
    kolorURL: ....
    </li>

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: print array in template Link to this post

    how about something like...

    public function produkty() {
    //do stuff to get results into $results
    $dos = new DataObjectSet();
    foreach ($results as $row) {
    foreach ($row as $key => $val) {
    $do->$key = $val;
    $dos->push($do);
    }}}

    <ul>
    <% control produkty %>
    <li>
    $KolorID
    <img src="$Filename" />
    </li>
    <% end_control %>
    </ul>

    1896 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.