How to not have all the code in one site page...

Soldato
Joined
31 Oct 2005
Posts
8,842
Location
Leeds
Sorry, another question, and another vague title

Currently I'm creating pages "all in" so to speak, with HTML and PHP etc all stuck in one file

I want to have one index page that just says

include or require_once CSS, Divs, Content, etc

Therefore keeping the code tidy

I'm not asking for help, I just want to know what this method is called so I can go a googling to learn

Many thanks
 
If your talking about splitting up data or logic elements from display elements then you might want to look into MVC (Model view controller).
 
Also, Kohana is a good framework to use if you want to get into MVC design.

If you just want a way of separating logic from markup and don't want the structure of a full framework, then you might want to look into Smarty.
 
Dunno if there is a name for it but you just need to learn how to separate your header, footer and content and learn how to reference them. Its all simple once you understand how a bit of HTML and PHP.
 
Dunno if there is a name for it but you just need to learn how to separate your header, footer and content and learn how to reference them. Its all simple once you understand how a bit of HTML and PHP.

I tried earlier, I created a php file called index.php

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?php
include($hello.php)
?>
<body>
</body>
</html>

This was hello.php

<?php
print "<br />This is a test "
?>

all I got were error messages
 
pass hello.php as a string, its not a variable so you dont need the dollar sign. try include('hello.php'). If you know the absolute path to hello.php then use that as its faster than giving a relative path.
 
pass hello.php as a string, its not a variable so you dont need the dollar sign. try include('hello.php'). If you know the absolute path to hello.php then use that as its faster than giving a relative path.

legend, gives me something to build on now

God you lot must be laughing at me:D
 
legend, gives me something to build on now

God you lot must be laughing at me:D

I was about to! Calling in other pages is a doddle and a very powerful doddle. You should be flying now, and now you will need to just think long and hard about your file structure! :D
 
right I've ht another snag, or two

Anyhoo I've now got a basic page that simply displays a line of text from a PHP file

How do I set out for example including a CSS file, or the DIVs for content

As I tried include('css.css') and had an error

What I'm basically asking is , well maybe could someone knock up a site

That just has one page, with a content bit, then styling seperate etc, that I can look at the code, I can't find tutorials
 
For example

I have a page index.php

I have 3 files

_footer.html
_header.html
hello.php

Using the include command I can get only one working at a time, if I include more I get all sorts of errors

index.php
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>


<?php

include('_footer.html')
?>
</body>
</html>

_header.html

<div id="menu">

<ul>

<li><a href="http://micholden.site50.net/rss2htm...TE=http://micholden.site50.net/indexnews.html">Home</a></li>
<li><a href="shopaccess.php">NUFC Shop </a></li>
<li><a href="gallery.html">Gallery</a> </li>
<li><a href="contact2.html">Contact</a><br />
<br />
</li>
</li>
</li>
<li><a href="warddrobe.html">NUFC Wardrobe </a></li>
<li><a href="forum.html">Forums </a></li>
<li><a href="register.html">Register</a></li>
<li><a href="map.html">About Us</a></li>
<li><a href="subscribe.html">Subscribe</a></li>
<li></li>
<img src="images/RSS.jpg" alt="" width="40" height="40" />
<li></li><li></li><li></li></ul>

</div>

_footer.html

<div id="footer">

<p><a href="http://validator.w3.org/check?uri=referer"><img

src="http://www.w3.org/Icons/valid-xhtml10"

alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a> <span class="style2">&copy;<span class="style1"> Newcastle United &nbsp;|&nbsp;</span><a href="loginadmin.php">Admin Login</a><span class="style1"> | </span>&nbsp;<a href="conditions.html">Privacy Statement </a></span><br />

</p>

<p><br />

</p>

</div>
 
I usually have something like this at the top of each page
Code:
<?php 
include 'includes/headsection.php'; 
?>

Then somewhere within that file I have

Code:
<LINK REL=StyleSheet HREF="stylesheets/style.css">
 
Yep, just include the css and javascript etc through another include file (in html) so its all wrapped up in one place (if its universal across all your pages). Make sure you include stuff correctly eg.

<LINK href="special.css" rel="stylesheet" type="text/css">
for css

<script type="text/javascript" src="external.js"></script>
for js

etc.

The css in your example wont be handled as php (and you dont want it to be) and thats why it doesnt work.
 
I always include this/a variation of this on all my sites in between my header and footer:

Code:
<?
$a = $_GET['a'];
$filename = "$a.php";
if (file_exists($filename)) {
include $filename;
} else {
include 'welcome.php';
}
?>

Easy to add and create new pages and it's also a kind of semi-inbuilt 404.

ie.
domain.com/?a=portfolio would include your portfolio content so long as you have a file named portfolio.php
domain.com/?a=fgkjfkdf would display your main welcome content
 
Last edited:
I always include this/a variation of this on all my sites in between my header and footer:

Code:
<?
$a = $_GET['a'];
$filename = "$a.php";
if (file_exists($filename)) {
include $filename;
} else {
include 'welcome.php';
}
?>

Easy to add and create new pages and it's also a kind of semi-inbuilt 404.

ie.
domain.com/?a=portfolio would include your portfolio content so long as you have a file named portfolio.php
domain.com/?a=fgkjfkdf would display your main welcome content

Of course, it also means that an attacker could potentially include any PHP file on the server that the Apache process has access to, including the current file (causing recursive inclusion).
 
I'd just like to say thanks to all who helped/ had input into this thread

Got myself sorted, and can now understand how basic some elements are, no doubt I'll be back though

Once again many thanks for putting up with me:)

I always include this/a variation of this on all my sites in between my header and footer:

Code:
<?
$a = $_GET['a'];
$filename = "$a.php";
if (file_exists($filename)) {
include $filename;
} else {
include 'welcome.php';
}
?>

Easy to add and create new pages and it's also a kind of semi-inbuilt 404.

ie.
domain.com/?a=portfolio would include your portfolio content so long as you have a file named portfolio.php
domain.com/?a=fgkjfkdf would display your main welcome content

Brilliant, cheers, goes into my "useful/amazing webcode" folder
 
Last edited:
I'd just like to say thanks to all who helped/ had input into this thread

Got myself sorted, and can now understand how basic some elements are, no doubt I'll be back though

Once again many thanks for putting up with me:)



Brilliant, cheers, goes into my "useful/amazing webcode" folder

Take note of what inquisitor said though. This is a very dangerous piece of code as it does no checking on what 'a' might contain (anything) and the script will blindly include the file if it exists. A safer way of using it may be checking a against an array of allowed files or something.

In fact, wherever a GET is used, the variable should be checked and validated in some way before it is used. Using unchecked/unsanitized data is one of the most dangerous things you can do!
 
Take note of what inquisitor said though. This is a very dangerous piece of code as it does no checking on what 'a' might contain (anything) and the script will blindly include the file if it exists. A safer way of using it may be checking a against an array of allowed files or something.

Again, this is an area in which frameworks can make your life so much easier :cool:

With a proper MVC framework, you'd create a controller for each page, and the framework would then analyse the URI for each request and look up the corresponding controller file (and then automatically call the appropriate function within), doing all the necessary validation behind the scenes.

Give it a look :)
 
Back
Top Bottom