vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=111)
-   -   PHP - Select Case / Switch help (looking for a good example) (https://vborg.vbsupport.ru/showthread.php?t=68689)

Tekton 08-25-2004 04:48 AM

PHP - Select Case / Switch help (looking for a good example)
 
I spent some time searching for a good php select case / switch example, but the one I found doesn't seem to work... Maybe I can get a better example since it's about 20 or so cases total, and I don't want to do that on if/elses :) I've taken out other things inside of them for sanity.

PHP Code:

switch ($magic) {

case 
0 to $chance_for_normal:
$special 0;
break;
case 
$endchance to 20//must be set lower if normal chance is reduced.
$special 1;
break;

case 
21 to 25:
$special 3;
break;

etc.....

etc..... 

Seems to just give me errors. Am I doing it wrong?

Brad 08-25-2004 04:58 AM

If you have trouble with php code look for an example within vBulletin or other scripts, here is an example of a proper switch statement, you can find it in memberlist.php in vBulletin 3.0.3

PHP Code:

    switch ($sortfield)
    {
        case 
'username':
            
$sqlsort 'user.username';
            break;
        case 
'joindate':
            
$sqlsort 'user.joindate';
            break;
        case 
'posts':
            
$sqlsort 'user.posts';
            break;
        case 
'lastvisit':
            
$sqlsort 'lastvisittime';
            break;
        case 
'reputation':
            
$sqlsort iif($show['reputationcol'], 'reputationscore''user.username');
            
$secondarysortsql ', user.username';
            break;
        default:
            
$sqlsort 'user.username';
            
$sortfield 'username';
    } 

Is that clear enough? :)

Tekton 08-25-2004 05:18 AM

Well, it doesn't use "to"s, or numbers, but I suppose I can try again. I did look at the memberlist one before :)

UPDATE :

For future refrence or similar questions, it's like this...

PHP Code:

case '0 to $chance'

or

PHP Code:

case '0 to 5'

:)

Brad 08-25-2004 06:07 AM

Generaly text has to be in quotes, but numbers don't.

Natch 08-25-2004 07:33 AM

The only way to do what you wanna do without having heaps of code is to do the following:
PHP Code:

switch ($magic) {
    case 
1:
    case 
2:
    case 
3:
    ...
    case 
18:
    case 
19:
    case 
20:
        
$special 1;
        break;
    case 
21:
    case 
22:
    case 
23:
    case 
24:
    case 
25:
        
$special 3;
        break;
    default:
        
$special 0;


The logic of it is that if if comes to case 15, and doesn't come to a break before the end of case 20 then it will apply the $special = 1 for all cases up to 20 - you will have to enter each case but not define the value for each one.

Tekton 08-26-2004 01:45 AM

Quote:

Originally Posted by Natch
The only way to do what you wanna do without having heaps of code is to do the following:
PHP Code:

switch ($magic) {
loooong thing


The logic of it is that if if comes to case 15, and doesn't come to a break before the end of case 20 then it will apply the $special = 1 for all cases up to 20 - you will have to enter each case but not define the value for each one.

I updated my post with a method that worked...

PHP Code:

case '0 to $chance'

or

PHP Code:

case '0 to 5'



All times are GMT. The time now is 07:50 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02235 seconds
  • Memory Usage 1,744KB
  • 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
  • (8)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (6)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