Moeite met Foreach..

Status
Niet open voor verdere reacties.

emplode

Gebruiker
Lid geworden
2 feb 2011
Berichten
10
Ik heb een opdracht, en het is de bedoeling dat ik doormiddel van de foreach en for lus gegevens uit mijn html formulier haal en ze post in een bestelling overzicht.

Dit is wat ik heb momenteel.


Bestellingfuncties.php
Code:
 <?php
function beschikbaarheid($merk, $os)
{

$beschikbaar = array
( 
"Toshiba" => array
(
"xp"=> "U heeft voor de operating system xp gekozen.", 
"vista" => "Deze laptop is niet beschikbaar met de vista operating system.", 
"linux" => "U heeft voor de operating system linux gekozen."
),
"Acer" => array
(
"xp"=> "U heeft voor de operating system xp gekozen.", 
"vista" =>"U heeft voor de operating system vista gekozen.", 
"linux" =>" U heeft voor de operating system linux gekozen."
),
"Hp" => array
("xp"=> "U heeft voor de operating system xp gekozen.", 
"vista" => "Deze laptop is niet beschikbaar met de vista operating system.", 
"linux" => "Deze laptop is niet beschikbaar met de linux operating system."
)
);
 
return($beschikbaar[$merk][$os]);
}
 
// 
 
function bestellingoverzicht()
{
 
$laptop["toshiba"] = "toshiba";
$laptop["acer"] = "acer";

 
$info[0] = "product";
$info[1] = "merk";
$info[2] = "model";
$info[3] = "os";
$info[4] = "aantal";
$info[5] = "prijs";



echo "<br /> 
<table width='80%' border='1' >
<caption>
<strong>Bestellingoverzicht</strong>
</caption>
<thead>
<tr><th>Product</th><th>Merk</th><th>Model</th><th>OS</th><th>Aantal</th><th>Basisprijs</th></tr>
</thead>
<tbody>";
 
foreach($laptop as $key => $value)
{$aantal = $value. "aantal";
	if($_POST[$aantal] == 0) continue;
		$reij = "<tr>";


for($x=1; $x<=sizeof($info); $x++) 
{$element = $value . $info[$x];
	$reij = $reij . "<td>" . $element ."</td>";	}
	$reij = $reij . "</tr>";
echo $reij;
}
echo "</tbody></table>";
}
?>

lab10.php
Code:
<?php
include("bestellingfuncties.php")
?>
<hr />
<table border=0 cellpadding=0 cellspacing=0 width=100%>
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 
Korting:<br />
<input type="checkbox" name="korting1" value="15">Student 15%</br />
<input type="checkbox" name="korting2" value="10">Senior 10%</br />
<input type="checkbox" name="korting3" value="5">Klant 5%</br />
 
<hr />
Betalingswijze:
<input type="radio" name="Betalingswijze" value="Paypal">Paypal
<input type="radio" name="Betalingswijze" value="Mastercard">Mastercard
<input type="radio" name="Betalingswijze" value="Visa">Visa
<hr />
 
<tr>
<td>
<img src="toshiba.jpg"  alt="Toshiba laptop" />
</td>
</tr>
 
<tr>
<td>
Toshiba Satellite A100-510   Basisprijs 999.99
</td>
</tr>
 
<tr>
<td><!--Shopping Cart Begin-->
<input type="hidden" name="product" value="001" />
<input type="hidden" name="merk" value="Toshiba" />
<input type="hidden" name="model" value="Satellite A100-510" />
 
Operating system: 
<select name="os">
<option value="xp">Windows xp</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
 
Aantal: 
<input type="text" size=2 maxlength=3 name="aantal" value="0" />
<input type="hidden" name="prijs" value="999.99" />
 
<input type="image" src="bestel.png" border=0 value="bestellen" />
</td>
 
<tr>
<td>
<img src="acer.jpg"  alt="Acer laptop" />
</td>
</tr>
 
<tr>
<td>
Acer Aspire 57357Z Basisprijs 529.99
</td>
</tr>
 
<td>
<input type="hidden" name="product2" value="002" />
<input type="hidden" name="merk2" value="Acer" />
<input type="hidden" name="model2" value="Aspire 5735Z" />
 
Operating system: 
<select name="os2">
<option value="xp">Windows xp</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
 
Aantal: 
<input type="text" size=2 maxlength=3 name="aantal2" value="0" />
<input type="hidden" name="prijs2" value="529.99" />
<input type="image" src="bestel.png" border=0 value="bestellen" />
<hr /></td>
 
