0

i have stored some value into DB table and want to get them displayed on my form with a restriction that the column title in DB and the Value of the previous feild are same and only the cloumn rows are shown inthe following feild in the form....!!!! Cant Figure out the Code

2
Contributors
3
Replies
25
Views
4 Years
Discussion Span
Last Post by diafol
0

no this is another question ....

let me make it a bit clearer

i made a table in data base with columns one two three four five
i have added rows under them as red blue purple

now this list varies like under One there are Five colours and under three there two.

in my main form where the user is getting registered if he selects Three that is not actually from data base but a simple drop down menue from my class i want the next drop down to be populated with values of colour in DB and and the user could see it. i am un able to code ....!!

0

I'm not entirely sure but something like this?
I think you're using vanilla mysql_* functions, so I'll do that, but bear in mind that this is deprecating, so moving to mysqli or PDO is strongly suggested.

$result = mysql_query("SELECT colour_id, colour_name FROM colours ORDER BY colour_name");
$drop = "\n\t<option value='0'>(not chosen)</option>";
while($data = mysql_fetch_assoc($result)){
    $drop .= "\n\t<option value='{$data['colour_id']}'>{$data['colour_name']}</option>";
}

//then in the appropriate place in your code:

<select id='colours1' name='colours1'>
    <?php echo $drop;?>
</select>
<select id='colours2' name='colours2'>
    <?php echo $drop;?>
</select>
<select id='colours3' name='colours3'>
    <?php echo $drop;?>
</select>
<select id='colours4' name='colours4'>
    <?php echo $drop;?>
</select>
<select id='colours5' name='colours5'>
    <?php echo $drop;?>
</select>

If you want to use them in an edit form with the selected values from the DB for a particular user, that could be more complicated. For that you'r need to loop through each colour in each instance of $drop and match the DB value to the option value, and where there's a match, you need to insert 'selected' or 'selected="selected"', depending on your html preference.

Edited by diafol

This topic has been dead for over six months. 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.