Web Hosting Talk







View Full Version : PHP Help Needed $$


liam_tmt7
07-15-2003, 05:55 PM
Its funny how php programers see the $$ sign and instaly think cash, but no my friend you are wrong. Its a trick to get you to come into my thread and help me for free :-) please.

I am using the following piece of code:



<?php
switch( $_GET['page'] ) {
case 'home' :
include 'main.php';
break;

case 'portfolio' :
include 'portfolio.php';
switch ($_GET['id']) {

default:
echo "portfolio.php";
break;

case "design";
echo "design";
break;

case "maintenance";
echo "maintenace.php";
break;

case "flyer";
echo "flyer.php";
break;

case "card";
echo "card.php";
break;

case "graphic";
echo "graphic.php";
break;

case "program";
echo "program.php";
break;
}

break;
case 'design' :
include 'design.php';
break;
case 'hosting' :
include 'hosting.php';
break;
case 'templates' :
include 'tesmplates.php';
break;
case 'contact' :
include 'contact.php';
break;
case 'services' :
include 'services.php';
break;
default:
include 'main.php';
break;
}
?>


but when i load index.php?page=portfolio&id=design it loads the portfolio page, even though design exisits.

can anyone tell me where I am going wrong, or the correct code please?

cheers guys
Liam

zubuz
07-15-2003, 06:13 PM
Your case statements should be terminated with : (a colon) instead of ; (a semicolon).

Also, as a matter of good programming practice, the default case should be specified last. I don't think it will work with defaults specified first.

liam_tmt7
07-15-2003, 06:25 PM
ive changed it so all cases end with a : but it still doesnt solve the problem

Liam

zubuz
07-15-2003, 08:09 PM
Okay. Here is the problem:

You call index.php?page=portfolio&id=design. The server executes as follows:


<?php
switch( $_GET['page'] ) { // look at $_GET['page] == portfolio
case 'home' : // nope
include 'main.php';
break;

case 'portfolio' : // yes
include 'portfolio.php'; // so it loads portfolio.php
switch ($_GET['id']) { // now look at $_GET['id'] == design

default: // this is wrong...put at the end of the switch
echo "portfolio.php";
break;

case "design"; // yes
echo "design"; // so it echoes "design"
break; // skip to end of switch

case "maintenance";
echo "maintenace.php"; // note your misspelling here
break;

case "flyer";
echo "flyer.php";
break;

case "card";
echo "card.php";
break;

case "graphic";
echo "graphic.php";
break;

case "program";
echo "program.php";
break; // this is the end of switch
}

break; // this breaks out of the first switch
case 'design' : // you never get here because page==portfolio
include 'design.php';
break;
case 'hosting' :
include 'hosting.php';
break;
case 'templates' :
include 'tesmplates.php'; // note the misspelling here
break;
case 'contact' :
include 'contact.php';
break;
case 'services' :
include 'services.php';
break;
default:
include 'main.php';
break; // end of the first switch
}
?>


So you've told it to load portfolio.php. If you want to load design.php, you're going to have to change the GET request to
?page=design

Good luck.

Promethyl
07-18-2003, 09:38 AM
Liam:

The subswitch would be inside the included file.