View Full Version : Populating a select form with PHP
mgallen
June 28th, 2004, 08:05 PM
Hi everyone, I have a Mysql databse and some of the fields inside have multiple values , for example the color field has: blue, red, green. I want to store the values inside a select form using PHP.
If anyone can point me to some tutorials or examples of how to do this it will be realy great.
mgallen
Hans Kilian
June 28th, 2004, 08:20 PM
I assume you want to do a drop-down box for your database field. So you're going to want to generate some HTML in a form that looks something like this:
<select name="color">
<option value="blue" selected>blue</option>
<option value="red">red</option>
<option value="green">green</option>
</select>
I've put the 'selected' attribute on blue here. You want to make your PHP script put it on the color that's already in your database.
That's it. When you get it back it'll be in $_POST['color'] ready to store back in the database.
mgallen
June 29th, 2004, 01:35 PM
Thanks Hans, but the color value is alrady in the database, and I want the values of color be desplayed in the drop-down box. The code I have is something like this:
<?php
print ('<select name="color">');
for ($i=0; $i<$my_colors; $i++){
print ('<option value='$my_colors'>$my_colors</option>');
}
But I can't take one color at a time, for example the value of $my_colors is red,green,blue. So the option displays red,green,blue.
Hans Kilian
June 29th, 2004, 03:17 PM
OK. I misunderstood. You need to split up the value from your table and then use each value like this:
<?php
print ('<select name="color">');
$colors = split(',', $mycolors);
for ($i=0; $i<count($colors); $i++) {
print '<option value=' . $colors[$i] . '>' . $colors[$i] . '</option>';
}
mgallen
June 29th, 2004, 03:25 PM
I thought so but the value in the drop down box displays 'array' (whith out the quotes) The code looks now like this:
while ($myrow= mysql_fetch_row($resultado)){
print ('<select name="color">');
$colors = split('.', $myrow);
for ($i=0; $i<count($colors); $i++) {
print '<option value=' . $colors[$i] . '>' . $colors[$i] . '</option>';
}
}
Hans Kilian
June 29th, 2004, 03:57 PM
You need to change
$colors = split('.', $myrow);
to
$colors = split(',', $myrow['column_name']);
Where column_name is the name of your column in the table. You also need to change the period to a comma if your colour values are separated by commas (and you indicated they were in post #3).
mgallen
June 29th, 2004, 07:11 PM
Thanks Hans, done. It finaly works, the code is:
while ($myrow = mysql_fetch_row($resultado)){
print ('<select name="color">');
$colors = split(',', $myrow[0]);
for ($i=0; $i<count($colors); $i++) {
print '<option value=' . $colors[$i] . '>' . $colors[$i] . '</option>';
}
print "</select>";
}
I'm using 0 in split(',',$myrow[0]) because only mysql_fetch_array lets you call the array value by colum_name
Thanks a lot!!! IOU one :)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.