duanjihe5180 2011-10-31 20:31 采纳率: 100%
浏览 19
已采纳

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

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!

  • 写回答

3条回答 默认 最新

  • duanqu9279 2011-11-19 01:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样