PDA

View Full Version : many pages in one tutorial



Mentor
17-07-2005, 05:27 PM
another one im putting here as me sites tut system still needs upgrading (im in a tutorial mood, rather than working with new db system mood)

Ok this is a simple tutorial showing how to fit many diffent pages in to a single file.

Ok, lets say you have 5 or 6 small pages, but you dont want to clutter your website folder with them, and would prefer to have it all done in a single file.

With PHP this is quite an easy thing to achaive.

Lets say you have 3 html pages, Page1 page2 and page3, and you want to keep them all in thisfile.php

Start the php page as your normaly would



<?php


The next step is to get the varaible carryed in the url, so it knows wich page to display.

In this exsample the varbople used will be $show


if(!$show){ $show = $HTTP_GET_VARS['show']; }

What the above does is simply, run the if statment to find out of the varible $show already exists, wich is may on some web server set ups where the varible is set from the url automatiocaly.

If not, it uses the global varbale $HTTP_GET_VARS to get the varible from the url and set it manualy to $show


Now we have $show as a varible, the rest is just a simple if stament


if($show=="page1"){


The if stament if($show=="page1") tests to see if the contents of $show were equal to that conatined between the ""in this case page1

if so it then ddoes whats between then {} tags.

As these tags can influnce data outside of the php aria, so as not to interfear with the pages html, we can close the php tag



?>


Now it is close the html code for page one would be put here

The page is the reopened


<?php
}


and the closer to the first tag is shut.


So far you have



</php

if(!$show){ $show = $HTTP_GET_VARS['show']; }

if($show=="page1"){
?>

PAge 1 content here, just html as normal

<?php
}


To test for another page we simply carry on the if stament and use, elseis



elseif($show=="page2"){
?>


Wich simply does the same as before, exspet now the content of page2 is here



}

<?php

You would then carry this on to do all the pages so for the 3 pages in the exsample the code would be



<?php

if(!$show){ $show = $HTTP_GET_VARS['show']; }

if($show=="page1"){
?>

PAge 1 content here,

<?php
}
elseif($show=="page2"){
?>

page 2 content here

<?php
}
elseif)$show=="page3"){
?>

and finaly page 3 conetnt here

<?php
}
?>


would be the finished code. If we wanted to show a page if on of teh stored pages had not been requested, you would do as before exspet use the else tag

so you final code would be



</php

if(!$show){ $show = $HTTP_GET_VARS['show']; }

if($show=="page1"){
?>

PAge 1 content here,

<?php
}
elseif($show=="page2"){
?>

page 2 content here

<?php
}
elseif)$show=="page3"){
?>

and finaly page 3 conetnt here

<?php
}
else{
?>

page for if none is requested

<?php
}
?>


Exspet that where it says page 3 content etc, you would actaly have your pages content there.

You can follow the steps and have as many pages as you like stored in it.

To display any pages your url would have to how the $show varible

so in the exsample

(? turns whats after it in to a request string)

thisfile.php?show=page1

would display page1

and
thisfile.php?show=page3

would display page 3

athogh you did not have to use the variables used in the exsample you could have named the pages what ever you liked, and just had the request string match, say if you had stored a page, wich had in its if or else if tag mrpage

it would be accesed by thisfile.php?show=mrpage

this can also be a useful techiqe when writing scripts, such as guest books etc, to keep many differnt code opertains seperate, exspt of cause you would cose the <?php ?> tags dueing them


I hope this serves to help someone :D Please post any questions.

iRoss
17-07-2005, 05:36 PM
Woah. Great Tutorial man. I never knew that could be done :)

JoeComins
17-07-2005, 05:36 PM
Thnkx Mentor. Great advice again

splintercell!
17-07-2005, 08:27 PM
get into database fixing mode dammit !!!11 But great tut

TheGSP
10-08-2005, 05:39 PM
Hey,

You know when you start the php, you used
</ is that sopose to be
<? ??

aaron778
10-08-2005, 06:38 PM
Hey,

You know when you start the php, you used
</ is that sopose to be
<? ??

Yea It Sopose to be this
<?

TheGSP
10-08-2005, 07:45 PM
Hey,

That's ok then. I thought i was making errors in current scripts like this one.

Lysine
10-08-2005, 08:00 PM
Great tut
10/10

Mentor
10-08-2005, 08:38 PM
Hey,

You know when you start the php, you used
</ is that sopose to be
<? ??
Oh yea sorry didnt notice that, must have missed my shift. fixed now :D

Want to hide these adverts? Register an account for free!