Creating a dropdown from a MySql query is quite easy
There are a few ways, simplest is to create an array then just loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<select> <option selected="selected">Choose one</option> <?php // A sample animal array $animals = array("Dog", "Fish", "Rabbit", "Wolf"); // Iterating through the animals array foreach($animals as $item){ echo "<option value='strtolower($item)'>$item</option>"; } ?> </select> |
Another method without an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $sql = "select clientname,id from client group by clientname"; if(!$result = $mysqli->query($sql)){ die('There was an error running the query [' . $mysqli->error . ']'); } ?> <select id="siteclientname" name="siteclientname" class="form-control"> <?php echo "<option value=''>Choose</option>"; while($row = mysqli_fetch_array($result)) { echo "<option value='".$row['id']."'>".ucfirst($row['clientname'])."</option>"; } // echo "<option>New Client</option>"; ?> </select> |
Maybe pull three things…..
1 2 3 |
echo "<option value='" . $row['ID'] ."'>" . $row['client'] ." - ". $row['campaign'] ." - ". $row['ID'] ."</option>";} |
And for two dropdowns take a look here.