SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
Hybrid View
-
Apr 30, 2007, 13:48 #1
- Join Date
- Apr 2007
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
"parsing" a string from one collumn in mysql db
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.
-
Apr 30, 2007, 13:56 #2
- Join Date
- Feb 2007
- Location
- Swindon, UK
- Posts
- 50
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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:
PHP 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]";
}
-
Apr 30, 2007, 16:23 #3
- Join Date
- Apr 2007
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
im talking about having the options.... pepsi,coke,sprite all in one line of the collumn not in individual cells.
-
Apr 30, 2007, 16:28 #4
- Join Date
- Feb 2007
- Location
- Swindon, UK
- Posts
- 50
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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...
PHP 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++ ;
}
-
Apr 30, 2007, 16:43 #5
- Join Date
- Apr 2007
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hrmm trying to get it to work keeps giving me the white screen hehe.
-
Apr 30, 2007, 16:43 #6
- Join Date
- Apr 2007
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
the collumn where the strings of options are located is food_description
-
Apr 30, 2007, 16:47 #7
- Join Date
- Feb 2007
- Location
- Swindon, UK
- Posts
- 50
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
and whats the table called?
-
Apr 30, 2007, 16:50 #8
- Join Date
- Apr 2007
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
food_descriptions
-
Apr 30, 2007, 16:52 #9
- Join Date
- Feb 2007
- Location
- Swindon, UK
- Posts
- 50
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ok, I'm off now anyway, but this is what you need to do:
PHP 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
-
Apr 30, 2007, 17:28 #10
- Join Date
- Apr 2007
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
it does not appear to be pulling any data hrmm.
-
Apr 30, 2007, 18:57 #11
- Join Date
- Apr 2007
- Location
- Goldfields, VIC, Australia
- Posts
- 518
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Apr 30, 2007, 19:21 #12
- Join Date
- Apr 2007
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
woot i got it! its working! ty guys.
Bookmarks