简体   繁体   English

如何在表格的TD中填充组合框

[英]How to fill combo box in a td of a table

I want to retrieve some data from the database and show them in a table, i need to make one field Order_Status to be as a combo box, its data is filled from another table and to add in the beginning of the combo the result of the row returned from the database. 我想从数据库中检索一些数据并将它们显示在一个表中,我需要使一个字段Order_Status作为组合框,其数据从另一张表填充,并在组合的开头添加从数据库返回的行。

Data Sample from database 来自数据库的数据样本

The data needed to be like the below, in the red section to be combobox. 数据需要像下面一样,红色部分为组合框。 Required Result 所需结果

The problem is that i couldn't put the result for the field Order_status to appear as a combo box in the td of the table 问题是我无法将字段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");
}

That code is... hard to read... 该代码...很难阅读...

To me it seems cleaner to do the following instead of the foreach on the actual query: 对我来说,在实际查询中执行以下操作而不是foreach似乎更干净:

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

Also, breaking out of the PHP in the foreach to then turn around and echo inside the HTML is odd. 同样,在foreach中突破PHP,然后转向并在HTML内部回显是奇怪的。 Clean that up. 清理一下。

Lastly, in order to make this more manageable, I would use a couple of functions that you call from the HTML. 最后,为了使其更易于管理,我将使用您从HTML调用的几个函数。 Put the functions in the top of the doc. 将功能放在文档顶部。

To answer your question, I think the problem is that you do not define a select. 为了回答您的问题,我认为问题是您没有定义选择。

do this: 做这个:

$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>';

One more thing! 还有一件事! You are using $stmnt variable in two different places for two different queries. 您在两个不同的地方对两个不同的查询使用$ stmnt变量。 either use a different variable or put your queries in functions. 使用其他变量或将查询放入函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM