SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Member
    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.

  2. #2
    SitePoint Enthusiast
    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]";

    Where $link is your mysql_connect function

  3. #3
    SitePoint Member
    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.

  4. #4
    SitePoint Enthusiast
    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++ ;

    Hope this helps

  5. #5
    SitePoint Member
    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.

  6. #6
    SitePoint Member
    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

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Swindon, UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and whats the table called?

  8. #8
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    food_descriptions

  9. #9
    SitePoint Enthusiast
    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++ ;

    All that needs changing is substituting $link for the resource that holds your mysql_connect - persuming you have already connected to the DB

    Post a reply if you have any issues, and I'll check back in the morning

  10. #10
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it does not appear to be pulling any data hrmm.

  11. #11
    SitePoint Evangelist catweasel's Avatar
    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.

  12. #12
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    woot i got it! its working! ty guys.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •