SQL下拉选择显示到PHP表中,标题仅在顶部
I am pulling stuff from a DB and populating it into a drop-down select. When the user selects the query, I want it to be displayed in a table (which it is now). But the problem is with the format. I want the headers to be displayed just on the top of the table rather than for every single row. What is wrong with my code? Any ideas/suggestions? Thanks in advance.
Resulting page of code:
Desired format (table headings just on top):
<?php
session_start();
if(!isset($_SESSION['EmployeeID'])){
$URL="Logon.php";
header ("Location: $URL");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Orders Page</title>
</head>
<body>
<h1>Orders Information Page</h1>
<form action="Orders.php" method="post">
<?php
require "Information.php";
try{
$database="Northwind";
$conn = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
} catch(PDOException $e){
$conn->setArribute(PDO::ATT_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Could not open database.";
}
$lname= $_POST['txtFirstName'];
$extension = $_POST['txtLastName'];
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$_SESSION['OrderID']=$_POST['selOrder'];
} //end if is a post
echo "<select name='selOrder'>";
$sql = $conn->prepare("SELECT * FROM orders WHERE EmployeeID=? ORDER BY CustomerID ASC");
if ($sql->execute(array($_SESSION['EmployeeID']))) {
while ($row = $sql->fetch()) {
echo "
\t<option value='" . $row['OrderID'] . "'";
if(isset($_SESSION['OrderID']) && $_SESSION['OrderID']== $row['OrderID']){
echo " selected='selected' ";
}
echo ">";
echo "Cust:" . $row['CustomerID'] . " ordered on " . $row['OrderDate'];
echo "</option>";
}
}
echo "</select>";
?>
<input name="btnSubmit" type="submit" value="Submit" />
<?php
if(isset($_SESSION['OrderID'])){
echo "<h3>Order Details</h3><p>";
$sql = $conn->prepare("SELECT * FROM orders, orderdetails, products WHERE orders.OrderID=orderdetails.OrderID AND products.ProductID=orderdetails.ProductID AND orders.OrderID=? ");
if ($sql->execute(array($_SESSION['OrderID']))) {
while ($row = $sql->fetch()) {
echo "<table border = '1'>
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
{
echo "<tr>";
echo "<td>" . $row['OrderID'] . "</td>";
echo "<td>" . $row['OrderDate'] . "</td>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
}
echo "</p>";
}
$conn =null;
?>
</form>
</body>
</html>
Maybe I understand the problem very poorly, or do not understand at all, but is
...
echo "<table border = '1'>
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
while ($row = $sql->fetch()) {
echo "<tr>";
echo "<td>" . $row['OrderID'] . "</td>";
echo "<td>" . $row['OrderDate'] . "</td>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "</tr>";
}
echo "</table>";
...
not what you are looking for? Eg displaying the <th>
's only once?
This is not very difficult, you just made a little mistake.
Get the
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
part out of the while loop, just before the while loop, and it will work like how you want it to.
Hope this helps.
move
"<table border = '1'><tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
(and while you're at it the echo "</table>";
)
out of the while loop:
echo "<table border = '1'><tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
while ($row = $sql->fetch()) {
echo "<tr>";
echo "<td>" . $row['OrderID'] . "</td>";
echo "<td>" . $row['OrderDate'] . "</td>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "</tr>";
}
echo "</table>";
note that you had two extra curly braces that weren't doing anything.