PHP $_GET Problem

Discussion in 'PHP' started by nimierkki, Jun 25, 2006.

  1. #1
    I hve tred to do incudescrit with thsi code
          
    55  <?PHP
    56   
    57 if (isset(intval($_GET['ID']))) { if (
    58 $_GET['ID'] == 1) { include 'index.php'; } elseif (
    59 $_GET['ID'] == 2) {include 'design.php'; } elseif (
    60 $_GET['ID'] == 3) {include 'graphics.php'; } elseif (
    61 $_GET['ID'] == 4) {include 'portfolio.php'; } elseif (
    62 $_GET['ID'] == 5) {include 'about.php'; } elseif (
    63 $_GET['ID'] == 6) {include 'contact.php'; } elseif (
    64 $_GET['ID'] == 0) {include 'index.php'; } 
    65       } else { include
    66      'home.php'; }
    67      ?> 
    
    Code (markup):
    And i get this message

    Parse error: parse error, unexpected T_STRING, expecting T_VARIABLE or '$' in /mbnet/mydomani/subfolder/subfolder/index.php on line 57

    what i shuld do?
    sorry my bad english!
     
    nimierkki, Jun 25, 2006 IP
  2. Bob_

    Bob_ Peon

    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Not certain but I think you should have a $ in front of all those ID's eg $ID
     
    Bob_, Jun 25, 2006 IP
  3. nimierkki

    nimierkki Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    sill i get same error
     
    nimierkki, Jun 25, 2006 IP
  4. Jangro

    Jangro Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The problem is that you're passing an integer to isset, not actually checking if the $_GET['ID'] is set.

    I'd take that intval out.

    if (isset($_GET['ID'])) { if (

    Also, for the comparisons that you're doing, you shouldn't need to convert $_GET['ID'] to an integer. If you do need to convert it, do it separately, and not in the conditional like that.
     
    Jangro, Jun 25, 2006 IP
  5. kjewat

    kjewat Active Member

    Messages:
    149
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #5
    Edit: Damn, you posted righ before me Jangro :)

    You can't check if a return value of a function is set, so
    if (isset(intval($_GET['ID'])))
    PHP:
    won't work.

    Use something like:
    $get=intval($_GET['ID']);
    if (isset($get)) 
    PHP:
    instead.

    The complete code would be:
    
    55  <?PHP
    56 $get= intval($_GET['ID']);
    57 if (isset($get)) { if (
    58 $_GET['ID'] == 1) { include 'index.php'; } elseif (
    59 $_GET['ID'] == 2) {include 'design.php'; } elseif (
    60 $_GET['ID'] == 3) {include 'graphics.php'; } elseif (
    61 $_GET['ID'] == 4) {include 'portfolio.php'; } elseif (
    62 $_GET['ID'] == 5) {include 'about.php'; } elseif (
    63 $_GET['ID'] == 6) {include 'contact.php'; } elseif (
    64 $_GET['ID'] == 0) {include 'index.php'; } 
    65       } else { include
    66      'home.php'; }
    67      ?> 
    
    Code (markup):
     
    kjewat, Jun 25, 2006 IP
  6. nimierkki

    nimierkki Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    what i have to write in link?? i cant get that right

    <a href="index.php?ID=1">
     
    nimierkki, Jun 25, 2006 IP
  7. kjewat

    kjewat Active Member

    Messages:
    149
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #7
    You should write something like:

    <a href="index.php?ID=1">LinkThextHere</a>
     
    kjewat, Jun 25, 2006 IP
  8. nimierkki

    nimierkki Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i can oppen "new" page design.php i wrote in the link <a href="index.php?ID=2">Thext</a> and it wont work
     
    nimierkki, Jun 25, 2006 IP
  9. Borghunter

    Borghunter Well-Known Member

    Messages:
    212
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #9
    I would simply do the code this way:
    if ($_GET['ID'] == 1) { include 'index.php'; } 
    elseif ($_GET['ID'] == 2) {include 'design.php'; } 
    elseif ($_GET['ID'] == 3) {include 'graphics.php'; } 
    elseif ($_GET['ID'] == 4) {include 'portfolio.php'; } 
    elseif ($_GET['ID'] == 5) {include 'about.php'; } 
    elseif ($_GET['ID'] == 6) {include 'contact.php'; } 
    elseif ($_GET['ID'] == 0) {include 'index.php'; } 
    else { include 'home.php'; }
    Code (markup):
    This Always works, you really have no need for isset

    By The way if this is in index.php you can't include itself
     
    Borghunter, Jun 25, 2006 IP
  10. itsme

    itsme Well-Known Member

    Messages:
    1,232
    Likes Received:
    96
    Best Answers:
    0
    Trophy Points:
    180
    #10
    for this sort of stuff it's MUCH better and easier to use the PHP switch statement:


    switch ($_GET['ID']) {
    case 0:
    include ("example.php");
    break;
    case 1:
    include ("example.php");
    break;
    case "this is text":
    include ("example.php");
    break;
    default:
    echo "ERROR, ID not found";
    break;
    }


    You'll notice that you can setup the switch statement to match against integers or strings, it's up to you.

    ...not to mention the default action that can be added. You don't have to have it but it is a fantastic feature.

    direct link to the PHP manual's switch statement

    You'll also notice that I wrote the includes with round brackets and double quotes, I suggest you do this also.
     
    itsme, Jun 26, 2006 IP
  11. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #11
    You should definitely go down the switch route. Much more efficient use of your servers resources.

    You can write;

    case 0 :
    case 1 :
    include 'index.php';
    break;

    since it is doing the same thing for each case.

    Good luck! :)
     
    Weirfire, Jun 26, 2006 IP
    itsme likes this.
  12. Borghunter

    Borghunter Well-Known Member

    Messages:
    212
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #12
    Ya, sorry forgot about that, it's probalbly the best and most secure way to go.
     
    Borghunter, Jun 26, 2006 IP