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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Archive /

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

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

Using a custom array inside a <% control property %>


Go to End


2 Posts   2693 Views

Avatar
Mednezz

Community Member, 23 Posts

2 October 2007 at 6:29am

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!

Avatar
Mednezz

Community Member, 23 Posts

2 October 2007 at 9:50pm

Edited: 02/10/2007 10:06pm

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,
        );