Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Using a custom array inside a <% control property %>

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2274 Views
  • Mednezz
    Avatar
    Community Member
    23 Posts

    Using a custom array inside a <% control property %> Link to this post

    Hey there,

    Ik got a function which creates an array based on form fields on the page before.

    Now with that array i want a simple foreach kind a thing in the template, i know i'm close, but could anyone help me a step further?

    Here's the php

    if ($_POST['Intermediair_250'] > 0) {
          $producten[]['product'] = 'Intermediar pakket 250';
          $producten[]['prijs_totaal'] = $_POST['Intermediair_250'] * 2697;
          $producten[]['prijs_per_stuk'] = 2697;
       }
           
       if ($_POST['Intermediair_1000'] > 0) {
          $producten[]['product'] = 'Intermediar pakket 1000';
          $producten[]['prijs_totaal'] = $_POST['Intermediair_1000'] * 7188;
          $producten[]['prijs_per_stuk'] = 7188;
       }
       
       $totaalprijs = 0;
       foreach ($producten as $product) {
          $totaalprijs = $totaalprijs + $product['prijs_totaal'];
       }
       $doProducten = new DataObjectSet();
       $doProducten->producten = new ArrayData($producten);
       $data = array(
    "Title" => "Mijn bestelling",
    "producten" => $doProducten,
    "totaalprijs" => $totaalprijs
    );

    And then in the template :

    <% control producten %>
             <tr>
             <td class="icon"><img src="empex/images/shoppingcart.jpg" width="25" height="23" /></td>
             <td class="Producten">$product</td>
             <td class="Aantal"></td>
             <td class="Categorie"></td>
             <td class="Credits"></td>
             <td class="Geldigheid"></td>
          </tr>
          <% end_control %>

    so i want to iterate producten, but how?

    Thanks!

  • Mednezz
    Avatar
    Community Member
    23 Posts

    Re: Using a custom array inside a <% control property %> Link to this post

    Got it to work! with the help of Matt!!

    check it out!

    $producten = new DataObjectSet();
       
       if ($_POST['vacature_laag'] > 0) {
          $producten->push(new ArrayData(array('product' => 'Vacature(s) categorie 1',
                                   'prijs_totaal' => $_POST['vacature_laag'] * 30,
                                   'prijs_per_stuk' => 30)));
       }
       
       if ($_POST['vacature_hoog'] > 0) {
          $producten->push(new ArrayData(array('product' => 'Vacature(s) categorie 2',
                                   'prijs_totaal' => $_POST['vacature_hoog'] * 109,
                                   'prijs_per_stuk' => 109)));
       }
       
       if ($_POST['Intermediair_100_stage'] > 0) {
          $producten->push(new ArrayData(array('product' => 'Intermediar pakket 100 stage',
                                   'prijs_totaal' => $_POST['Intermediair_100_stage'] * 990,
                                   'prijs_per_stuk' => 990)));
       }
       if ($_POST['Intermediair_100'] > 0) {
          $producten->push(new ArrayData(array('product' => 'Intermediar pakket 100',
                                   'prijs_totaal' => $_POST['Intermediair_100'] * 999,
                                   'prijs_per_stuk' => 999)));
       }
           
       if ($_POST['Intermediair_250'] > 0) {
          $producten->push(new ArrayData(array('product' => 'Intermediar pakket 100',
                                   'prijs_totaal' => $_POST['Intermediair_250'] * 2697,
                                   'prijs_per_stuk' => 2697)));
       }
           
       if ($_POST['Intermediair_1000'] > 0) {
          
          $producten->push(new ArrayData(array('product' => 'Intermediar pakket 1000',
                                   'prijs_totaal' => $_POST['Intermediair_1000'] * 7188,
                                   'prijs_per_stuk' => 7188)));
          
       }
       
       $data = array(
    "Title" => "Mijn bestelling",
    "producten" => $producten,
    );

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