The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Checkbox processing for subscriptions
Discuss Checkbox processing for subscriptions in the PHP Development forum on Dev Shed. Checkbox processing for subscriptions PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 14th, 2013, 02:10 PM
|
 |
from the lab...
|
|
Join Date: Nov 2004
Location: the land of wind and ghosts
|
|
|
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!
|

February 14th, 2013, 02:19 PM
|
|
|
|
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.
|

February 14th, 2013, 02:24 PM
|
 |
from the lab...
|
|
Join Date: Nov 2004
Location: the land of wind and ghosts
|
|
Quote: | 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!
|

February 14th, 2013, 02:35 PM
|
|
|
|
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.
|

February 14th, 2013, 03:34 PM
|
 |
from the lab...
|
|
Join Date: Nov 2004
Location: the land of wind and ghosts
|
|
|
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.
|

February 15th, 2013, 06:51 AM
|
|
|
|
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.
|

February 15th, 2013, 10:42 AM
|
 |
from the lab...
|
|
Join Date: Nov 2004
Location: the land of wind and ghosts
|
|
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?
|

February 15th, 2013, 11:08 AM
|
 |
from the lab...
|
|
Join Date: Nov 2004
Location: the land of wind and ghosts
|
|
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!
|

February 15th, 2013, 11:11 AM
|
|
|
|
Now all you have to do is rewrite the database statements to eliminate the deprecated MySQL extensions and use PDO.
|

February 15th, 2013, 02:46 PM
|
 |
from the lab...
|
|
Join Date: Nov 2004
Location: the land of wind and ghosts
|
|
Quote: | 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!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|