473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1668
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

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

Similar topics

15
by: Holly | last post by:
I'm trying to get this page to look the same in Netscape and IE. I have a box that says 'Upcoming Events' on the lower left of the page. It's supposed to have a gray background, but in NS 4.x it...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
53
by: Paul Watt | last post by:
Hi again, Ok so I've got two new problems with this layout http://www.paulwatt.info/test/turn/ 1) When I increase the width of the Maincontent Div from 73% to 74% to line it up with the right...
1
by: danxavier | last post by:
I successfuly installed dd.php and sajax.php files. It runs fine, but I would like to link the $items to an image. I called the field in mysql with the link "pic". Any help would be AWESOME!!! I've...
0
by: magicofureyes | last post by:
Hello Guys im a just a new user and i dnt knw much abt Xml i want to upload a new template in Blogger so got some free coding but when i save this code in Blogger template it say '''' Your...
2
by: Francesco | last post by:
Hi there! I'm trying to organize my sources into a webroot tree like this, webroot index.htm - only contains index.php into a frame index.php - require_once('inc/layout.php') ...
9
greensleeves
by: greensleeves | last post by:
Hi, Im a newbie to PHP and I'm wondering if someone could help me out with this Tryng to search out products from a database by clicking a checkbox option for instance if description contains...
2
by: sriniwas | last post by:
Hi Frnd's, m using prefuse visulation,it's have one display class and this class have one saveImage(outPutStream, String jpg,double size);. now graph is converting ia jpg image properly.now my...
1
by: taishin | last post by:
so far i got this... but the shapes that i created i need to be able to click and dragg and move around with the mouse..help!!!! and also some where here i need to draw and equal sided triangle ...
3
by: Andy B | last post by:
I am creating a CSS layout and need to test it with different browsers. I have the framework for the layout done with contrasting colors for each section. Can anybody if possible, go to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.