Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts

    Update depending on user levels >= <=

    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

    PHP Code:
    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
    Last edited by Dan13071992; 04-25-2012 at 06:05 PM.
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,518
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Try this (untested) ...

    PHP Code:

    // 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++;



    .

  • #4
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    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?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,518
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Sorry, I didn't know what your script is supposed to do.

  • #6
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    im sorry i should have tried harder to explain the problem, but thank you very much both of you for your help
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    $aAccessIn = array();
    switch (
    $levels)
    {
        case 
    'admins':
            
    $aAccessIn = array(45); // 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(23);
            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) . '\')';.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •