构建我的网站布局和内容加载的替代方法

A while ago I built a fan site for a PC game, and I believe that it could be set up better. Basically, right now all of my pages share a common header and footer, and the only real difference between pages is the content loaded into a main table cell.

Currently, I am trying to make 'clean' URLs by having each page in its own directory with an index.php file.

For example, my 'About' page is located in /about/index.php, but it can be accessed by just going to /about

Here is how the index.php looks inside of the 'about' directory:

<?php include '/home/content/69/7232369/html/loco_header.html' ?>
<tr><td>
    <table class="mainpagetable">
<tr>
    <td class="maincontent">
        <table class="contenttable" >
            <tr><td><div class="contenttop"><span class="newheader">About</span></div>
            <div class="contentmiddle" id="contentmiddle">
            <table class="newcontent">
            <tr><td>
            <br>
            <?php include 'about.html'; ?>
        </td></tr>
        </table>
        </div>
        <div class="contentbottom"></div></td></tr>

</table>

            </td>
    <td class="gap">

    &nbsp;</td>
<?php include '/home/content/69/7232369/html/site_footer.html' ?>

So basically, I created the entire layout in HTML, separating the parts of the site by using tables within tables. I have a PHP include for the header, the page content, and the footer. All of my pages look like this, and it seemed like a good approach at first, however I recently switched to a different host and when I copied everything over, none of the includes for the headers/footers worked because of the different path to the root directory:

<?php include '/home/content/69/7232369/html/loco_header.html' ?>

Which was a pain to go through 100+ php files and change the header/footer include on each of them one by one. It was then that I realized I am probably doing something wrong.

So my questions are:

1) Is there function in PHP that returns the root directory for your website, so that I could do something like include $root . "/header.html" ?

2) My method for creating clean URLs creates quite a bit of extra directories and files. Is there a different recommended approach that I can take which would run smooth and generate less files (while still being search engine friendly)?

3) Is there more efficient/more manageable approach for loading the content into the center of my page, that would also work nicely with a method for having clean URLs (see question #2)? AJAX comes to mind, however I would like there to be an actual URL link to each content page, rather than having everything loaded dynamically in the middle of my home page without the URL changing. I honestly don't know if there is a good solution to this.

4) Is it considered poor practice to use tables within tables for structuring my layout? As you can see, I am splitting up my HTML code in the middle of table tags and placing it in 3 different files (header, index, footer) to structure my site. This seems bad to me.

If it helps to clarify the structure of my pages, here is a link to a page on my site: http://locohq.com/about/

Any help is appreciated, thanks!

I ended up removing all of my tables and replacing them with elements and properly structuring using CSS.

For loading my content, I just made a main index page with a "content" container in the center of it. Within that container, I put a simple block of PHP code:

<?php 
        if(isset($_GET["content"])){
            $file = $_GET["content"] . ".php";
            if (file_exists($file))
                include($file); 
            else
                echo "Cannot find page <b>" . $_GET["content"] . "</b>";
        }
        else
            echo "No content loaded";
?>  

Basically I load content into my page by linking to main.php?content=page and the code from whatever the content is gets loaded into the container.

To get around the ugly URL, I used mod_rewrite and put this into my .htaccess

RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9]+)$ pages/main.php?content=$1

So if I entered a URL as www.example.com/about it would load www.example.com/pages/main.php?content=about and then the code inside of the file /pages/about.php would get loaded into my content container. If the user tries to go to a page that doesn't exist, it will load the main page and inside of the content container, it will display a message that the page they are looking for doesn't exist.

Your includes can be relative rather than absolute.

<?php include 'site_footer.html'; ?>

The above would probably work for you.

I believe what your looking for may be:

    <?php 
        $_SERVER['DOCUMENT_ROOT'];
    ?>

but if not you can see everything in the $_SERVER array you have to work with by executing:

    <?php
        echo '<pre>';
        print_r($_SERVER);
    ?>

Good luck, hopefully this is what you were looking for... I am a bit caffeine deprived today!

P.S I found this question as I'm trying to look for the most efficient way to load content into a layout, will update later with what I find!