Hello, i would like to have one collumn in my db table that holds data such as pepsi,diet pepsi,coke,diet coke,sprite,root beer and what i need to do is parse it out with php so i can build a radio button for each option in that collumn.
| SitePoint Sponsor |
Hello, i would like to have one collumn in my db table that holds data such as pepsi,diet pepsi,coke,diet coke,sprite,root beer and what i need to do is parse it out with php so i can build a radio button for each option in that collumn.
Have you already created the table and the column? Persuming that the name of the table is `drinks` and the column is called `name`, you would have something like this:
Where $link is your mysql_connect functionPHP Code:$query = "SELECT `name` from `drinks`";
$result = mysql_query($query,$link);
while ($row = mysql_fetch_array($result)){
print "<input type='radio' name='drink' value='$row[name]' /> $row[name]";
}
im talking about having the options.... pepsi,coke,sprite all in one line of the collumn not in individual cells.
oh right ok, sorry, misunderstood.
Ok, all you need to do is explode the string to get an array....
In this example, $list contains the string of drinks...
Hope this helpsPHP Code:$list = explode("," $list); # Explode splits a string into an array by the deliminator specified (here its ",")
$x = 0;
while ($x < count($list)){ #Loop around the array
print "<input type='radio' name='drink' value='$list[$x]' /> $list[$x]";
$x++ ;
}
![]()
hrmm trying to get it to work keeps giving me the white screen hehe.
the collumn where the strings of options are located is food_description
and whats the table called?
food_descriptions
Ok, I'm off now anyway, but this is what you need to do:
All that needs changing is substituting $link for the resource that holds your mysql_connect - persuming you have already connected to the DBPHP Code:$query = "SELECT `food_description` from `food_descriptions`";
$result = mysql_query($query,$link);
$row = mysql_fetch_array($result);
$list = $row["food_description"];
$list = explode(",", $list); # Explode splits a string into an array by the deliminator specified (here its ",")
$x = 0;
while ($x < count($list)){ #Loop around the array
print "<input type='radio' name='drink' value='$list[$x]' /> $list[$x]";
$x++ ;
}
Post a reply if you have any issues, and I'll check back in the morning![]()
it does not appear to be pulling any data hrmm.



If you want to have a selection of one or more drinks make the column a "set" type.. if you want only one option from a list to be entered make the column an "enum" type. The "set" type will store the info as a comma seperated list.. all chosen from a default list of options. The enum type will only store one drink that has been chosen from a default list of options.
woot i got it! its working! ty guys.
Bookmarks