vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 3 Articles (https://vborg.vbsupport.ru/forumdisplay.php?f=187)
-   -   Checking if a number is either X, Y, or Z (https://vborg.vbsupport.ru/showthread.php?t=35743)

Admin 03-04-2002 10:00 PM

Checking if a number is either X, Y, or Z
 
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... :)

Neo 03-06-2002 02:55 AM

Very nice firefly. This is very helpful... I really didnt like arrays but I am starting to see the usfulness in them. :D

Dark_Wizard 03-07-2002 05:20 PM

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.

Admin 03-07-2002 05:32 PM

Post in the PHP forum about it and I'll see what I can do. :)

buro9 03-21-2002 05:23 AM

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.


All times are GMT. The time now is 02:16 PM.

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.01316 seconds
  • Memory Usage 1,726KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete