Forum Moderators: coopster

Message Too Old, No Replies

Multiple Pages Per One PHP File

multiple pages for one php file

         

cmatcme

5:34 pm on Jan 18, 2005 (gmt 0)

10+ Year Member



I have seen php files where multiple text has been embedded into one pages (ie):

file.php?id=1

would bring up a certain series of images and text and...

file.php?id=2

would bring up another and have wondered how to do it.

Can you tell me how it is done?

Lawbreak

5:47 pm on Jan 18, 2005 (gmt 0)

10+ Year Member



if ($_GET["id"]=="1") {
echo "yadda, yadda, yadda";
} else if ($_GET["id"]=="2" {
echo "qwerty";
} else {
echo "Hello, world.";
}

or, look up "switch" command on php.net

dreamcatcher

7:28 pm on Jan 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, as Lawbreak mentioned, check out a switch statment. Its best to have the header/footer and main body in seperate files. Cuts down on the work load as you don`t have too many pages to update. Here is a little project for you:

1. Copy and paste the following into a text editor.


<html>
<head><title>My Homepage</title>
</head>

<body>
<table cellpadding=3 cellspacing=3 width=100%>
<tr>
<td>
<p><a href="index.php?cmd=page1">Page One</a></p>
<p><a href="index.php?cmd=page2">Page One</a></p>
<p><a href="index.php?cmd=page3">Page One</a></p>
</td>
<td>
<!-- Load Template -->
<p><b>You have just loaded page One.</b></p>
<!-- Load Template -->
</td>
</tr>
</table>
</body>
</html>

2. Go into your code and seperate the code into 3 areas and save each with a .php extension.

So, save the following as 'header.php'


<html>
<head><title>My Homepage</title>
</head>

<body>
<table cellpadding=3 cellspacing=3 width=100%>
<tr>
<td>
<p><a href="index.php?cmd=page1">Page One</a></p>
<p><a href="index.php?cmd=page2">Page One</a></p>
<p><a href="index.php?cmd=page3">Page One</a></p>
</td>
<td>
<!-- Load Template -->

The following as 'footer.php'


<!-- Load Template -->
</td>
</tr>
</table>
</body>
</html>

and the following as 'page1.php'


<p><b>You have just loaded page One.</b></p>

3. Next do 2 'save as' and duplicate your 'page1.php' and call them 'page2.php' and 'page3.php'. So you have now have 5 files. On your page 2 and 3, type You have just loaded page Two and You have just loaded page Three respectively.

4. Next, create a new page and call it 'index.php'. On that page, add the following:


<?php

$cmd = $_GET['cmd'];

@include('header.php');

switch($cmd)
{

case "page1":
@include('page1.php');
break;

case "page2":
@include('page2.php');
break;

case "page3":
@include('page3.php');
break;

}
@include('footer.php');

?>

Finally upload the files to your server and access your index.php page in your browser. When you click each link it should load the appropriate template.

Hope that helps you understand how it works.

dc
:)

dreamcatcher

9:04 pm on Jan 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On thing I forgot. Beneath:

$cmd = $_GET['cmd'];

add the following:

if ($cmd=="") { $cmd = "page1.php"; }

dc