douweilaton2762 2014-07-08 14:17
浏览 21
已采纳

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:

enter image description here

Desired format (table headings just on top):

enter image description here

<?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>
  • 写回答

3条回答 默认 最新

  • douzhong1907 2014-07-08 14:23
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图