help with php layout

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jaimebienlesfruits
    New Member
    • Apr 2007
    • 14

    help with php layout

    Hi guys,

    I'm a newbie at PHP. For the past few weeks, I've been trying to work a on PHP layout/template and creating a dynamic site, so that the pages look something like site.com/index.php?page=file. What I really want is want is basically to have a separate design/template page and a separate content/file page and of course, custom titles for pages (not the same title throughout the site). I don't really want to use PHP includes--I tried that once for another website (header.php, footer.php, sidebar.php) and so many includes defeated the purpose. If you know of a way to do that, just disregard what I have below.



    I've tried two solutions, but none really works yet.

    The first one, which is really the one I want to go with, looks like this.

    code for index.php:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <?php $site_title = "In Transit: ";?> 
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta name="Keywords" content="in transit" />
    <meta name="Description" content= "Come drop by In Transit's official site!" /> 
    <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    
    <body>
    
    <?php
    if(isset($_GET['page']))
    {
    $page=$_GET['page'];
    if(is_file($page . ".php"))
    {
    include $page . ".php";
    }
    else
    {
    if(is_file($page . ".html"))
    {
    include $page . ".html";
    }
    else include "404.php";
    }
    }
    else include "main.php";
    ?>
    
    </body>
    
    </html>
    code for file.php:
    Code:
    <?php $title = "Main";?>
    
    <title><?php echo $site_title . $title;?></title>
    
    <h1><?php echo $title;?></h1>
    
    <p>Welcome! Enjoy your visit and drop us a note any time. Comments and suggestions appreciated!</p>
    The main flaw in this is that the <title> tag is placed in the body, and that's just wrong. How do I get around this?

    What I also want, actually, is for the $title to be whatever the file name is, using this code

    [PHP]<?php
    $title = basename ($_SERVER["SCRIPT_FILENAM E"]);
    $title = str_replace ("_"," ",$page);
    $title = str_replace (".php","",$pag e);
    ?>[/PHP]

    But so far, that seems a bit complicated because of the presence of the $page variable and the fact that that when the url looks like this site.com/index.php?page=file.

    Anyway, that's not so important yet. The second solution that I tried looks like this.

    code for index.php:
    [php]<?php
    function title($title) {
    echo "<html><head><t itle>$title</title></head><body>";
    }

    if(isset($_GET["page"]))
    {
    $page=$_GET["page"];
    if(is_file($pag e . ".php"))
    {
    require ($page . ".php");
    }
    else {require ("404.php"); }
    }
    else {require ("main.php") ;}

    echo "<i>Made by Pristine Ong</i></body></html>";
    ?>[/php]

    code for file.php:
    Code:
    <?php title("Portrait of a Family"); ?>
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus in tortor sit amet enim consectetuer facilisis. Vestibulum mollis eros eu libero porttitor euismod. Curabitur congue, lacus a porttitor sodales, lorem felis aliquet tellus, vel egestas ante dolor eget quam. Proin consequat, nibh eu convallis faucibus, lacus est consectetuer enim, ut cursus tellus felis eu ligula. Fusce ac dolor. Integer placerat egestas nunc. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Integer pellentesque suscipit sem. Proin fringilla quam. Morbi nisl dui, aliquet ac, euismod sit amet, sagittis sed, urna. Nullam nec mauris. Pellentesque vestibulum magna sed nisi. Proin dignissim sapien ut urna. Nam malesuada ligula a nisl. Cras non arcu. Etiam ipsum libero, scelerisque congue, auctor eget, eleifend id, nisl.</p>
    The problem with this solution is that in index.php, I can't really have more complex html coding. I have to keep escaping quotes. Also, I'd prefer to separate the html and php coding more.


    What should I do? This thing is really getting to me and I tried it so many times and basically, all this week, I haven't really gotten anyway. Any help would be appreciated.

    Thanks!
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    I usually put all HTML code in string variables and read them either from a seperate .php file or from a database.

    For example:
    [PHP]
    $HTML_BODY = '
    <html><head><ti tle>Hello</title></head>
    <body bgcolor="#00000 0" color="#FFFFFF" >

    <div align="center"> !-Header-!</div>
    <div align="center"> !-Content-!</div>

    </body>
    </html>';

    if(isset($_GET['page']))
    {
    $content = GetContent($_GE T['page']);
    $header = GetHeader($_GET['page']);

    $old = array("!-Header-!", "!-Content-!");
    $new = array($header, $content);
    echo str_replace($ol d, $new, $HTML_BODY);
    }
    [/PHP]

    The GetContet() and GetHeader() functions would fetch the page by reading it from the database or including it.

    If you don't want that you could put the HTML code in seperate .html files and use the fread() functions to read the file into a string.

    Comment

    • jaimebienlesfruits
      New Member
      • Apr 2007
      • 14

      #3
      Atli, thanks for your reply. Actually, between my post and today, I found a little solution (sorry, at that time, I couldn't really understand what you did. Now, I understand a little more though). I realised that in the php "layout", the title tag can be written to look like this

      Code:
      <title><?php echo $title;?></title>
      At the start of the page, I define the $page variable, which is what comes after index.php?page= any_random_file .

      [php]$page=$_GET["page"];
      $page=str_repla ce("_", " ", $page);
      $page=str_repla ce(".php", "", $page);
      $title=$page;[/php]

      The problem with this is that sometimes, the file name is not what I want the page title to be, for example, if the title contains accented characters. Currently, my solution (not the most robust) is to include this code at the start of the layout page for all pages with custom titles so that basically, the file name is replaced by what I want it to be called.

      [php]$page=str_repla ce("main", "Official Site", $page);[/php]

      Yeah...this is my best shot so far. What do you think?

      Cheers!

      Comment

      Working...