views:

114

answers:

4

I have been making websites for years now. Most of them tend to be under 10 pages and don't require any programming at all. I am wondering if anyone with lots of experience can recommend the standard or best way to go about setting up these types of websites. Let me explain in a bit more detail what I mean.

Here is a typical structure of one of the websites I have made in the past.

/css
/images
/js
/html
/html/about.html
header.php
footer.php
index.php

In this case, my index.php includes the header.php file at the top and the footer.php file at the bottom and I use PHP's switch function to handle the different cases for the content that I would like to view. For example, I might see index.php?z=about in the URL bar and my index.php will look for a file called about.html in /html

I know this all seems a little dumb to ask since is there really a best way to go about doing this?

Depending on the page, one of my main navigation items will have an "active" class. The work is done in PHP but I wonder if using javascript would be a better solution.

I'll appreciate any replies, thanks.

+1  A: 

Just a few things off the top of my head:

  • Yes, stick with PHP for the active class. Some people have JavaScript disabled.
  • You might want to use URL rewriting to de-uglify the URLs. (ex. http://www.example.com/about in the URL bar gets rewritten on the server to http://www.example.com/index.php?z=about)
  • If the site gets bigger, you might want to use a database instead of files in a folder.
  • I'd put header.php and footer.php either outside of the document root or in an includes folder that disallows access to it with a 403 Forbidden error.
icktoofay
I do remember using mod_rewrite in htaccess files once before to rewrite the URL. Thanks for reminding me about that.Your advice to stick the header and footer files in an /inc folder sounds good. I usually left those files in the root because of path problems I always seemed to get myself into. I will see if I can make it so that I don't run into that problem again.
Justin
database connectivity file also should be kept outside the document root
RSK
A: 

Use what is simplest yet does what you need. What you described seems fine and if it works for you, go ahead with it. And yes, use PHP for generating the "active" class - in fact, do as much as you can in PHP so that you don't need to rely on JavaScript.

casablanca
Funnily enough, I wanted to avoid using PHP to take care of my header,content,footer situation, so I developed a way in jQuery to handle the different content pages. You can see it here: http://stackoverflow.com/questions/3155828/how-to-reference-pound-sign-in-jqueryBut a friend told me the same thing you did in regards to not relying on javascript for something as important as page navigation.
Justin
+1  A: 

I have uploaded a zip file of an example site to http://superuntitled.com/downloads/default.zip Take a look if you like.

I like to store all of my html content in included files, and leave my index.php file to call these files into a template.php file...

require_once('common.php');

$view = (isset($_GET['page']) && $_GET['page'] != '') ? $_GET['page'] : '';

switch ($view) {
    case '' :
        $view = 'home';
        $section    = 'templates/template-default.php';
            $pageTitle = "The Homepage";        

        break;

    case 'about' :
        $view = 'about';
        $section = 'templates/template-about.php';  
            $pageTitle = "The About Page";          
    $sidebar = 1;

        break;

    case 'notfound' :
            $section    = "templates/template-not-found.php";   
            $pageTitle  = "404 not found";
        break;
}

require_once 'view/template.php';

The template file is as simple as this:

require_once('header.php');  
if(isset($sidebar))include("view/sidebar.php"); 
echo "<div id='content'>";
    require_once (SERVER_URL.$section);
echo "</div>";
require_once('footer.php');  

This way I can set all sorts of variables in the index.php file. The "pretty urls" are simple with htaccess (RewriteRule ^([-\A-Za-z0-9\_]*)$ index.php?page=$1).

The common.php file contains all of the globals, session variables and includes database connection files and php functions files.

As for the active class, the $view variable can be checked against in the nav section, I use a simple php function:

function curPage($current, $pageTitle) {
  if ($pageTitle==$current) 
  echo " class=\"selected\"";
}

And in the nav section I simply call this function:

<a href="about" <? curPage('about',"$view"); ?> >About</a>

This way keeps things clean and simple.

superUntitled
A: 

Why don't you use a simple templating framework? They are relatively simple to understand and will expose you to some tried and tested methodology.

You'll also gain experience in using resources already available rather that coding everything yourself.

You won't need to make initial design decisions yourself which gets you up and running faster. (in simple cases like this)

For example: http://www.smarty.net/

Pick one with good documentation and an active developer community behind it.

Rimian