Deaf Topics - Expekt bonuses - William Hill Vouchers - Bet365 bonus - Web Hosting

PDA

View Full Version : PHP $_GET Problem


nimierkki
Jun 25th 2006, 6:45 am
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 ?>


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!

Bob_
Jun 25th 2006, 6:53 am
Not certain but I think you should have a $ in front of all those ID's eg $ID

nimierkki
Jun 25th 2006, 6:56 am
sill i get same error

Jangro
Jun 25th 2006, 7:07 am
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.

kjewat
Jun 25th 2006, 7:09 am
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'])))

won't work.

Use something like:
$get=intval($_GET['ID']);
if (isset($get))
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 ?>

nimierkki
Jun 25th 2006, 7:14 am
what i have to write in link?? i cant get that right

<a href="index.php?ID=1">

kjewat
Jun 25th 2006, 7:29 am
what i have to write in link?? i cant get that right

<a href="index.php?ID=1">

You should write something like:

<a href="index.php?ID=1">LinkThextHere</a>

nimierkki
Jun 25th 2006, 7:38 am
i can oppen "new" page design.php i wrote in the link <a href="index.php?ID=2">Thext</a> and it wont work

Borghunter
Jun 25th 2006, 4:44 pm
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'; }
This Always works, you really have no need for isset

By The way if this is in index.php you can't include itself
if ($_GET['ID'] == 1) { include 'index.php'; }

itsme
Jun 26th 2006, 1:18 am
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 (http://www.php.net/switch)

You'll also notice that I wrote the includes with round brackets and double quotes, I suggest you do this also.

Weirfire
Jun 26th 2006, 3:56 am
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! :)

Borghunter
Jun 26th 2006, 11:18 am
Ya, sorry forgot about that, it's probalbly the best and most secure way to go.