Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2004
    Posts
    38
    Thanks
    0
    Thanked 2 Times in 2 Posts

    splitting up array

    I have a form that has checkboxes in it that i get through a query on a table the checkboxes are named my_events[] and I can insert them into my table fine with implode, and can even get them out of the table with this query

    PHP Code:
    $my_results MYSQL_QUERY("SELECT my_events FROM users WHERE username = '$username'");

        echo 
    "<ul>";
    while(
    $my_eventsp MYSQL_FETCH_ARRAY($my_results)){

        echo 
    "<li>$my_eventsp[0]</li>";

        echo 
    "</ul>"
    What I would like to do is seperate them to be listed seperatly, in the table they are seperated by a comma. I think I should be using explode() but haven't figured out how that would work with what I have now. Any help is appreciated.

  • #2
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would advise you first to rethink your database design. Generally speaking, you shouldn't be storing comma-separated lists in your db; you should be storing each element of the list in its own field. Usually, this means a separate table.

    Barring that, explode()'ll work fine. Explode $my_eventsp[0] separately and then loop through that array to add your list tags.

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Why not use serialize()/unserialize()??
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #4
    Regular Coder
    Join Date
    May 2004
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    while($row MYSQL_FETCH_ARRAY($my_results)){

    $my_eventsp explode(","$row[columnname]);

    echo 
    "<li>$my_eventsp[0]</li>";

        echo 
    "</ul>"
    with mysql_fetch_array you can use the column name inside the brackets. $row[name] instead of $row[0]
    my_eventsp[0] will give you the value of whats before the comma.

    that might work,
    good luck
    c.c.
    Last edited by ClubCosmic; 07-06-2005 at 01:45 AM.

  • #5
    New Coder
    Join Date
    Jan 2004
    Posts
    38
    Thanks
    0
    Thanked 2 Times in 2 Posts
    ok so I created a new table with these fields in it, username, name( which will be "my_events") and value.

    now how would i go about inserting that array into the new table haven't figured it out and arrays are not really my thing.

  • #6
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd like to ask a couple of questions about your tables first, if I could. Does your users table have a unique id for each user aside from the username? In your new table, what kind of information is going to be in your value column?

  • #7
    New Coder
    Join Date
    Jan 2004
    Posts
    38
    Thanks
    0
    Thanked 2 Times in 2 Posts
    yeah there is a uniqid in the main user table, but do not really need to have that in the table I just made. just a username will do because that information will not get displayed unless the user is logged in.

    the value column is going to be the name of a category that they selected by checking a checkbox or multiple boxes. So what I was looking to do was just add the username to the usename field, then seeing I know what this is going to be for I can put my_events in the name field, then whatever checkboxes they selected then have an entry in the table for each one that was selected along with the username, my_events with it.

    Or is there an easier way of doing it. Basically it will be building a profile for them to display information on their page that they can track.

  • #8
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You'd be better off using the unique id -- it'll make a more efficient index. Your other table doesn't need a my_events fields, since my_events should be the only type of info in it. So you've got two fields with userid and event (of course, you can name them anything you'd like; I'd avoid 'value' as too vague, though). Then you can do something like this for your loop:

    PHP Code:
    $query "SELECT event FROM my_events where userid = $id";
    $result mysql_query($query) or die(mysql_error());


    echo 
    "<ul>";

    while( 
    $row mysql_fetch_row($result) )
    {
        echo 
    "<li>$row[0]</li>";

        echo 
    "</ul>"


  •  

    Posting Permissions

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