<!--Shopping Cart End  -->
</tr>
</form>
</table>
 
<?php
 
echo "Korting is: " . ($_POST["korting1"] + $_POST["korting2"] + $_POST["korting3"]) . " procent" . "<br />";
if($_POST["aantal"] * $_POST["prijs"]>="5000")
{
echo "Kredietlimiet bereikt---Je bestelling mag maximaal 5000 euro’s bedragen " . $_POST["naam"];
}
else if($_POST["aantal"] * $_POST["prijs"]<"5000" && $_POST["aantal"] * $_POST["prijs"]>"0")
{
echo "Uw bestelling bedraagt " .$_POST["aantal"] * $_POST["prijs"];
}
 
switch($_POST['Betalingswijze'])
{
case "Paypal":
echo "<br />Uw betaling wordt behandeld via PayPal";
break;
 
case "Mastercard":
echo "<br />Uw betaling wordt behandeld via Mastercard";
break;
 
case "Visa":
echo "<br />Uw betaling wordt behandeld via Visa";
break;
 
default:
echo "<br />U moet een betalingswijze kiezen! <br />";
}
 
 
echo beschikbaarheid($_POST['merk'], $_POST['os']);
 
echo bestellingoverzicht()
 
?>

Als iemand evne zou willen kijken enmij de goeie richting op kan sturen.

Overigens, er is recentelijk nog een post gemaakt hierover. Maar kon ik niks uit wijsmaken.

Alvast bedankt.
 
U zit toch niet met een vraag zoals dit

http://www.helpmij.nl/forum/showthr...voor-een-externe-functie-van-een-shoppingcart

bekijk de posts van mij eens


ik merk op dat zeer vuile code is.

PHP:
<?php
$translations['nl']['Paypal']="Uw betaling wordt behandeld via PayPal";
$translations['nl']['Mastercard']="Uw betaling wordt behandeld via Mastercard";
$translations['nl']['Visa']="Uw betaling wordt behandeld via Visa";
$translations['nl']['paymentnotexist']="U moet een betalingswijze kiezen! ";
$lang="nl";
$translation= $translations[$lang];
$payments = array('Paypal', 'Mastercard', 'Visa');
$products['Toshiba A100-510']['code']="001";
$products['Toshiba A100-510']['availebleos']=array('xp','vista','linux');
$products['Toshiba A100-510']['img']='toshiba.jpg';
$products['Toshiba A100-510']['price']='999.99';
$products['Toshiba A100-510']['name']='Toshiba A100-510';
$products['Toshiba A100-510']['merk']='Toshiba ';
$products['Acer Aspire 57357Z']['code']="002";
$products['Acer Aspire 57357Z']['availebleos']=array('xp','vista','linux');
$products['Acer Aspire 57357Z']['img']='acer.jpg';
$products['Acer Aspire 57357Z']['price']='529.99';
$products['Acer Aspire 57357Z']['name']='Aspire 5735Z';
$products['Acer Aspire 57357Z']['merk']='Acer ';

print_r($_POST);
?>

</hr>
<table border=0 cellpadding=0 cellspacing=0 width=100%>
    <form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Korting:<br />
    <input type="checkbox" name="korting1" value="15">Student 15%</br />
    <input type="checkbox" name="korting2" value="10">Senior 10%</br />
    <input type="checkbox" name="korting3" value="5">Klant 5%</br /> 
    <hr />
    Betalingswijze:
        <?php 
        foreach($payments as $option){print '<input type="radio" name="Betalingswijze" value="'.$option.'">'.$option;
        }  
        foreach($products as $product){
            print'<tr><td><img src="'.$product["img"].'"  alt="Toshiba laptop" /></td></tr>';
            print'<tr><td>'.$product["name"]."   ".$product["price"].'></td></tr>';
            print'<tr><td>';
            $key = 'bestelling['.$product["code"].']';
            print'<input type="hidden" name="'.$key.'[product]" value="'.$product["code"].'" />';
            print'<input type="hidden" name="'.$key.'[merk]" value="'.$product["merk"].'" />';
            print'<input type="hidden" name="'.$key.'[model]" value="'.$product["name"].'" />';
            print'Operating system:  <select name="'.$key.'[os]" >';
            foreach($product['availebleos'] as $os){
               print'<option value="'.$os.'">'.$os.'</option>';        
            }
            print' </select>';
            print'Aantal: <input type="text" size="2" maxlength="3" name="'.$key.'[aantal]" value="0" />
                        <input type="hidden" name="'.$key.'[prijs]" value="'.$product['price'].'" />
                <input type="image" src="bestel.png" border="0" value="bestellen" /></td><tr>';
        }
        ?>
