2025-06-06 17:14:52 +08:00

129 lines
5.5 KiB
PHP

<html>
<head>
<title>Online Fruit Store</title>
<script src="iframe.js"></script>
<script src="item_modify.js"></script>
<link rel="stylesheet" href="./cart.css" type="text/css" />
</head>
<body>
<br><br><br><br><br><br><br>
<div id="grid">
<?php
include "../ConnectDB.php";
// Start the session
session_start();
$username = mysqli_real_escape_string($conn, $_SESSION['username']);
$usertype = mysqli_real_escape_string($conn, $_SESSION['user_type']);
if($usertype == "seller"){
$a = "seller_availability";
$sql_col = "DESCRIBE seller";
$m = "seller_price";
}
else{
$a = "buyer_availability";
$sql_col = "DESCRIBE buyer";
$m = "buyer_price";
}
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
$res = mysqli_query($conn, $sql_col);
// Extract column names into an array
$col_name = array();
while ($row = mysqli_fetch_array($res)) {
$col_name[] = $row['Field'];
}
$sql_cart = "SELECT * FROM $usertype WHERE name='$username'";
$result = mysqli_query($conn, $sql_cart);
$col = mysqli_num_fields($result);
$goods = mysqli_fetch_array($result);
$total = 0;
for ($i = 3; $i < $col; $i++) {
//check availability
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
$check_sql = "SELECT $a FROM inventory WHERE name = '$col_name[$i]'";
$res = mysqli_query($conn, $check_sql);
$avail = mysqli_fetch_array($res);
if($avail[$a] == 0 && $avail[$a] != null){
echo "<script type='text/javascript'>
alert('$col_name[$i] is currently unavailable!');
</script>";
}
if($goods[$i] > 0){
$col_name_i = mysqli_real_escape_string($conn, $col_name[$i]);
//display image
$sql_img = "SELECT img FROM inventory WHERE name='$col_name_i'";
$picaddress = mysqli_query($conn, $sql_img);
$img = mysqli_fetch_array($picaddress);
//get item
$sql_price = "SELECT $m FROM inventory WHERE name='$col_name[$i]'";
$res = mysqli_query($conn, $sql_price);
$price = mysqli_fetch_array($res);
//unit price
$unitprice = $price[$m];
//total price
$total += $unitprice * $goods[$i];
//display
echo '<table id="items">';
echo '<tr>';
echo '<td width="20%">';
echo '<img src="data:image/jpeg;base64,' . base64_encode($img['img']) . '" title="' . $col_name[$i] . '" id="' . $col_name[$i] . '" onclick="details(this)">';
echo '</td>';
echo '<td width="20%">';
echo '<h4 style="margin: 0%;">' . $col_name[$i] . '</h4>';
echo '</td>';
echo '<td width="24%" id="icon">';
echo '<p>¥'.$unitprice.'</p>';
echo '</td>';
echo '<td width="6%" id="icon">';
echo '<img src="../../icon/add.svg" id="' . $col_name[$i] . '" onclick="item_add(this)">';
echo '</td>';
echo '<td width="8%">';
echo '<input type="text" style="margin: 0%;" id="' . $col_name[$i] . '" value="' . $goods[$i] . '" onfocusout="item_modify(this)">';
echo '</td>';
echo '<td width="6%" id="icon">';
echo '<img src="../../icon/subtract.svg" id="' . $col_name[$i] . '" onclick="item_subtract(this)">';
echo '</td>';
echo '<td width="6%" id="icon">';
echo '<img src="../../icon/del.svg" id="' . $col_name[$i] . '" onclick="item_del(this)">';
echo '</td>';
echo '</tr>';
echo '</table>';
}
}
if($total != 0){
echo '<br>';
echo '<table width="20%" style="text-align: center;">';
echo '<tr>';
echo '<td><h1>Total Price: ¥ '.$total.'</h1></td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo '<form action="checkup.php">';
echo '<button type="submit">Check up</button>';
echo '</form>';
echo '</td>';
echo '</tr>';
echo '</table>';
}
else{
echo '<h1>Nothing Here</h1>';
}
}
?>
</div>
</body>
</html>