Skip to content Skip to sidebar Skip to footer

Creating A Dynamic Drop Down Menu When Returning A MySQL Result Set

I am returning a result set from a mySQL database into a table using 5 columns. So far so good the table is displaying the correct field data. I would like to know how I should go

Solution 1:

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

This is only sample. You can use according to your requirements


Solution 2:

<?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>";
?>

Then write a JS function myJSFunction to handle the change event.


Solution 3:

Like this. Note that the $options are outside of the loop. I also fixed the position of your <h4> tag - it was inside the <table> tag before.

<?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>";

Solution 4:

You could also use:

<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>';
                  }
            ?>          


Post a Comment for "Creating A Dynamic Drop Down Menu When Returning A MySQL Result Set"