#1
  1. from the lab...
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2004
    Location
    the land of wind and ghosts
    Posts
    1,844
    Rep Power
    560

    Checkbox processing for subscriptions


    Hey folks,

    I'm developing a system that basically allows users to 'subscribe' for various 'divisions' (of which there are around 15).

    These are managed via checkboxes through their account, that they can update whenever they feel like.

    My question is, is there an efficient way to process their division subscription changes?

    There's the 'divisions' table, and a table joining the user to each division via user_id and division_id.

    I just don't think that each time the user changes a division there should be a query to cross reference whether they are or are not subscribed, and updating the table based on their checkbox selections (or de-selections) relative to whether they are or are not subscribed already, but I can't really think of how else to handle it.

    Any help'd be greatly appreciated!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    What difference does it make to the update whether or not they are already subscribed? Just update the all current selections without regard to previous selections.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. from the lab...
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2004
    Location
    the land of wind and ghosts
    Posts
    1,844
    Rep Power
    560
    Originally Posted by gw1500se
    What difference does it make to the update whether or not they are already subscribed? Just update the all current selections without regard to previous selections.
    Hmm, I suppose just for adding more subscriptions it wouldn't matter, but if they want to unsubscribe by de-checking a box, that's where it gets a little confusing/resource intensive, at least in my mind.

    Thanks!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    From a database update perspective it doesn't matter. If you have to take other actions to unsubscribe someone then you would indeed need to check if they are already subscribed but I see no way around that. However, you only need to do 1 query to get the status of all the subscriptions so I don't see any problem with extra overhead from a database standpoint.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. from the lab...
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2004
    Location
    the land of wind and ghosts
    Posts
    1,844
    Rep Power
    560
    Yeah, I guess if I just put more work into processing/preparing the actual checkbox part of it, the mysql becomes simpler.

    Was also thinking of maybe javascript/ajax to just do the work as they check/uncheck each box, rather than submitting the whole form.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    I think that would be less efficient as it means more DB transactions. However, unless you are expecting hundreds of transactions per minute I doubt any of your concerns will be worth a lot of effort.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. from the lab...
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2004
    Location
    the land of wind and ghosts
    Posts
    1,844
    Rep Power
    560
    Thanks for the input thus far. I think I've got a way to do this without javascript, but I'm just a bit unsure of the syntax.

    Here's what I'm using for each form field (a common method i've found for processing checked, as well as unchecked form fields):
    Code:
    <label>Concrete:</label>
    <input type="hidden" name="checkbox-5[]" value="0">
    <input type="checkbox" name="checkbox-5[]" class="checkbox" value="1" >
    The number after the hyphen is the unique ID associated with the division, which is what I actually need to use to either insert OR delete from the db.

    Here's what I'm trying to use to either add or delete the division, but it's not working:
    PHP Code:
    if(isset($_POST['updateDivisions'])) {

        
    $checkbox $_POST['checkbox'];
        
    $countCheck count($_POST['checkbox']);

        for(
    $i=0;$i<$countCheck;$i++) {

            
    $div_id explode("-"$checkbox) ;
            
    $div_id $checkbox[1][$i];

            if(
    $checkbox[$i] == 1) {
            
            
    $div_insert "
            INSERT INTO
                user_divisions
                (user, division)
            VALUES
                (
                "
    .mysql_real_escape_string($_SESSION['user']['user_id'])."
                ,
                "
    .mysql_real_escape_string($div_id)."
                )
            "
    ;
            
    $div_result mysql_query($div_insert);
            
    /*    
            } else {
                mysql_query("DELETE FROM user_divisions WHERE user = ".mysql_real_escape_string($_SESSION['user']['user_id'])." AND division = ".mysql_real_escape_string($_POST['checkbox'][$i])."") or die() ;
            }
            */
            
    }
        }
    }

    // SUCCESS OR FAILURE MESSAGE WILL APPEAR ONCE THIS IS SORTED 
    Any thoughts?
  14. #8
  15. from the lab...
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2004
    Location
    the land of wind and ghosts
    Posts
    1,844
    Rep Power
    560
    Okay, I got this sorted. I was overthinking how to do this.
    I ditched the hidden field, and processed it with this:
    PHP Code:
    if(isset($_POST['updateDivisions'])) {

        
    $total_divisions countDivisions() ;

        for(
    $i=1;$i<=$total_divisions;$i++) {

            if (isset(
    $_POST['checkbox-'.$i.''])) {
                
    // ADD THE ENTRY
                
    mysql_query("INSERT INTO user_divisions (user, division) VALUES (".mysql_real_escape_string($_SESSION['user']['user_id']).", ".$i.")") or die();
            } else {
                
    // DELETE THE ENTRY    
                
    mysql_query("DELETE FROM user_divisions WHERE user = ".mysql_real_escape_string($_SESSION['user']['user_id'])." AND division = ".$i."") or die();        
            }

        }  
        

    It'll be cleaned up and return messages based on additions/deletions, but it works.

    Thanks for the help thus far!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    Now all you have to do is rewrite the database statements to eliminate the deprecated MySQL extensions and use PDO.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. from the lab...
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2004
    Location
    the land of wind and ghosts
    Posts
    1,844
    Rep Power
    560
    Originally Posted by gw1500se
    Now all you have to do is rewrite the database statements to eliminate the deprecated MySQL extensions and use PDO.
    Had a couple of stabs at PDO on smaller projects, but that's for another day.

    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo