Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

HTML Forum


You are currently viewing our HTML Forum as a guest. Please register to participate.
Login



Post a Project »

Find a Professional HTML Freelancer!

Find a Freelancer to help you with your HTML projects

FREE Outsourcing eBook!

Reply
Where To Put DOCTYPE, etc In PHP Includes
Old 07-15-2009, 11:43 PM Where To Put DOCTYPE, etc In PHP Includes
Experienced Talker

Posts: 30
Trades: 0
Am about to change website structure similar to the following (for each page):

Quote:
<?php

include('overall_header.html');

include('main.html');

include('overall_footer.html');

?>
The 'header' and 'footer' were previously contained in the main.html file and the main contained the DOCTYPE, head, html, etc tags.

The question: where should the tags (DOCTYPE, head, html, etc) be contained now? Works with the "tags" in main.html, but I'm not sure it's not a 'fluke' and just luck.
html911rap is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-16-2009, 12:07 AM Re: Where To Put DOCTYPE, etc In PHP Includes
racer x's Avatar
Ultra Talker

Posts: 457
Name: Randy
Location: Northern Wisconsin
Trades: 0
You can structure includes any logical way you want but since you always want the Doctype at the top, in your case just make sure that include(overall_header) has the doctype in it.

Also, just IMO, I use the extension .php for my includes. You don't have to, the html will render the same, but this way, if you ever need an include within an include, you can do so.
racer x is offline
Reply With Quote
View Public Profile Visit racer x's homepage!
 
Old 07-16-2009, 12:08 AM Re: Where To Put DOCTYPE, etc In PHP Includes
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Since DOCTYPEs should be the first thing output, overall_header.html should contain them.

But, let's get to the real issue. When you broke the page into 3 segments, you should have had a reason. What most people would do is have your header have everything including and above <body> (maybe add in a bit of navigation or something too... stuff that's at the top of every page using this method) and your footer would contain footer stuff designed to show up on every page using this method too. Knowing the purpose of the files instantly answers your question.

Hope that helps!
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-16-2009, 12:12 AM Re: Where To Put DOCTYPE, etc In PHP Includes
racer x's Avatar
Ultra Talker

Posts: 457
Name: Randy
Location: Northern Wisconsin
Trades: 0
@JeremyMiller - I was going to ask the same thing.
racer x is offline
Reply With Quote
View Public Profile Visit racer x's homepage!
 
Old 07-16-2009, 01:22 AM Re: Where To Put DOCTYPE, etc In PHP Includes
Experienced Talker

Posts: 30
Trades: 0
Thank You Both !!
html911rap is offline
Reply With Quote
View Public Profile
 
Old 07-16-2009, 06:04 PM Re: Where To Put DOCTYPE, etc In PHP Includes
Experienced Talker

Posts: 30
Trades: 0
Follow-on question:

Can the <meta ...>, <title>,<style>, etc tags be placed after the <body> tag? If not, I'm not sure how those pieces of data could be unique for each page.
html911rap is offline
Reply With Quote
View Public Profile
 
Old 07-16-2009, 06:39 PM Re: Where To Put DOCTYPE, etc In PHP Includes
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
Originally Posted by html911rap View Post
Follow-on question:

Can the <meta ...>, <title>,<style>, etc tags be placed after the <body> tag? If not, I'm not sure how those pieces of data could be unique for each page.
No, those need to be contained within the <head> of the document. I sometimes structure documents something like this:
PHP Code:
<?php include "inc/init.inc.php";//functions, declarations, DOCTYPE, etc.
?>
<title>Example</title>
<meta name="description" content="whatever" />
<?php include "inc/head.inc.php";
//shared styles, scripts, opening body tag, wrapper info, etc.
?>
<!--HTML, additional info goes here-->
<?php include "inc/nav.inc.php"//navigation
?>
<!--more unique content-->
<?php include "inc/foot.inc.php";
//shared footer, closing </body> tag, scripts, etc.
?>
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 07-16-2009, 08:43 PM Re: Where To Put DOCTYPE, etc In PHP Includes
Experienced Talker

Posts: 30
Trades: 0
Thank you ... but
I'm confused and dense (some would spell it "dunce").

