SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot Darren Lewis's Avatar
    Join Date
    Jul 2002
    Location
    UK
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with array and if statement

    Hi, I'm a novice at php and I'm having trouble with this script I'm trying to write.
    It should check the usergroupid of the visitor, and then do either action1 or action2 depending which group they're in. I can get the usergroupid OK and I've checked it by echoing to screen.

    This is my code so far
    PHP Code:
    $allowbbgroups "5,6";
     
    $bbusergroupid code to get usergroupid of visitor
     
    $bbgids 
    explode(",",$allowbbgroups);
     
    $i 0;
     
    $a count($bbgids);
     if (
    $a 1) {
      
    $bbusergrps "$bbusergroupid=='$bbgids[0]'";
      ++
    $i;
      while (
    $i $a) {
      
    $bbusergrps .= " OR $bbusergroupid=='$bbgids[$i]'";
      ++
    $i;
      }
     } else {
      
    $bbusergrps "$bbusergroupid==$bbgids[$i]";
     }
     
    if (
    $bbusergrps) {
      do 
    action1
    } else {
      do 
    action2

    Any ideas on how to explode the $allowbbgroups and use them in the action1 if statement?
    The method above doesn't work, it does action1 regarless of the usergoupid.

    Thanks.
    Censura - Add reviews and ratings to your site

  2. #2
    As the name suggests... trickie's Avatar
    Join Date
    Jul 2002
    Location
    Melbourne, Australia
    Posts
    678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    if ($bbusergrps) {
      do 
    action1
    } else {
      do 
    action2
    }  
    ?>
    in your code $bbusergrps will always evaluate to true???
    It's a string with 1 or more charcters that isn't '0' or false

  3. #3
    SitePoint Zealot Darren Lewis's Avatar
    Join Date
    Jul 2002
    Location
    UK
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So how could I do it so that if a user is not in either of the $allowbbgroups usegroups, the code returns a false?

    I can do it if I only set $allowbbgroups to a single value

    PHP Code:
    if ($bbusergroupid==$allowbbgroups ) {
      do 
    action1
    } else {
      do 
    action2


    but I want to be able to allow multiple values. How can I code that part? I had thought expoding $allowbbgroups to an array would be the answer, but I'm not sure what to then with that array.

    I think I need to be able to place an OR statement into my if statement, but there may be one, two or more $allowbbgroups values.

    Thanks.

    Darren.
    Censura - Add reviews and ratings to your site

  4. #4
    SitePoint Addict BenANFA's Avatar
    Join Date
    Apr 2003
    Location
    Bath, UK
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try

    PHP Code:

    $match_found 
    FALSE// assume no matches

    foreach( $bbgids as $testbbgid )
    {
    // found a match, set flag and stop loop
        
    if ( $bbusergroupid == $testbbgid )
        {
            
    $match_found TRUE;
            break;
        }
    }

    if (
    $match_found

        do 
    action1 

    else 

        do 
    action2 


  5. #5
    SitePoint Zealot Darren Lewis's Avatar
    Join Date
    Jul 2002
    Location
    UK
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Do I add that into my code, or instead of the explode and array bit?

    How do I relate your code to the $allowbbgroups

    Thanks.

    Darren.
    Censura - Add reviews and ratings to your site

  6. #6
    SitePoint Addict BenANFA's Avatar
    Join Date
    Apr 2003
    Location
    Bath, UK
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Keep these 3 lines from your original code, put the code I gave after them.

    PHP Code:
    $allowbbgroups "5,6";

    $bbusergroupid code to get usergroupid of visitor

    $bbgids 
    explode(",",$allowbbgroups); 

  7. #7
    SitePoint Zealot Darren Lewis's Avatar
    Join Date
    Jul 2002
    Location
    UK
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Ben, I'll give that a try later tonight or tomorrow.
    Censura - Add reviews and ratings to your site

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Even shorter and faster. Replace all your code with ...
    PHP Code:
    $allowbbgroups = array ('5','6');
    $bbusergroupid code to get usergroupid of visitor
    if ( in_array ($bbusergroupid$allowbbgroups) )

        do 
    action1 

    else 

        do 
    action2 

    Ian Anderson
    www.siteguru.co.uk

  9. #9
    SitePoint Zealot Darren Lewis's Avatar
    Join Date
    Jul 2002
    Location
    UK
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Both methods work perfectly.

    Thank you to both of you

    Darren.
    Censura - Add reviews and ratings to your site


Bookmarks

Posting Permissions

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