3 replies [Last post]
Fruitcake
Offline
Enthusiast
Perth, Australia
Last seen: 12 years 19 weeks ago
Perth, Australia
Timezone: GMT+8
Joined: 2004-04-12
Posts: 257
Points: 0

I'd have to say one of my favourite ways of making a website is to have the top half of the Markup in "header.php" and the bottom half of the Markup in "footer.php". Then, i would have "index.php" which would includew those at the top and bottom of the page.

However, there is a problem with one of my more recent problems. between, header and footer, there is a sidebar which may change depending on which page the visitor is on. Now, in the previously mentioned method that leaves me with layout markup inside the page which, as the project continues to grow could get a bit messy and hard to deal with (example code at the bottom of this post).

Is there a better way to separate markup from information without using a database but still utilising separate pages so that Google will still index each separate page for each section?

I've thought of a few methods but it requires a lot of different includes which just makes things messy anyway. I'm also trying to steer clear of Template engines available on the net.

Any help on this would be much appreciated.

Thanks,

Dan.

Example code:

Two problems with index.php is the markup for the news and the breadcrumb trails. Trying to remove layout markup from the information.

header.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<title>Company name</title>
<link rel="stylesheet" type="text/css" href="style.css" />

</head>
<body>

<div id="wrapper">
   <div id="header"><img src="images/sd_logo.jpg" alt="Company Logo" /></div>
   <div id="outer">
      <div id="inner">
         <div id="container">
            <div id="content">

footer.php

         </div>
         <div id="sidebar">
            <a href="#" class="nav"><span></span>Company Overview</a>
            <a href="#" class="nav"><span></span>Services</a>
            <a href="#" class="nav"><span></span>Equipment</a>
            <a href="#" class="nav"><span></span>Rig Locator</a>
            <a href="#" class="nav"><span></span>Innovations</a>
            <a href="#" class="nav"><span></span>Safety</a>
            <a href="#" class="nav"><span></span>Careers</a>
            <a href="#" class="nav"><span></span>Contact Us</a>
         </div>
         <div class="spacer"></div>
      </div>
   </div>
   <div id="footer">
      Copyright &copy; Company Name 2005. |
      <a href="#">home</a> |
      <a href="#">services</a> |
      <a href="#">equipment</a> |
      <a href="#">rig locator</a> |
      <a href="#">innovations</a> |
      <a href="#">safety</a> |
      <a href="#">careers</a> |
      <a href="#">contact us</a> |
   </div>
</div>

</body>
</html>

index.php

<?php include('header.php'); ?>
<div id="trails">
   <a href="#">sitemap</a> | <a href="index.html">home</a> &raquo; <a href="#" class="this">company overview</a>
</div>
   <div id="right">
      <h3>Latest News</h3>
      <ul class="news">
         <li>&raquo; <a href="#">Managing Director competes in Finke Desert Race</a></li>
      </ul>
   </div>
   <div id="main">
      <h1>Welcome To Company Name</h1>
      <p>Paragraph 1</p>
      
      <p class="last">Paragraph 2</p>

   </div>
</div>
<?php include('footer.php') ?>

I am Dan, Dan I am.

Tags:
Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 7 years 46 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

A better way for such a project?

When you make something more complex, your solution needs to be more complex too.

I reckon the simplest ways to stretch your current model would be

include('header.php');
<!-- content -->
include('thispagesidebar.php');

and include the footer in the sidebar file.

or

include('header.php');
<!-- content -->
include('sidebars\thispage.php');
include('footer.php');

there are more complex ways. With apache for instance, it is possible to alter the page request between the browser and the page being served. That way one page can handle requests for many pages. A search engine still sees many pages and links don't include complex query strings.

[edit]
you can get sophisticated:
when there is a different sidebar for each page
- create a subfolder with all the sidebars in it, called 'sidebars'
- include('sidebars\'.basename(__FILE__));

- or more robust

  $sidebar = 'sidebars\'.basename(__FILE__);
  if (file_exists($sidebar))
    include($sidebar);
  else
    include('sidebars/default_sidebar.php');

stonehinge
stonehinge's picture
Offline
Regular
midwest usa
Last seen: 15 years 10 weeks ago
midwest usa
Joined: 2005-06-03
Posts: 20
Points: 0

A better way for such a project?

Chris..S wrote:

  $sidebar = 'sidebars\'.basename(__FILE__);
 

Chris, i'm totally new to PHP, yet I need to put your idea into play immediately. Can you help me understand the intent of the .basename(__FILE__) syntax you have used here. What does that do?

no noobs up in this ma'

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 7 years 46 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

A better way for such a project?

__FILE__ is a special PHP variable which is the complete path to the current file (ie. the file name you need to give to the OS to open the file, path (incl. drive for windows) & actual file name).

basename() takes a path to a file and strips off everything but the actual file name.

If the current file resided at /var/www/mydomain/webroot/thispage.php, basename(__FILE__) would return "thispage.php"

What I was suggesting was a general method for including a file with the same name as the current file but in a sub-directory.

FYI, your best resource for PHP reference information is www.php.net. Putting php function names or reserved words into the search field will nearly always get you the appropriate manual page.