1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP: Switch/Case problem

Discussion in 'PHP' started by Panjabi, Dec 24, 2007.

  1. #1
    Will probably be a simple solution but I cannot figure out why my php switch function is not working on my site.

    I have just changed hosting and it was working perfectly before and Ive changed nothing and just uploaded all the files the way they should be but the function does not work.

    Any ideas?
     
    Panjabi, Dec 24, 2007 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    It's hard to say without you code. I guess I can be wrong permissions on files or some php extentions not installed on your new hosting.
     
    AsHinE, Dec 25, 2007 IP
  3. faceless

    faceless Peon

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I don't think it has something to do with the switch function. But maybe with register_globals?
     
    faceless, Dec 25, 2007 IP
  4. Panjabi

    Panjabi Peon

    Messages:
    362
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The code is

    <?php
    switch($id) { 
    default: 
    include('default.php');
    break; case "2": 
    include('2.php');
    break; case "3": 
    include('3.php');
    break; case "4": 
    include('4.php');
    
    }
    ?>
    Code (markup):
     
    Panjabi, Dec 25, 2007 IP
  5. MrX

    MrX Well-Known Member

    Messages:
    1,563
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    140
    #5
    i agree with faceless. try echo $id to make sure it contains a value you're expecting (and is not empty).
     
    MrX, Dec 25, 2007 IP
  6. coches

    coches Peon

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    yes , debug a little ,
    also remember that around integers no quotes need to be used

    so

    switch ( $id )
    {
    case 1:
    // do your shit
    break;

    // etc
    }
     
    coches, Dec 26, 2007 IP
  7. coches

    coches Peon

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    buah and if ur code is this
    <?php
    switch($id) {
    default:
    include('default.php');
    break; case "2":
    include('2.php');
    break; case "3":
    include('3.php');
    break; case "4":
    include('4.php');

    }
    ?>

    maybe better to do something like
    <?php
    $pages = array(1,2,3,4,5);
    include( (isset($var) && in_array($var,$pages)) ? $var.'.php' : '1.php' );
     
    coches, Dec 26, 2007 IP
  8. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Looks like you're expecting register_globals to be set to On. Don't rely on register_globals: it's insecure and deprecated.

    You'll need:
    $id = $_REQUEST['id'];

    as the first line after the opening PHP tag and that should work.
     
    TwistMyArm, Dec 26, 2007 IP
    Panjabi likes this.
  9. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #9
    default is never at top

    Regards

    Alex
     
    kmap, Dec 26, 2007 IP
  10. Panjabi

    Panjabi Peon

    Messages:
    362
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks to all who helped, TwistMyArm's answer worked.

    Rep added.
     
    Panjabi, Dec 26, 2007 IP