Forum Moderators: coopster
<?php
//DB connection code here....
//$member stores the member name obtained from the seesion variable. This is created when
//member logs in.... This part works fine with the rest of the program.
//Run query
$q = "SELECT name FROM style_custom WHERE member = '$member'";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r)) {
$val = $row["name"];
}
?>
<html>
<head>
</head>
<body>
<form>
<select name="grab">
<br /><option value="<?php echo $val ?>"><?php echo $val ?><br/></option>
</select>
</form>
</body>
</html>
You can do two things in this case: either create an empty array to store each `name` value through each loop iteration, and then use it to create the SELECT element's OPTION set, or simply output the OPTION set within the loop. The latter is probably easier and more efficient, because you only have to use a single loop structure to make it work.
Let me give you an example using your code.
// standard query, OK
$q = "SELECT name FROM style_custom WHERE member = '$member'";
// basic call to execute the query, fine,
// but could use some troubleshooting in case it fails
$r = mysqli_query($dbc, $q);
// output the SELECT
echo '<select name="grab" id="grab">';
// a default "blank" option, if you like
echo '<option value=""></option>';
// begin looping through the resultset, creating the OPTION set
while ($row = mysqli_fetch_array($r)) {
echo "<option value='{$row['name']}'>{$row['name']}</option>";
}
// end the SELECT element
echo '</select>';
Note that the $row['name'] element is simply passed into the string that outputs the OPTION element. The curly braces surrounding it help the parser identify the array element within and not confuse the 'single quotes'.
sigh. I miss PHP bbcode tags