Hello! This is my very first post, anywhere, asking for advice.
I have a mysql table named "children". It has 5 columns. "username" "child1" "child2" "child3" "child4"
Based on my register form, A user creates a username and list the names of children he/she has (up to 4) on submit, it updates the "children" table just fine. I have written a ugly code to populate a dropdown with the names of the children based on their "username"
It works ok, except for 1 problem. If the user has only, lets say, 2 kids, my dropdown displays their names BUT also 2 blank selections. How do I display only the names of the children they have but not the blank selections? The following code is clunky and works but is there a better way of doing this? Thanks in advance!
Cotrac
<?php
session_start();
$user = $_SESSION['user'];
include 'dbc.php';
$query = ("select `child1`,`child2`,`child3`,`child4` FROM children WHERE username='$user'");
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
?>
<form>
Children:
<select name="child_menu">
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo '
<option value="'.$row['child1'].'"'.
(isset($_SESSION['$user']) && $_SESSION['$user'] == $row['child1'] ? 'selected="selected"' : '').'>'.
$row['child1'].'</option>
<option value="'.$row['child2'].'"'.
(isset($_SESSION['$user']) && $_SESSION['$user'] == $row['child2'] ? 'selected="selected"' : '').'>'.
$row['child2'].'</option>
<option value="'.$row['child3'].'"'.
(isset($_SESSION['$user']) && $_SESSION['$user'] == $row['child3'] ? 'selected="selected"' : '').'>'.
$row['child3'].'</option>
<option value="'.$row['child4'].'"'.
(isset($_SESSION['$user']) && $_SESSION['$user'] == $row['child4'] ? 'selected="selected"' : '').'>'.
$row['child4'].'</option>';
}
?>
</select>
</form>