Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Checking if a number is either X, Y, or Z
Admin's Avatar
Admin
Join Date: Oct 2023
Posts: 1

Admin

Server
Show Printable Version Email this Page Subscription
Admin Admin is offline 03-04-2002, 10:00 PM

Frequently, you want parts of your hack to only be accessible by moderators and admins.

To do so, you use this if-block:
PHP Code:
if ($bbuserinfo['usergroupid'] == or $bbuserinfo['usergroupid'] == or $bbuserinfo['usergroupid'] == 7) {
  
/* Do stuff */
} else {
  
/* Access denied */

And if you want to give another usergroup access for that action, you need to add even another OR to that statement!

A much more elegant solution would be to use in_array():
PHP Code:
if (in_array($bbuserinfo['usergroupid'], array(567))) { 
You can even declare an array, let's say $allowedgroups, that can be used throughout the code:
PHP Code:
/* Earlier... */
$allowedgroups = array(567);

/* Somewhere in your code */
if (in_array($bbuserinfo['usergroupid'], $allowedgroups)) {
  
/* Do stuff */
} else {
  
/* Access denied */

This is also not only useable when checking usergroups! Let's say you have feature you only want visible on forums 4, 6, 18 and 65:
PHP Code:
/* Earlier... */
$specialforums = array(567);

/* Somewhere in your code */
if (in_array($forumid$specialforums)) {
  
/* Make use of the feature */

Not really a tip nor a trick, but I'm a bit bored so...
Reply With Quote
  #2  
Old 03-06-2002, 02:55 AM
Neo's Avatar
Neo Neo is offline
 
Join Date: Oct 2001
Location: Anywhere
Posts: 1,817
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Very nice firefly. This is very helpful... I really didnt like arrays but I am starting to see the usfulness in them.
Reply With Quote
  #3  
Old 03-07-2002, 05:20 PM
Dark_Wizard Dark_Wizard is offline
 
Join Date: Nov 2001
Location: North Carolina
Posts: 1,251
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Great info Firefly...very much appreciated, thank you! Can someone post something about joins? I am trying to something using joins and it just isn't working right.
Reply With Quote
  #4  
Old 03-07-2002, 05:32 PM
Admin's Avatar
Admin Admin is offline
Coder
 
Join Date: Oct 2023
Location: Server
Posts: 1
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Post in the PHP forum about it and I'll see what I can do.
Reply With Quote
  #5  
Old 03-21-2002, 05:23 AM
buro9 buro9 is offline
 
Join Date: Feb 2002
Location: London, UK
Posts: 585
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Firefly, that's good, but for performance you should really use a switch as this would prevent the test of an array, convert to an array and loop through array stuff that happens partly implicitly...

try something like...

PHP Code:
switch ($bbuserinfo['usergroupid']) {
  case 
5:
  case 
6:
  case 
7:
    
// Do stuff for usergroups 5 or 6 or 7
    
break;
  case 
2:
    
// Do stuff for usergroup 2
    
break;
  default:
    
// Do stuff for everyone else

Just an improvement. Doesn't change functionality... but under strain this should shine.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:56 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03720 seconds
  • Memory Usage 2,244KB
  • Queries Executed 18 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (5)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (5)post_thanks_box
  • (5)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (5)post_thanks_postbit_info
  • (4)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete