...

View Full Version : Resolved Update depending on user levels >= <=



Dan13071992
04-25-2012, 02:06 PM
hello everyone, im having a spot of bother with this script im trying to make. basically if the select box is admins, send to user access level of 4 and 5, if send to mods accesslevel of 3 and 4, hdo send to user accesslevel of 1 ect

but im having trouble tring to write this so it adds the AND accesslevel (part here)

below is my code, i think you should be able to discover what i mean more by looking at it.

admins accesslevels are 4 and 5
mods are 2 and 3
hdo is 1



if ($level == 'admins') {
$leveltosendto = "AND accesslevel >='4'";
}elseif ($level == 'mods') {
$leveltosendto = "AND accesslevel >='2' && <='3'";
}



$total=0;

$select_all=mysql_query("SELECT * FROM users WHERE status='Alive' $leveltosendto");

while($send_to=mysql_fetch_object($select_all)){
mysql_query("INSERT INTO `inbox` (`id`, `to`, `from`, `message`, `date`, `read`, `folder`) VALUES ('', '".dbSafe($send_to->username)."', '".dbSafe($fetch->username)."', '".dbSafe($mass_msg)."', '".dbSafe($date)."', '0', 'inbox');") or die (mysql_error());
$total++;
}


Thanks.

Dan

Fou-Lu
04-25-2012, 04:50 PM
There's a syntax error in your sql. This isn't valid: accesslevel >='2' && <='3'. That has to be either accesslevel >='2' AND accesslevel <='3', (accesslevel ='2' || accesslevel ='3') or accesslevel IN (2, 3). Drop the quotes of these are actual numbers, otherwise MySQL has to implicitly convert the datatype to compare to which will slow down performance.

You could also use bitwise checks if the user levels were specified in bits (which they are not). Then you'd have 1, 2, 4, 8, 16 representing say guest, user, mod, supermod, admin. Then you can use bits to check the values, so if you sent to a group that check for admin and supermod, you could use accesslevel & 24 != 0. Then its a simple matter of bitor the level you want to find. Users and supermods would be 10 for example.

mlseim
04-25-2012, 04:59 PM
Try this (untested) ...




// My thinking on this ...
// Everyone is a zero level, so that's the default.
// If you're an hdo, you can access all levels below 2. (0,1)
// If you're a mod, you can access all levels below 4. (0,1,2,3)
// If you're an admin, you can access all levels below 6. (0,1,2,3,4,5)

// default
$leveltosendto = "AND accesslevel = '0' ";

if ($level == 'hdo') {
$leveltosendto = "AND accesslevel < '2' ";
}
if ($level == 'mods') {
$leveltosendto = "AND accesslevel < '4' ";
}
if ($level == 'admins') {
$leveltosendto = "AND accesslevel < '6' ";
}

$total=0;

$select_all=mysql_query("SELECT * FROM users WHERE status='Alive' $leveltosendto");

while($send_to=mysql_fetch_object($select_all)){
mysql_query("INSERT INTO `inbox` (`id`, `to`, `from`, `message`, `date`, `read`, `folder`) VALUES ('', '".dbSafe($send_to->username)."', '".dbSafe($fetch->username)."', '".dbSafe($mass_msg)."', '".dbSafe($date)."', '0', 'inbox');") or die (mysql_error());
$total++;
}





.

Dan13071992
04-25-2012, 05:05 PM
thanks Fou-lou, i used accesslevel IN (2, 3) and it worked a treat.

@mlseim thanks for your post, however i didnt uunderstand a part of it, being htat its a mass messaging system to send messages to either all user/mods/admins/hdo's depending one what the select box was set at, so by using <6 wouldnt it send to everyone under that accesslevel.

I may be wrong (more than likely) but that is what my understanding is?

mlseim
04-25-2012, 05:20 PM
Sorry, I didn't know what your script is supposed to do.

Dan13071992
04-25-2012, 06:04 PM
im sorry i should have tried harder to explain the problem, but thank you very much both of you for your help :)

Fou-Lu
04-25-2012, 06:53 PM
IN would also be my recommendation. Its the shortest to write, and if you want you can easily use an array in PHP and implode it to end up with the criteria required for the IN clause.


$aAccessIn = array();
switch ($levels)
{
case 'admins':
$aAccessIn = array(4, 5); // I don't ever trust >=, <= etc for my checks. This said I use a much more complicated approach to groups
break;
case 'mods':
$aAccessIn = array(2, 3);
break;
//...
}

if (!empty($aAccessIn))
{
$levelstosend = ' AND accesslevel IN (' . implode(', ', $aAccessIn) . ')';
}

I assumed integers. If they are strings you would use ' AND accesslevel IN (\'' . implode("', '", $aAccessIn) . '\')';.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum