0

我正在使用 5 列将结果集从 mySQL 数据库返回到表中。到目前为止,表格显示了正确的字段数据。我想知道我应该如何为每一行创建一个下拉菜单?这将是名为“状态”的第 6 列,由三个值组成,这些值将改变行的外观。另一件要提的是,“状态”不会链接到数据库。这是我当前的代码:

<?php
$result = mysql_query("SELECT * FROM somewhere")
or die (mysql_error());
?>
<table class="table1" >
<h4>Orders</h4>
<tr>
<th>Number</th>
<th>Date</th>
<th>Ordered By</th>
<th>Supplier</th>
<th>Price</th>
<th>Status</th>
</tr>
<?php
while($row=mysql_fetch_array($result)){
echo "</td><td>";
echo $row['Orderno'];
echo "</td><td>";
echo $row['Orderdate'];
echo "</td><td>";
echo $row['Orderedby'];
echo "</td><td>";
echo $row['Supplier'];
echo "</td><td>";
echo $row['totalprice'];
echo "</td><td>";
echo $row['Status'];
echo "</td></tr>";
}
echo "</table>";
?>
4

4 回答 4

1
<select>
    <?php
    while($row=mysql_fetch_array($result)){
     ?>
     <option value = "<?php echo $row['Orderno']?>">
          <?php $row['other']?>
     </option>
     <?php
     }
     ?>
</select>

这只是样本。您可以根据自己的要求使用

于 2012-08-29T14:42:49.710 回答
0

像这样。请注意,$options它们在循环之外。我还固定了<h4>标签的位置 - 它之前在<table>标签内。

<?php

function create_select($name, $options = array(), $selected = null)
{
    $html = '<select name="'.$name.'">';
    foreach ($options as $k => $v)
    {
        $html .= '<option value="'.$k.'"';
        if ($k == $selected)
        {
            $html .= ' selected';
        }

        $html .= '>'.$v.'</option>';
    }

            $html .= '</select>';

    return $html;
}

$result = mysql_query("SELECT * FROM somewhere")
or die (mysql_error());
?>
<h4>Orders</h4>
<table class="table1" >
    <tr>
        <th>Number</th>
        <th>Date</th>
        <th>Ordered By</th>
        <th>Supplier</th>
        <th>Price</th>
        <th>Status</th>
    </tr>
<?php
$options = array(
    'despatched' => 'Despatched',
    'pending' => 'Pending'
    // etc...
);

while($row=mysql_fetch_array($result))
{
    echo "</td><td>";
    echo $row['Orderno'];
    echo "</td><td>";
    echo $row['Orderdate'];
    echo "</td><td>";
    echo $row['Orderedby'];
    echo "</td><td>";
    echo $row['Supplier'];
    echo "</td><td>";
    echo $row['totalprice'];
    echo "</td><td>";
    echo create_select('status_'.$row['id'], $options, $row['Status']);
    echo "</td></tr>";
}
echo "</table>";
于 2012-08-29T14:44:26.247 回答
0
<?php
    $result = mysql_query("SELECT * FROM somewhere")
    or die (mysql_error());
    ?>
    <h4>Orders</h4>
    <table class="table1" >
        <tr>
        <th>Number</th>
        <th>Date</th>
        <th>Ordered By</th>
        <th>Supplier</th>
        <th>Price</th>
        <th>Status</th>
    </tr>
    <?php
    while($row=mysql_fetch_array($result)){
        echo "</td><td>";
        echo $row['Orderno'];
        echo "</td><td>";
        echo $row['Orderdate'];
        echo "</td><td>";
        echo $row['Orderedby'];
        echo "</td><td>";
        echo $row['Supplier'];
        echo "</td><td>";
        echo $row['totalprice'];
        echo "</td><td>";
        echo '  <select id="'.$row['Orderno'].'" onchange="myJSFunction(this)">
                    <option>Example</option>
                    <option>Example 2</option>
                </select>';
        echo "</td></tr>";
    }
    echo "</table>";
?>

然后编写一个 JS 函数 myJSFunction 来处理 change 事件。

于 2012-08-29T14:45:26.413 回答
0

您还可以使用:

<select class="form-control" name="job_sector" id="job_sector">
            <?php
                $num_results = mysqli_num_rows($result);
                for ($i=0;$i<$num_results;$i++) {
                  $row = mysqli_fetch_array($result);
                  $name = $row['job_title'];
                  echo '<option value="' .$name. '">' .$name. '</option>';
                  }
            ?>          

于 2019-05-04T21:14:44.067 回答