</form>
</table>
 <?php
echo "Korting is: ".($_POST["korting1"]+$_POST["korting2"]+$_POST["korting3"])." procent". "<br />";
 
if($_POST["aantal"] * $_POST["prijs"]>="5000"){
    echo "Kredietlimiet bereikt---Je bestelling mag maximaal 5000 euro’s bedragen " . $_POST["naam"];
}else if($_POST["aantal"] * $_POST["prijs"]<"5000" && $_POST["aantal"] * $_POST["prijs"]>"0"){
    echo "Uw bestelling bedraagt " .$_POST["aantal"] * $_POST["prijs"];
}
 
if (in_array($_POST['Betalingswijze'],$payments)){
    echo $translations[$_POST['Betalingswijze']];
} else{
    echo $translation['paymentnotexist'];    
}
echo beschikbaarheid($_POST['merk'], $_POST['os']);
if(isset($_POST) && isset( $_POST["bestelling"])) {
    echo bestellingoverzicht($_POST["bestelling"]);
}
function beschikbaarheid($merk, $os){
    // deze functie heeft twee input parameters
    // $merk verwijzt naar de merk-hash-array
    // $os verwijzt naar de os-hash-array
    // deze functie geeft de beschikbaarheid van de laptop met os terug.
    $beschikbaar = array
        ("Toshiba" => array(
            "xp"=> "U heeft voor de operating system xp gekozen.", 
            "vista" => "Deze laptop is niet beschikbaar met de vista operating system.", 
            "linux" => "U heeft voor de operating system linux gekozen."
        ),"Acer" => array(
            "xp"=> "U heeft voor de operating system xp gekozen.", 
            "vista" =>"U heeft voor de operating system vista gekozen.", 
            "linux" =>" U heeft voor de operating system linux gekozen."
        ),"Hp" => array(
            "xp"=> "U heeft voor de operating system xp gekozen.", 
            "vista" => "Deze laptop is niet beschikbaar met de vista operating system.", 
            "linux" => "Deze laptop is niet beschikbaar met de linux operating system."
        )
    );       
    return($beschikbaar[$merk][$os]);
}
function bestellingoverzicht($producten){
    $laptop["toshiba"] = "toshiba";
    $laptop["acer"] = "acer";
        
    $info = array(1 => "product","merk","model","os", "aantal","prijs");
    ?>
    <br /> 
    <table width='80%' border='1' >
        <caption>    <strong>Bestellingoverzicht</strong>    </caption>
    <thead>         
        <tr>
            <th>Product</th>
            <th>Merk</th>
            <th>Model</th>
            <th>OS</th>
            <th>Aantal</th>
            <th>Basisprijs</th>
        </tr>
    </thead>
    <tbody>
    <?php
    foreach($producten as $key => $value){
        if($value['aantal'] == 0) continue;
        print "<tr>";
        for($x=1; $x<=sizeof($info); $x++){
           print "<td>".$value[$info[$x]]."</td>";
        }
        print "</tr>";
    }
    echo "</tbody></table>";
}
?>
 
Nou de vraag is ongeveer het zelfde, alleen is het niet nodig om de code precies goed uit te schijven, ;)

Ik heb u post, meerdere malen door gelezen, voordat ik zelf deze thread in leven bracht.

Ik citeer: "
foreach($laptop as $key => $value)
{
$aantal = $value. "aantal";
if($_POST[$aantal] == 0) continue;
$reij = "<tr>";
for($x=1; $x<=sizeof($info); $x++)
{
$element = $value . $info[$x];
$reij = $reij . "<td>" . $_POST[$element] ."</td>";
}
$reij = $reij . "</tr>";
echo $reij;
}
echo "</tbody></table>";
}

je gaat hier van een array af die geen array key value is dus kan je ook geen value paken php gaat hier niets van zeggen als een variabele leeg is dus dit ga je ook moeten oplossen."

Hoewel ik redelijk wat weet van arrays, rijkt mijn kennis maar tot dusver.
Ik werk zeg maar uit een boek, maar dat boek is niet heel leerzaam. Eigenlijk wordt ik een beetje in het diepe gegooit hier.
En dus is het zo dat ik niet helemaal snapt wat u bedoelt.

Alvast bedank,
:)
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan