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.

Data Model Questions /

print array in template


Reply


3 Posts   2159 Views

Avatar
snaip

Community Member, 180 Posts

7 March 2011 at 11:47pm

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>

Avatar
snaip

Community Member, 180 Posts

7 March 2011 at 11:59pm

the end result should be like this:

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

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

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

Avatar
swaiba

Forum Moderator, 1799 Posts

8 March 2011 at 2:08am

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>