or what about, set everything to unpublished, and then only publish those in the post array? Bit clumsy but simple and easy???
Well, if you're not grouping into categories, then I'd say you're okay. For me, this was the easiest implementation I could muster. Basically, I couldn't just "deauthorize" someone from an organization, so I had to dynamically find a way to set and update and remove permissions.
If it's just a general list, where it's not split, then unsetting all, and enabling only those selected is a fine course of action. But if later they want to split into categories or groups, then you're going to have an issue because you can't remember what state the items that aren't affected were in before you unset everything.
I'll try to walk you through my code...
// 1.) Remove duplicates from $orgs array (if there are any)
$orgs = array_unique($orgs);
Pretty self explanatory. If there are duplicate values (like: 5,6,7,8,7,9,0,8) then we only want one instance of them. So the above set becomes: 5,6,7,8,9,0.
// 2.) Take out their primary orgID as defined in $org_id above...
if(($key = array_search($org_id, $orgs)) !== false)
{
unset($orgs[$key]);
}
This was just a step to remove the primary organization from the array. So if they're primarily affiliated with id #7, then the set from above becomes: 5,6,8,9,0.
// 3.) Get their curent orgs (if any) from the DB
$db_orgs = array();
$query = "SELECT `id`, `org_id` FROM `organization_admin` WHERE `user_id`=" . (int)$userID;
$result = mysql_query($query) or db_error($query, __FILE__, __LINE__);
while($row = mysql_fetch_array($result))
{
$db_orgs[$row['id']] = $row['org_id'];
}
$free = mysql_free_result($result);
This just gets the database organizations and the corresponding row ID. This way, I can easily tell which rows to delete if need be. I create an array of key value pairs with the row ID (which should always be unique) as the key, and the organization ID as the value.
// 4.) Which organizations are duplicates between DB and $orgs:
$dups = array_intersect($db_orgs, $orgs);
So here what we're doing is taking two arrays and finding what values are in both arrays. So if my $orgs array (remember it's 5,6,8,9,0) has any values in the $db_orgs array (let's populate that with: 1=>0, 10=>3, 5=>5, 8=>123), then the $dups variable will become an array with the duplicate values copied from $db_orgs. So in our case, we'd have an array of: 1=>0, 5=>5.
So let's just recap real quick:
$orgs = array{5, 6, 7, 8, 9, 0}
$db_orgs = array{1=>0, 10=>3, 5=>5, 8=>123}
$dups = array{1=>0, 5=>5}
With me so far? Good.
if(is_array($dups) && count($dups) > 0)
{
foreach($dups as $id=>$val)
{
$_orgs_id = array_search($val, $orgs);
unset($db_orgs[$id]);
unset($orgs[$_orgs_id]);
}
}
unset($dups);
Okay, so if the $dups variable is an array and it's not an empty array (as in there are duplicates) we need to "weed them out" of our arrays so they're not disabled by accident. So we loop through the $dups array, and since it's populated with the info from the $db_orgs array, we can just quickly [man]unset/man the array element with the id of the current iteration, and then we search the $orgs array for the ID of the element with the value of the current iteration. Then we just remove the $dups array from memory since we don't need it anymore. So our three arrays, be come two which look like:
$orgs = array{6,8,9}
$db_orgs = array{10=>3, 8=>123}
// 5.) Remove those left in $db_orgs from DB
if(!empty($db_orgs))
{
$keys = array_keys($db_orgs);
$query = "DELETE FROM `organization_admin` WHERE `id` IN (" . implode(',', $keys) . ")";
$result = mysql_query($query) or db_error($query, __FILE__, __LINE__);
if(mysql_affected_rows() != count($db_orgs)) { return false; }
$free = mysql_free_result($result);
}
This is pretty self explanatory. We take the $db_orgs array (if it's not empty) and delete the rows associated with them in the database. For you it'd be a simple UPDATE statement to say UPDATE table SET display=0 WHERE id IN(....) and you'd use the [man]array_keys/man as a comma-separated-value (CSV) in the WHERE clause to limit what gets updated. So the real query would look like:
UPDATE table SET display=0 WHERE id IN(10,8)
// 6.) Add those left in $orgs
if(!empty($orgs))
{
$query = "INSERT INTO `organization_admin` (`user_id`, `org_id`) VALUES ";
foreach($orgs as $org)
{
$query .= "\n(" . (int)$userID . ", " . (int)$org . "),";
}
$query = substr($query, 0 ,-1);
$result = mysql_query($query) or db_error($query, __FILE__, __LINE__);
if(mysql_affected_rows() != count($orgs)) { return false;}
}
Now comes the easy part. If the $orgs array isn't empty (as in we're actually adding items) then we just create the INSERT statement to do so. For you, it'd be another UPDATE in which case you'd UPDATE all those rows with an ID that's in the CSV of the values of the $orgs array. So your update statement would look like:
UPDATE table SET display=1 WHERE id IN(6,8,9)
// 7.) If we're here, we've successfully inserted them!!
return true;
And again, self explanatory.
I hope this explanation helped you understand what's going on. It's up to you how to implement it, but my method will allow you greater control, and can possibly (in the long run) actually be faster.