Keeping in mind the last response ... my target structure is something like (in simplistic form):


Quote:
<?php
include('header.html');
include('page1.html');
include('footer.html');
?>
and


Quote:
<?php
include('header.html');
include('page2.html');
include('footer.html');
?>
and


Quote:
<?php
include('header.html');
include('page3.html');
include('footer.html');
?>
and ...............,where page1 and 2 and 3 ...each has unique styles/scripts.

(I have an overall CSS file but it would be huge if all the unique styles were put there.)

I think that if the last response method is used, then I'd just have another layer of php files.

What am I missing?
html911rap is offline
Reply With Quote
View Public Profile
 
Old 07-16-2009, 09:25 PM Re: Where To Put DOCTYPE, etc In PHP Includes
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
I find those structures very limiting. As you have discovered, there is unique data that needs to be in the top section before the opening <body> tag.

However if you divide the middle file up with its own includes, you could probably get away with keeping it this simple on the main page. Just remember, the second file will need to contain part of the <head> so that it may have unique meta data, and a <title>.

Find an organizational scheme and work with it. This is a personal choice, but it has to make sense to you. I'm sure you'll figure something out. There is not one answer to this question.
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 07-16-2009 at 09:27 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 07-17-2009, 02:02 PM Re: Where To Put DOCTYPE, etc In PHP Includes
Novice Talker

Posts: 7
Name: James Choo
Trades: 0
if I'm not misunderstanding what are you talking about, your php script might be like this:


PHP Code:
<!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">
<head>
<title>Hello everyone</title>
<meta name="description" content="whatever" />
<link href="../style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
echo('header.php');
?>
 
<?php
echo('body text....');
?>
 
<?php
echo('footer.php');
?>
</body>
</html>
__________________
Webmaster Infomation & Solutions
Free Tools & Resources

Please login or register to view this content. Registration is FREE
JTcuy is offline
Reply With Quote
View Public Profile
 
Old 07-17-2009, 06:16 PM Designing An Application Architecture & Implementation
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I think it might help to give, as Chris once called it, "a tutorial of a post"...

The structure of a program can significantly impact how much code is needed for a site, how complicated it is to add new pages, and how complicated it is to make changes. Each coder is likely to answer the question of application structure differently, but it is the thought process involved which will determine how your app is structured. Let's see this in action!

First, we'll define our goals:

1) Our application will be a PHP 5 application where URLs are of the form "http://www.mydomain.com/file-name.php".
2) The application needs to separate CSS, JavaScript, XHTML, and page-specific content from each other.
3) Our goal is to minimize the amount of code needed for adding pages and making changes where common routines are commonly accessible.

We can skip #1 above for the time being, but let's look at what #2 entails. First, to separate CSS, JavaScript, and XHTML, we'll want to use an XHTML structure of the form:

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="stylesheet.css" />
    <script type="text/javascript" src="javascript.js"></script>
  </head>
  <body>
  </body>
</html>
With that setup, we're now ready to take a look at adding in page-specific content while keeping it separate. Let's try a structure like this:

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
<?php
  
//Allow access to a global object containing page-specific content
  
global $page_specific_content;
  
  if (
strlen($page_specific_content->title) == 0) {
    
//Page title was not set, so default to website URL
    
$page_specific_content->title $_SERVER['HTTP_HOST'];
  }
  
//Output page title
  
echo '    <title>'.$page_specific_content->title."</title>\n";
  
  
//Output page stylesheet include(s)
  
if (strlen($page_specific_content->stylesheet) > 0) {
    if (
is_array($page_specific_content->stylesheet)) {
      
//An array of stylesheet file paths.
      
echo "    <link rel=\"stylesheet\" type=\"text/css\" href=\"".implode("\" />\n  <link rel=\"stylesheet\" type=\"text/css\" href=\"",$page_specific_content->stylesheet)."\" />\n";
    } else if (
is_string($page_specific_content->stylesheet)) {
      
//A single stylesheet
      
echo '    <link rel="stylesheet" type="text/css" href="'.$page_specific_content->stylesheet."\" />\n";
    }
  }
  
  
//Output page javascript include(s)
  
