PDA

View Full Version : URL variables in PHP



AusQB
12 Mar 2009, 11:40 AM
<?php
if (isset($_REQUEST['tab'])) {
$tab = $_REQUEST['tab'];}
else {$tab = 1;}
?>




<?php
switch ($tab) {
case 1:
?>

<b>Tab 1</b>
<a href="index.php?tab=2">Tab 2</a>

<?php include("page1.php"); ?>

<?php
break;
case 2:
?>

<a href="index.php?tab=1">Tab 1</a>
<b>Tab 2</b>

<?php include("page2.php"); ?>

<?php
break;}
?>


In the above code, how do I change the variable "tab" to a custom name? For example, instead of having "index.php?tab=1", I would like "index.php?mypage".

Alan
12 Mar 2009, 04:21 PM
Your switch statement won't work. In order for it to work, all the "html" code must be contained within the php tags. Otherwise the html code won't be parsed by the PHP. You need to change to this:



<?php
switch ($tab)
{
case 1:
echo '<b>Tab 1</b>';
echo '<a href="index.php?tab=2">Tab 2</a>';
include("page1.php");
break;
case 2:
echo '<a href="index.php?tab=1">Tab 1</a>';
echo '<b>Tab 2</b>';
include("page2.php");
break;
default:
break;
}
?>


Now as for the url. I don't believe you can do that.

index.php?mypage

You are defining a get variable here called mypage and inserting nothing in it. The ? will see the mypage as a variable and not a value to be used. You will have to put in it like so:

index.php?page=Home

I was looking into "the urls used in wikipedia", I can't for the life of me remember the name of the technique and I couldn't find it anyway. (Probably because I don't know what to look for.) But maybe that's a direction you need to look into.