[PHP] index.php?do=page (Howto)

Mikey

Resident
Joined
Sep 26, 2009
Messages
507
Reaction score
4
FP$
3,028
I originally posted this tutorial at my site, The Geek District - here it is for the benefit of ForumPromotion

So, you decided to build a site using PHP and now have several files: page.php, portfolio.php, contact.php, etc. This is how many people start out, and it's quite messy. Using separate files means that any bug found in one file will need to be fixed in all the files containing that same code. It's not fast, it's not easy, and it's a hassle.

Wouldn't it be better to only edit one file and have it apply to all other files that require the same code? The good news is that this is possible using the if statement!

So, open up your new file.. here's how I always start mine:

Code:
<?php
// include doctype, head tags, title, global styling, and scripts..
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
	\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\"> 
<head> 
	<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/> 
<title>My awesome one page script!</title> 
<link href=\"favicon.ico\" rel=\"icon\" type=\"image/x-icon\" /> 
<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\"></head> 
<body> ";

We've just echoed out the stylesheet link, doctype, header, etc. All the standard stuff, still sort of HTML.. Now, to define what the user hits when they first get on your page..

Code:
if ($_GET['do'] == 'home' || $_GET['do'] == '') {
echo "<ul>
<li><a href=\"index.php?do=contact\" target=\"_blank\">Contact Me</a></li>
<li><a href=\"index.php?do=portfolio\" target=\"_blank\">Portfolio Page</a></li>
<li><a href=\"index.php?do=services\" target=\"_blank\">Services</a></li>
</ul>
Welcome to my awesome page!<br />Choose from the above menu!";
}

This above section of code will make it so when your visitor hits index.php, or index.php?do=home - shows that piece of text, in this case, a menu of contents, linking to the rest of the site. Of course, specifying the 'home' part is completely optional, but I prefer it.

Right, now you have your basic 'landing page' when the user hits your script, now you want to make the ?do=contact and ?do=portfolio, etc, work.

You can use the same principle as I have up there, changing 'home' to your variable. Here's a sample for the contact form.

Code:
if ($_GET['do'] == 'contact') {
echo "<h1>Contact page!</h1>
Enter Name: <input type=\"text\" name=\"name\" /><br />
Enter Email Address: <input type=\"text\" name=\"email\" /><br />
Enter Message: <input type=\"text\" name=\"msg\" style=\"width:50%;height:150px;\"/><br />
<input type=\"submit\" value=\"submit\" />";
}


and finally, dont forget to close all the body tags and such which we defined in the top of the page, before the if's. You can do that by simply echoing it out at the bottom, like so:

Code:
echo "</body>
</html>";
?>

This method is an extremely effective way of enclosing your site content in a 'wrapper', where only the content changes, and not the styling or the header/footer. If you're feeling adventurous you can even code things like sidebars into there.

So your final script should look something along the lines of this (provided you only used the contact reference.

Code:
<?php
// include doctype, head tags, title, global styling, and scripts..
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
	\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\"> 
<head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/> 
<title>My awesome one page script!</title> 
<link href=\"favicon.ico\" rel=\"icon\" type=\"image/x-icon\" /> 
<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\"></head> 
<body> ";

if ($_GET['do'] == 'home' || $_GET['do'] == '') {
echo "<ul>
<li><a href=\"index.php?do=contact\" target=\"_blank\">Contact Me</a></li>
<li><a href=\"index.php?do=portfolio\" target=\"_blank\">Portfolio Page</a></li>
<li><a href=\"index.php?do=services\" target=\"_blank\">Services</a></li>
</ul>
Welcome to my awesome page!<br />Choose from the above menu!";}

if ($_GET['do'] == 'contact') {
echo "<h1>Contact page!</h1>
Enter Name: <input type=\"text\" name=\"name\" /><br />
Enter Email Address: <input type=\"text\" name=\"email\" /><br />
Enter Message: <input type=\"text\" name=\"msg\" style=\"width:50%;height:150px;\"/><br />
<input type=\"submit\" value=\"submit\" />";}

echo "</body>
</html>";
?>

I've attached the full final file, which I've tested, and it works, you can simply swap out my content for yours, build upon it, whatever you like. 🙂

Hope this was useful!

EDIT: Forgot to add, you can also use this method with INCLUDES.. so you can include your contact.php to index.php?do=contact - though I really don't see the point in that. 😛
 

Attachments

I am confused by what you would use this for. Can you explain what this does differently?
 
It means you don't have to change the footer, sidebar, header etc for every page each time you make a change THT.

Why don't you use 's instead of \"s?
 
Really, the best way to do this is:

Code:
<ul class='mavigation'>
<?php
$page = array('Home', 'Contact');

foreach ($page as $key => $value) {
  echo "<li><a href='index.php?do=".$value."'>".$value."</a></li>";
}
?>
</ul>
<?php
if (is_file("files/".$value.".html") {
  echo file_get_contents("files/".$value.".hrml");
} else {
  echo "Page not found.";
}
?>

You should put the page names you want into the $page array. The contents of the pages are stored in the files directory with the name you have in the $pages array, then ".html" as the file name.
 
Jonathan said:
It means you don't have to change the footer, sidebar, header etc for every page each time you make a change THT.

Why don't you use 's instead of \"s?
I don't :S I just use the php includes :S
 
Raymond said:
I'd rather use Cosmics way 🙂

Cosmics way is more efficient, but this tutorial was meant to be easy to understand for absolute beginners. :great:
 
Back
Top Bottom