SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Multiple selections with checkboxes

    I wonder if someone can help. I have a form which allows users to choose which categories they would like to recieve a newsletter about. I have a category table and a users table. The checkboxes are dynamic and display correctly with a query from the category table.

    I have code which shows which categories the user has selected and it does if it is a single choice. However, if they choose more than one, it only displays the final choice or the first. I think it's a looping problem. The form updates the users table and using implode and delimited by a comma, places the values successfully into a single row.

    The problem I'm having is getting the selected checkboxes to show so if three boxes are selected, all three checkboxes are checked. I need to extract the data and split it so that I can check each category id with the values in the array. If the array contains a cat ID then that checkbox is checked by default.

    Here's my code;

    <form id="form1" name="category" method="post" action="">
    <?php
    echo "User name: $_SESSION[username]<br />";

    //existing ctegories selected
    $result = mysql_query("SELECT cat_selected, news_address FROM newsletter WHERE `userName` = '$_SESSION[username]'") or die('Error Displaying DB' . mysql_error());
    while($sql=mysql_fetch_array($result)){
    $existing=$sql['cat_selected'];
    }

    if(isset($_POST["cid"])) {
    $z = $_POST['cid'];
    $categories = implode(",",$z);
    $query = "UPDATE `newsletter` SET `cat_selected` = '$categories' WHERE `userName` = '$_SESSION[username]'";
    $res=mysql_query($query) or die ("Fail to add test_value");


    print $categories;

    }

    else{
    echo 'Please choose a category <br>';
    }



    $info_id=$_GET['info-id'];
    $result = mysql_query("SELECT * FROM category") or die('Error Displaying DB' . mysql_error());
    while($sql=mysql_fetch_array($result)){

    $id_category=$sql['cid'];
    $mk=$sql['category_name'];

    echo '<input type="checkbox" name="cid[]" id="cid" value="' . $id_category;
    if($id_category == $existing){
    echo '" CHECKED />';
    }
    elseif ($id_category == $categories){
    echo '" CHECKED />';
    }else{
    echo '" />';
    }
    echo '<label for="' . $id_category . '">' . $mk . '</label><br />';

    }
    echo '<input type="submit" name="submit" value="Submit Changes" /><br />';
    ?>

    </form>
    Last edited by freakystreak; Mar 26, 2009 at 03:09.

  2. #2
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At first glance you should remove the first opening php tag and the last closing php tag.

  3. #3
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, but they shouldn't have been on there. Any other ideas?

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    PHP Code:
    while ($sql=mysql_fetch_array($result)) {
      
    $existing $sql['cat_selected'];

    This code loops through all selected rows, and in the end $existing will contain only the last rows 'cat_selected' value. Since you need these values later on in the script, you'll have to store them in an array:
    PHP Code:
    while ($sql=mysql_fetch_array($result)) {
      
    $existing[] = $sql['cat_selected'];

    And this:
    PHP Code:
    if ($id_category == $existing) {
      echo 
    '" CHECKED />';

    must change in something like:
    PHP Code:
    if (in_array($id_category$existing) {
      echo 
    '" CHECKED />';


  5. #5
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That doesn't show any checkboxes at all. Where as before I was getting the last selected one. My MySQL table row for cat_selected looks like this: 95,97
    If a single category is chosen then it works fine but once you select muliples then it doesn't show the boxes as checked.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by freakystreak View Post
    My MySQL table row for cat_selected looks like this: 95,97
    So you have a field (not a row) in your table that contains multiple, comma separated, values? That usually means a bad database design and can lead to problems when you have to query the data. As you're finding out right now
    Do a google search for database normalization if you want to know more about it.

    A quick solution might be this:
    PHP Code:
    while ($sql=mysql_fetch_array($result)) {
      
    $existing explode(','$sql['cat_selected']);

    which creates an array with the single category codes.
    Now this should work too:
    PHP Code:
    if (in_array($id_category$existing) {
      echo 
    '" CHECKED />';


  7. #7
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that seems to be working fine. Thanks for the help. It's so easy when you know how.


Tags for this Thread

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
  •