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 /

[SOLVED] How to display an array of data


Reply


4 Posts   1645 Views

Avatar
donovanh

Community Member, 3 Posts

16 October 2009 at 4:23pm

Edited: 19/10/2009 2:27pm

Hi,

I'm just getting my head around how silverstripe works and would appreciate any tips to help get past a bit of a roadblock.

I'm building a method that once it has obtained an XML product list from Amazon, creates an array of information to display on a product page.

One approach I'm looking at is creating a simple array including items such as:

$product['title']
$product['description']
etc

I believe I could pass them in through Arraydata (http://doc.silverstripe.org/doku.php?id=arraydata). However, I am confused about one aspect, which is that there can be multiple authors.

Since the authors would need their own array, I don't know how to combine this with the other data.

Any tips on how to approach this much appreciated.

Don

Avatar
donovanh

Community Member, 3 Posts

16 October 2009 at 4:54pm

Trying to display the data, I can't get anything to appear.

The method includes the following code to assemble the data:

function getItem() {
...
$aItem['ReviewerLocation'] = $item->CustomerReviews->Review->{0}->Reviewer->Location;
$aItem['Review'] = $item->CustomerReviews->Review->{0}->Content;
$aItem['Summary'] = $item->CustomerReviews->Review->{0}->Summary;

$itemData = new DataObjectSet();
foreach($aItem as $key => $data) {
   $itemData->push(new ArrayData($data));
}
return $itemData;
}

I'm stuck on how to access this in the template.

Avatar
dalesaurus

Community Member, 283 Posts

16 October 2009 at 5:54pm

ArrayData expects an array() that is key>valued so you will have uniform access in the control loop in the template.

Ch-ch-check it out:

public function GetItem() {
...
$itemData = new DataObjectSet();
foreach( $item->CustomerReviews->Review as $review ) {
$data = array (
'ReviewerLocation' => $review->Location,
'Review' => $review->Content,
'Summary' => $review->Summary
);
$itemData->push(new ArrayData($data));
}
return $itemData;
}

and for the template, assuming the above is in your controller

DUDE!!! REVIEWS ARE IN!!!!!!!!!!!!!
<% control GetItem %>
Some hoser from $ReviewerLocation had this to say
$Summary

oh and the rest is: $Review
<% end_control %>

Avatar
donovanh

Community Member, 3 Posts

18 October 2009 at 8:35pm

Edited: 19/10/2009 2:27pm

Thank you for the reply.

EDIT: I realised what I was doing wrong.

In the end I decided to create a global variable, $ItemXML, which was then accessed by smaller methods, which grabbed the necessary arrays of info from the XML as needed.