Files
northwind-ordini-php/index.php

83 lines
3.2 KiB
PHP

<?php
require_once "db_conf.php";
$customer_id = $_GET["customerId"];
$order_id = $_GET["orderId"];
$sql = "";
if (isset($_GET["customerId"])) {
$sql = "SELECT o.* FROM Orders o INNER JOIN Customers c ON o.CustomerID = c.CustomerID WHERE c.CustomerID = " . $customer_id . ";";
}
else if (isset($_GET["orderId"])) {
$sql = "SELECT p.*, od.quantity AS order_quantity FROM Orders o INNER JOIN OrderDetails od ON o.OrderID = od.OrderID INNER JOIN Products p ON od.ProductID = p.ProductID WHERE o.OrderID =" . $order_id . ";";
}
else {
$sql = "SELECT DISTINCT CustomerID, CustomerName FROM Customers;";
}
$result = mysqli_query($conn, $sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Northwind Clients</title>
<link rel="stylesheet" href="style.css">
<style>
a{
color: white;
}
</style>
</head>
<body>
<?php
if($result){
if((isset($_GET["customerId"]) || isset($_GET["orderId"])) && mysqli_num_rows($result) > 0) {
echo "<a href='index.php'>Torna alla lista clienti</a><br><br>";
echo "<form method='GET' action='index.php'>";
echo "<select id='orderId' name='orderId'>";
while($row = mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['OrderID'] . "'>" . $row['OrderID'] . "</option>";
}
echo "</select>";
echo "<input type='submit' value='Visualizza Dettagli Ordine'>";
echo "</form><br>";
# Reset index della query
mysqli_data_seek($result, 0);
}
else if (mysqli_num_rows($result) <= 0){
echo "<a href='index.php'>Torna alla lista clienti</a><br><br>";
echo "Nessun ordine trovato per questo cliente.";
}
if(isset($_GET["orderId"])){
echo "<table>";
echo "<tr>";
echo "<th>ID Prodotto</th><th>Nome Prodotto</th><th>Quantità per unità</th><th>Prezzo</th><th>Quantità ordinata</th>";
echo "</tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['ProductID'] . "</td>" ;
echo "<td>" . $row['ProductName'] . "</td>" ;
echo "<td>" . $row['Unit'] . "</td>";
echo "<td>" . $row['Price'] . "$</td>" ;
echo "<td>" . $row['order_quantity'] . "</td>";
echo "</tr>";
}
}
if(!isset($_GET["customerId"]) && !isset($_GET["orderId"])){
echo "<table>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['CustomerID'] . "</td>" ;
echo "<td>" . $row['CustomerName'] . "</td>" ;
echo "<td><a href='index.php?customerId=" . $row['CustomerID'] . "'>Visualizza Ordini</a></td>" ;
echo "</tr>";
}
echo "</table>";
}
}
?>
</body>
</html>