accountUser.php
<?php
session_start();
if (!isset($_SESSION["user"]))
header("Location: login.php");
else {
$mysql = new mysqli("localhost", "root", "root", "electronicworld");
?>
<article id="shoppingCart">
<form name="arrangeForm" action="arrange.php" method="post">
<table>
<tr>
<th>Наименование товара</th>
<th>Количество</th>
<th>Цена</th>
<th>Инструменты</th>
</tr>
<?php
if (!isset($_SESSION["cart"])) {
?>
<tr><td colspan="4" style="text-align:center;">Корзина пуста!</td></tr>
<?php
} else {
foreach ($_SESSION["cart"] as $id => $value) {
$array = $array.$id.",";
}
$sql = "SELECT * FROM `products` WHERE `idProduct` IN (".substr($array, 0, -1).") ORDER BY `idProduct` ASC";
$products = $mysql -> query($sql);
$counter = 0;
while ($product = $products -> fetch_assoc()) {
?>
<tr>
<td><?=$product["nameProduct"];?></td>
<td><?=$_SESSION["cart"][$product["idProduct"]]["quantity"];?></td>
<td><?=intval(str_replace(" ", "", $product["cost"]))*$_SESSION["cart"][$product["idProduct"]]["quantity"]." руб.";?></td>
<td></td>
</tr>
<?php
}
}
?>
</table>
<input type="submit" name="arrange" value="Оформить">
</form>
</article>
<?php
$mysql -> close();
}
?>
arrange.php
<?php
session_start();
$mysql = new mysqli("localhost", "root", "root", "electronicworld");
$sumProducts = 0;
$numberProducts = 0;
foreach ($_SESSION["cart"] as $id => $value) {
if ($_SESSION["cart"][$id]["quantity"] != 1)
$quantity = $_SESSION["cart"][$id]["quantity"];
else
$quantity = 1;
$sumProducts += ($id*$quantity);
$numberProducts += $quantity;
$array = $array.$id.",";
}
$numberOrder = $sumProducts.$numberProducts.rand(10,99);
$nameUser = $_SESSION["user"]["name"];
$numberProducts = (string)$numberProducts;
$sql = "SELECT * FROM `products` WHERE `idProduct` IN (".substr($array, 0, -1).") ORDER BY `idProduct` ASC";
$products = $mysql -> query($sql);
while ($product = $products -> fetch_assoc())
$namesProducts = $namesProducts.$product["nameProduct"]." x ".$_SESSION["cart"][$product["idProduct"]]["quantity"].";<br>";
$namesProducts = substr($namesProducts, 0, -5);
$status = "adopted";
$mysql -> query("INSERT INTO `orders` (`numberOrder`, `nameCustomer`, `numberProducts`, `namesProducts`, `status`) VALUES ('$numberOrder', '$nameUser', '$numberProducts', '$namesProducts', '$status')");
$mysql -> close();
header("Location: accountUser.php");
?>