返回mySQL结果集时创建动态下拉菜单

时间:2012-08-29 14:38:47

标签: php mysql sql

我将mySQL数据库中的结果集返回到使用5列的表中。到目前为止,表格显示正确的字段数据。我想知道如何为每一行创建一个下拉菜单?这将是名为“Status”的第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 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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>";

答案 2 :(得分: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事件。

答案 3 :(得分: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>';
                  }
            ?>