Welcome to WebmasterWorld Guest from 54.162.4.139

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Several pages in one page?

index.php?id=1 and index.php

     
2:31 am on Feb 28, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 2, 2008
posts:99
votes: 0


Well I see it all the time and I'm wondering how you would do that where you have the index.php file that has some content on it and then if you do like this "index.php?id=1" then its almost a completely different page. Here's a code I found but its not working for everything I want:

*****Code:*********************************
<?php

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

switch($_GET['page'])
{
case "home":
?>HOME HTML HERE<?php
break;
case "test":
?>TEST HTML HERE<?php
break;
case "help":
?>HELP HTML HERE<?php
break;
default:
?>DEFAULT HTML HERE<?php
}

}

?>
*****************************************

Everything works right except on the default page when you add the html there and you got the the page it works but when you go to like index.php?page=help it has the html for that page plus the default page.

What do I do to make them like separate pages but all in one file.

8:17 am on Feb 28, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 2, 2007
posts:59
votes: 0


the ?id=1 is a variable within the URL that defines which content gets shown.

To show specific content when no variable is appended to the URL you can use an IF, which says IF condition 1 is met do something, otherwise dont.

I use this on one of my sites I have a page called artist.php. If this is called without a variable, I show a list of all artists. If a variable is on the URL (artist.php?a=1) I show only relevant information for that artist.

9:55 am on Feb 28, 2008 (gmt 0)

Full Member

5+ Year Member

joined:June 1, 2007
posts:201
votes: 0


I tested this out and it seems to work on my test box see how it works for you.


<?php
function viewPageSection($page)
{
switch($page) {
case 'home':
$section = 'home';
break;
case 'test':
$section = 'test';
break;
case 'help':
$section = 'help';
break;
default:
$section = false;
break;
}
return $section;
}

//get the page name from the get request
if(!empty($_GET['page']))
$section = viewPageSection($_GET['page']);

//page information goes below add whatever content you like
if (isset($section) && $section == 'home') {
//home section info goes here
print "<p>Print out some home section text.</p>";
} elseif (isset($section) && $section == 'test') {
//test section info goes here
print "<p>Print out some test section text.</p>";
} elseif (isset($section) && $section == 'help') {
//help section info goes here
print "<p>Print out the help information section.</p>";
} else {
//if all above fail then display this
print "<p>Just Information that gets shown if all other page checks fail.</p>";
}

?>

Best Regards,
Brandon

2:42 pm on Feb 28, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 2, 2008
posts:99
votes: 0


Ok thanks, I think that works. Thats what I was looking for thanks!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members