5 Replies - 4985 Views - Last Post: 14 April 2005 - 06:21 PM Rate Topic: -----

#1 aswettla  Icon User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 603
  • Joined: 27-September 02

Switch Error

Posted 14 April 2005 - 04:08 PM

Hi,

I'm using the following code to produce a index.php?p=contact style thingy. Here is my code, then I try to run index.php?p=contact i get the following error.

Quote

Parse error: parse error, unexpected T_VARIABLE, expecting '(' in c:\program files\easyphp1-7\www\czeg\index.php on line 2


my code:

<?php
switch $_GET['p']
{
    case "login":
        include('login.php'); 
        break;
    case "news":
        include('news.php'); 
        break;
    case "pilots":
        include('pilots.php'); 
        break;
    default:
        include('main.php'); 
}
?>



Could somebody please help!

This post has been edited by aswettla: 14 April 2005 - 04:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Switch Error

#2 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4517
  • View blog
  • Posts: 28,417
  • Joined: 21-March 01

Re: Switch Error

Posted 14 April 2005 - 04:16 PM

wouldnt you be better served just doing include('$_GET['p'].php'); ???
Was This Post Helpful? 0
  • +
  • -

#3 aswettla  Icon User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 603
  • Joined: 27-September 02

Re: Switch Error

Posted 14 April 2005 - 04:19 PM

Super, now I fixed it up using your suggestion and I get this error.

Parse error: parse error, unexpected T_CASE in c:\program files\easyphp1-7\www\czeg\index.php on line 4
Was This Post Helpful? 0
  • +
  • -

#4 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4517
  • View blog
  • Posts: 28,417
  • Joined: 21-March 01

Re: Switch Error

Posted 14 April 2005 - 04:23 PM

i dunno i probably didn't use the correst syntax for the get, i usually just use $p, maybe someone else can help with the correct syntax since i'm at work and don't have access to a php enabled box.
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Switch Error

Posted 14 April 2005 - 04:42 PM

Try
include($_GET['p'].".php");


Was This Post Helpful? 0
  • +
  • -

#6 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Switch Error

Posted 14 April 2005 - 06:21 PM

Just fixing your original code.
<?php
//You need teh parentheses '(' and ')'.
switch($_GET['p']) 
{
   case "login":
       include('login.php');
       break;
   case "news":
       include('news.php');
       break;
   case "pilots":
       include('pilots.php');
       break;
   default:
       include('main.php');
}
?>



Also note that while Sloth's and Amadeus' code is simpler (and far easier to do) they also open up scripts that you may not want people to see/use. But that's just what I think. And it depends on alot of things.


Oh and before I click 'Add Reply'.
<?php
//If you'd like to check if $_GET['p'] is valid.
$page = ($_GET['p'] == ('login'||'news'||'pilots')) : $_GET['p'] ? 'main';
include($page.'.php');
?>


This post has been edited by hotsnoj: 14 April 2005 - 06:22 PM

Was This Post Helpful? 1

Page 1 of 1