if (strlen($page_specific_content->javascript) > 0) {
    if (
is_array($page_specific_content->javascript)) {
      
//An array of javascript file paths.
      
echo "    <script type=\"text/javascript\" src=\"".implode("\"></script>\n  <script type=\"text/javascript\" src=\"",$page_specific_content->stylesheet)."\"></script>\n";
    } else if (
is_string($page_specific_content->javascript)) {
      
//A single javascript
      
echo '    <script type="text/javascript" src="'.$page_specific_content->javascript."\"></script>\n";
    }
  }
  
  
//Output other headers
  
if (strlen($page_specific_content->xhtml_header) > 0) {
    if (
is_array($page_specific_content->xhtml_header)) {
      
//An array of xhtml_header file paths.
      
echo implode("\n    ",$page_specific_content->xhtml_header);
    } else if (
is_string($page_specific_content->xhtml_header)) {
      
//A single xhtml_header
      
echo '    '.$page_specific_content->xhtml_header."\n";
    }
  }
?>
  </head>
  <body>
<?php
  
if (strlen($page_specific_content->xhtml_body) == 0) {
    echo 
$page_specific_content->page_not_found;
  } else {
    echo 
$page_specific_content->xhtml_body;
  }
?>
  </body>
</html>
Now, to demonstrate a couple of methods for applying point #3 above, we could simplify the code above to either of these 2 methods (and many others):

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
<?php
  
//Allow access to a global object containing page-specific content
  
global $page_specific_content;
  
  if (
strlen($page_specific_content->title) == 0) {
    
//Page title was not set, so default to website URL
    
$page_specific_content->title $_SERVER['HTTP_HOST'];
  }
  
//Output page title
  
echo '    <title>'.$page_specific_content->title."</title>\n";
  
  
$custom_includes = array('stylesheet'=>array('prefix'=>'    <link rel="stylesheet" type="text/css" href="''suffix'=>"\" />\n"),
                           
'javascript'=>array('prefix'=>'    <script type="text/javascript" src="''suffix'=>"\"></script>\n")
                          );
  
  
//Output page custom_includes
  
foreach ($custom_includes as $custom_type=>$fixes) {
    if (
strlen($page_specific_content->$custom_type) > 0) {
      if (
is_array($page_specific_content->$custom_type)) {
        
//Custom type is an array
        
echo $fixes['prefix'].implode($fixes['suffix'].$fixes['prefix'], $page_specific_content->$custom_type).$fixes['suffix'];
      } else if (
is_string($page_specific_content->$custom_type)) {
        
//Custom type is a string
        
echo $fixes['prefix'].$page_specific_content->$custom_type.$fixes['suffix'];
      }
    }
  }
  
  
//Output other headers
  
if (strlen($page_specific_content->xhtml_header) > 0) {
    if (
is_array($page_specific_content->xhtml_header)) {
      
//An array of xhtml_header file paths.
      
echo implode("\n    ",$page_specific_content->xhtml_header);
    } else if (
is_string($page_specific_content->xhtml_header)) {
      
//A single xhtml_header
      
echo '    '.$page_specific_content->xhtml_header."\n";
    }
  }
?>
  </head>
  <body>
<?php
  
if (strlen($page_specific_content->xhtml_body) == 0) {
    echo 
$page_specific_content->page_not_found;
  } else {
    echo 
$page_specific_content->xhtml_body;
  }
?>
  </body>
</html>
or

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
<?php
  
//Allow access to a global object containing page-specific content
  
global $page_specific_content;
  
  if (
strlen($page_specific_content->title) > 0) {
    
//Page title was not set, so default to website URL
    
$page_specific_content->title $_SERVER['HTTP_HOST'];
  }
  
//Output page title
  
echo '    <title>'.$page_specific_content->title."</title>\n";
  
  
//Output headers
  
if (strlen($page_specific_content->xhtml_header) > 0) {
    if (
is_array($page_specific_content->xhtml_header)) {
      
//An array of xhtml_header file paths.
      
echo implode("\n    ",$page_specific_content->xhtml_header);
    } else if (
is_string($page_specific_content->xhtml_header)) {
      
//A single xhtml_header
      
echo '    '.$page_specific_content->xhtml_header."\n";
    }
  }
