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
- <!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>
Expand|Select|Wrap|Line Numbers
- <?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>
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
- <?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>
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!