0

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>
3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by cotrac
0

Is it too late to change the design of the 'children' table? Your problem comes from there.

Why not have only two columns? One column would identify the user and the other the name of one child. Each user could have as many rows in the children table as needed to identify all that user's children. If you must enforce a maximum of four children per user you could do that in your program logic. But a one-many relation between the user table and the children table would give you more flexibility than your actual design, in my opinion.

0

as above ^

Your table is not normalised and forces you to use bad code to find the children. Imagine checking to see if fred has a child called jimmy - you'd have to query each column via an or, whereas with a proper;y normalised database, you'd just search on user=fred and child = jimmy.

table = users - their data
table userkids - user and childname, that's all (unless you need to store more about each child)

Then your select for the drop down is just child where username = whatever.

Edited by drjohn: n/a

0

Thank you both very much! You are right about the table being set up wrong as well as my code for that function being wrong. I will restructure the table with your suggestions in place. This one can be marked as solved ;)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.