【问题标题】:How to fill combo box in a td of a table如何在表格的 td 中填充组合框
【发布时间】:2017-09-27 19:04:02
【问题描述】:

我想从数据库中检索一些数据并将它们显示在一个表中,我需要将一个字段Order_Status 作为一个组合框,它的数据从另一个表中填充并添加到开头组合从数据库返回的行的结果。

Data Sample from database

数据需要如下所示,红色部分为组合框。 Required Result

问题是我无法将字段 Order_status 的结果作为组合框显示在表格的 td 中

$stmt ="SELECT distinct Order_ID,Customer_ID,Required_Date,Order_Status FROM Orders where Required_Date between '".$SDate."' and '".$EDate."'";
$row = $conn->query($stmt)->fetchall(PDO::FETCH_ASSOC);

if (count($row) > 0)
{
    $output.='<hr />
            <table class="table1">
                <tr>
                    <th>Order No.</th>
                    <th>Customer Name</th>
                    <th>Order Details</th>
                    <th>Delivery Date</th>
                    <th>Order Status</th>
                </tr>
    ';
    foreach ($conn->query($stmt) as $rows) 
    {
        //Getting Customer Name
        $sql="SELECT nick_name_ FROM Customers where Cust_id='".$rows["Customer_ID"]."'";
        $result=$conn->query($sql)->fetch(PDO::FETCH_ASSOC);

        //Getting Order Data
        $query="SELECT * FROM Order_Product where Order_ID='".$rows["Order_ID"]."'";

        foreach ($conn->query($query) as $results) 
        {
            $newsql="SELECT Category_Name from Categories where Category_ID='".$results['Category_ID']."'";
            $newresult=$conn->query($newsql)->fetch(PDO::FETCH_ASSOC);
            $CatName=$newresult['Category_Name'];

            $newsql="SELECT Product_Name from Products where Product_ID='".$results['Product_ID']."'";
            $newresult=$conn->query($newsql)->fetch(PDO::FETCH_ASSOC);
            $ProName=$newresult['Product_Name'];

            $output.='
            <tr>
                <td>'.$rows['Order_ID'].'</td>
                <td>'.$result['nick_name_'].'</td>
                <td>'.$CatName.",".$ProName." ".$results['Amount'].'</td>
                <td>'.$rows['Required_Date'].'</td>
            ';
            $stmt = "SELECT * FROM Order_Status WHERE Status_Name !='".$rows['Order_Status']."'";
            //$res = $conn->query($sql)->fetch(PDO::FETCH_ASSOC);

            foreach ($conn->query($stmt) as $res) 
            {

                $output.='<td>'
                ?>
                    <option value="<?php echo $res['Status_ID']; ?>"><?php echo $res['Status_Name']; ?></option>
                <?php
                '</td>
                </tr>
                ';
            }

        }
    }

    $output.='</table>';
    $bool_is_data_saved = true;
    echo $output;
}

if(!$bool_is_data_saved)
{
    echo ("Failed");
}

【问题讨论】:

  • 这是顶天立地!你有什么问题?为什么将查询语句作为一行进行foreach>???

标签: php sql html pdo combobox


【解决方案1】:

那段代码……很难读……

对我来说,在实际查询中执行以下操作而不是 foreach 似乎更简洁:

$stmnt = $conn->prepare("SELECT * FROM table WHERE id=?");
$stmnt->execute(array($someId));
while ($result = $stmnt->fetch()){
    //do stuff
}

另外,在 foreach 中跳出 PHP 然后转身并在 HTML 中回显是很奇怪的。把它清理干净。

最后,为了使这更易于管理,我将使用您从 HTML 中调用的几个函数。将函数放在文档的顶部。

要回答您的问题,我认为问题在于您没有定义选择。

这样做:

$output.='<td>';
$output .= '<select name="comboBox">';

foreach ($conn->query($stmt) as $res) {
    $output .= '<option value="'.$res['Status_ID'] .'">'.$res['Status_Name'],'</option>';
}
$output .= '</select>';
$output .= '</td>';

还有一件事! 您在两个不同的地方为两个不同的查询使用 $stmnt 变量。要么使用不同的变量,要么将查询放入函数中。

【讨论】:

    猜你喜欢
    • 2018-03-03
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 2015-06-16
    • 2020-03-14
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多