我目前正在使用购物车制作一个超级简单的网上商店创意。最后,我想打印出一张包含您订购产品的表格。目前使用foreach但我不知道如何解决这个问题。我尝试使用会话作为一种方式,让循环了解订购了多少不同的产品。但似乎$ _SESSION方式将占用当前所有活动会话。并使用一个简单的变量f.e. $ piano将打印2行(在我的商店中使用2个钢琴,尝试使用if(会话激活)$ piano1 = active来解决它。但似乎foreach声明并没有给出一个呐喊关于那个,反正会打印2行。
很抱歉长文本块。这是我的页面。再次道歉。我刚刚开始使用php。
变量名称是荷兰语,但我认为这对你们来说并不重要。 从第103行开始。 提前谢谢!
<div class="logincontainer"><!-- Php Session Script Actief? -->
<?php
session_start();
if(isset($_SESSION['naam'])) :
echo "<div class='content_login'>";
echo "Hallo " . $_SESSION['naam'] . ". Welkom bij de Pianoshop.</br></br>";?>
<form method='post' action='uitlog.php'>
<input type='submit' name='loguit' Value='Loguit!'></form><br />
<form action='winkelmand.php' class="left">
<input type='image' src='images/winkelwagen-knop.png'/>
</form><br />
<form method='post' name='emptycart' action='emptycart.php' class="right">
<input type="submit" id="submitpic" name="leegwinkelmand" value="">
<?php
if(isset($_SESSION['winkelmand'])) {
echo $_SESSION['aantalproducten'] . " Item(s) - €" . $_SESSION['totaalprijs'] . ",-";
} else {
echo "Jouw winkelwagen is leeg.";
}?>
</form>
</div>
<?php else :?>
<div class='content_login'>
<form method='post' action='checklogin.php'>
<p><input type='text' name='gebruikersnaam' required='required' value='' placeholder='Gebruikersnaam'></p>
<p><input type='password' name='password' required='required' value='' placeholder='Wachtwoord'></p>
<font color="red"><p class='submit'>
<input type='submit' name='login' value='Login'>
<?php if(isset($_SESSION['logged_in'])) :?>
Verkeerd wachtwoord.
<?php session_destroy();
endif; ?>
</p></font>
<p>Nog niet geregistreerd? Doe dat <a href="account.php">hier!</a>.</p>
</form>
</div>
<?php endif; ?></div>
<div id="site">
<div id="menubar">
<div id="logo">
<img src="images/pianotoetsen.png" >
</div>
<div id="menu_items">
<ul id="menu">
<li><a href="index.php">Home</a></li>
<li><a href="toetsinstrumenten.php">Toetsinstrumenten</a></li>
<li><a href="account.php">Jouw account</a></li>
<li class="current"><a href="winkelmand.php">Winkelmand</a></li>
<li><a href="contact.php">Contact</a></li>
</ul>
</div></div>
<div id="site_content">
<div class="sidebar_container">
<div class="sidebar">
<h2>Sale!</h2>
<div id="thumbnail"><img src="images/piano1.jpg"></div>
<p>Yamaha CLP-575 voor maar €2599,- !</p>
<div id="thumbnail"><img src="images/piano2.jpg"></div>
<p>Ritmuller 120SL €4999,- !</p>
</div>
<div class="sidebar">
<h2>Laatste Updates</h2>
<h3>Juni 2015</h3>
<p>Site in constructie.</p>
</div>
<div class="sidebar">
<h3>Wij zijn op Facebook</h3>
<p>Klik <a href="#">hier.</p></a>
</div>
</div>
<div id="content">
<div id="wallpaperbanner">
<img src="images/banner.jpg">
</div>
<div class="content_item">
<h1>Winkelmand</h1>
<?php
$user = 'root';
$pass = '';
$db = 'online shop';
$conn = mysql_connect('localhost', $user, $pass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if(isset($_SESSION['winkelmand'])) {
echo "Deze producten staan in je winkelwagen</br></br>";
if(isset($_SESSION['totaalprijs2']) == 0) {
// Do nothing?
} else {
mysql_select_db($db);
$query = mysql_query("SELECT * FROM product WHERE productnummer='2'");
$productgegevens = mysql_fetch_row($query);
$piano["piano2"] = "ritmuller";
$pianoarray[1] = $productgegevens['1'];
$pianoarray[2] = $productgegevens['2'];
$pianoarray[3] = $productgegevens['4'];
$pianoarray[5] = $productgegevens['3'];
$pianoarray[4] = $_SESSION['aantal_prod2'];
}
if(isset($_SESSION['totaalprijs1']))
{
mysql_select_db($db);
$query = mysql_query("SELECT * FROM product WHERE productnummer='1'");
$productgegevens = mysql_fetch_row($query);
$piano["piano1"] = "yamaha";
$pianoarray[4] = $_SESSION['aantal_prod1'];
$pianoarray[1] = $productgegevens['1'];
$pianoarray[2] = $productgegevens['2'];
$pianoarray[3] = $productgegevens['4'];
$pianoarray[5] = $productgegevens['3'];
$pianoarray[4] = $_SESSION['aantal_prod2'];
}
echo "<br />
<table width='80%' >
<thead>
<tr><th>Productnaam</th><th>Merk</th>
<th>Voorraad</th><th>Aantal</th><th>Prijs</th>
</tr>
</thead>
<tbody>";
foreach($piano as $key => $value) {
echo $key . "</br>" . $value . "<br />";
$row = "<tr>";
for ($x=1; $x<=sizeof($pianoarray); $x++){
$row = $row . "<td>" . $pianoarray[$x] . "</td>";
}
$row = $row . "</tr>";
echo $row;
}
echo "<tr><td></td><td></td><td></td><td></td><td>" . '€' . $_SESSION['totaalprijs'] . ',-' . "</td></tr></tbody></table>";
}
else {
echo "Jouw winkelwagen is leeg. <br />" . "Klik <a href='toetsinstrumenten.php'>Hier</a> om wat items toe te voegen.";
}?>
</div>
</div>
</div>
</div>
答案 0 :(得分:0)
在$ _SESSION数组中创建一个数组变量,并在该
上执行foreach循环$_SESSION['cart']['piano1'] = 'piano1';
$_SESSION['cart']['piano2'] = 'piano2';
$cart = $_SESSION['cart'];
foreach ($cart as $key => $item) {
//do something with $item or $key
}