?>
  </head>
  <body>
<?php
  
if (strlen($page_specific_content->xhtml_body) == 0) {
    echo 
$page_specific_content->page_not_found;
  } else {
    echo 
$page_specific_content->xhtml_body;
  }
?>
  </body>
</html>
For the rest of this article I'm going to be working with the first of these 3 code segments.

Next we need to establish a structure for our application which allows us to use this XHTML template. First, we'll need to create a class for our $page_specific_content variable to use. Let's use this:

PHP Code:
class XHTMLPage {
  private 
$settings = array();
  
  public function 
__construct() {
    
//Initialize variable(s)
    
$this->settings['xhtml_body'] = '';
  }
  
  public function 
__get($variable_name) {
    switch (
$variable_name) {
      case 
'title':
      case 
'stylesheet':
      case 
'javascript':
      case 
'xhtml_header':
      case 
'xhtml_body':
        if (empty(
$this->settings[$variable_name])) {
          return 
'';
        } else {
          return 
$this->settings[$variable_name];
        }
        break;
      case 
'page_not_found':
        if (empty(
$this->settings[$variable_name])) {
          return 
'Page Not Found';
        } else {
          return 
$this->settings[$variable_name];
        }
        break;
    }
  }
  public function 
__set($variable_name$variable_value) {
    switch (
$variable_name) {
      case 
'title':
      case 
'page_not_found':
        
$this->settings[$variable_name] = $variable_value;
        break;
      case 
'xhtml_body':
        
$this->settings[$variable_name] .= $variable_value;
        break;
      case 
'stylesheet':
      case 
'javascript':
        if (empty(
$this->settings[$variable_name])) {
          
//First entry, so treat as a string
          
$this->settings[$variable_name] = $variable_value;
        } else {
          
//Not the first entry, so treat as an array
          
if (!is_array($this->settings[$variable_name])) {
            
$this->settings[$variable_name][] = $this->settings[$variable_name];
          }
          
$this->settings[$variable_name][] = $variable_value;
        }
        break;
      case 
'xhtml_header':
        
$this->settings[$variable_name][] = $variable_value;
        break;
    }
  }

We have now structured our application and can begin creating pages! Let's create an index page:

PHP Code:
<?php
global $page_specific_content;

require_once(
'XHTMLPage.php');

$page_specific_content =  new XHTMLPage();

$page_specific_content->title 'Welcome';

$page_specific_content->stylesheet 'stylesheet.css';

$page_specific_content->javascript 'javascript.js';

//$page_specific_content->xhtml_header = '';

$page_specific_content->xhtml_body '<h1>WELCOME</h1>';
$page_specific_content->xhtml_body '<p>I\'m excited to have this page up for you!</p>';

require_once(
'xhtml_template.php');
?>
To create additional pages, we need only duplicate index.php and change the variables. To update the template (to include navigation, for example), we only need to edit the template. We have, in effect, achieved our goals!

I have attached files demonstrating this layout to this post for your reference and use... they're free to all.

Now, when I personally create an application structure, there are generally far more goals for the application and the structure becomes much more sophisticated, but I use the same methodology above in designing the code. The key thing to the success of an application structure is to begin by defining your goals. Much of the rest of the application becomes necessary as a means of fulfilling your goals.
Attached Files
File Type: zip general_site.zip (1.8 KB, 15 views)
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-20-2009, 05:52 PM Re: Where To Put DOCTYPE, etc In PHP Includes
Experienced Talker

Posts: 30
Trades: 0
Thank you Jeremy - have downloaded the layout demonstration files and am in the process of 'digesting' them and the above.
html911rap is offline
Reply With Quote
View Public Profile
 
Old 07-22-2009, 02:44 PM Re: Where To Put DOCTYPE, etc In PHP Includes
Novice Talker

Posts: 7
Name: James Choo
Trades: 0
Thanks for JeremyMiller detailed post
It's a great information for the coding.
__________________
Webmaster Infomation & Solutions
Free Tools & Resources

Please login or register to view this content. Registration is FREE
JTcuy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Where To Put DOCTYPE, etc In PHP Includes
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46611 seconds with 13 queries