468,161 Members | 1,946 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,161 developers. It's quick & easy.

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:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.  
  4. <?php $site_title = "In Transit: ";?> 
  5.  
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  8. <meta name="Keywords" content="in transit" />
  9. <meta name="Description" content= "Come drop by In Transit's official site!" /> 
  10. <link rel="stylesheet" type="text/css" href="style.css" />
  11. </head>
  12.  
  13. <body>
  14.  
  15. <?php
  16. if(isset($_GET['page']))
  17. {
  18. $page=$_GET['page'];
  19. if(is_file($page . ".php"))
  20. {
  21. include $page . ".php";
  22. }
  23. else
  24. {
  25. if(is_file($page . ".html"))
  26. {
  27. include $page . ".html";
  28. }
  29. else include "404.php";
  30. }
  31. }
  32. else include "main.php";
  33. ?>
  34.  
  35. </body>
  36.  
  37. </html>
code for file.php:
Expand|Select|Wrap|Line Numbers
  1. <?php $title = "Main";?>
  2.  
  3. <title><?php echo $site_title . $title;?></title>
  4.  
  5. <h1><?php echo $title;?></h1>
  6.  
  7. <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_FILENAME"]);
$title = str_replace ("_"," ",$page);
$title = str_replace (".php","",$page);
?>[/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><title>$title</title></head><body>";
}

if(isset($_GET["page"]))
{
$page=$_GET["page"];
if(is_file($page . ".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:
Expand|Select|Wrap|Line Numbers
  1. <?php title("Portrait of a Family"); ?>
  2. <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!
Apr 21 '07 #1
2 1435
Atli
5,058 Expert 4TB
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><title>Hello</title></head>
<body bgcolor="#000000" color="#FFFFFF">

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

</body>
</html>';

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

$old = array("!-Header-!", "!-Content-!");
$new = array($header, $content);
echo str_replace($old, $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.
Apr 21 '07 #2
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

Expand|Select|Wrap|Line Numbers
  1. <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_replace("_", " ", $page);
$page=str_replace(".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_replace("main", "Official Site", $page);[/php]

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

Cheers!
Apr 23 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

15 posts views Thread by Holly | last post: by
53 posts views Thread by Paul Watt | last post: by
1 post views Thread by taishin | last post: by
3 posts views Thread by Andy B | last post: by
reply views Thread by kamranasdasdas | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.