Jump to:

3461 Posts in 1065 Topics by 740 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] How to display an array of data

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

Page: 1
Go to End
Author Topic: 1589 Views
  • donovanh
    Avatar
    Community Member
    3 Posts

    [SOLVED] How to display an array of data Link to this post

    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

  • donovanh
    Avatar
    Community Member
    3 Posts

    Re: [SOLVED] How to display an array of data Link to this post

    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.

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: [SOLVED] How to display an array of data Link to this post

    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 %>

  • donovanh
    Avatar
    Community Member
    3 Posts

    Re: [SOLVED] How to display an array of data Link to this post

    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.

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