Jump to content

Recommended Posts

Ok i have a global layout which works when put in one giant file.

 

Now what i did was put one half of it in one include and the other half in the second include.

 

 

Then i did this:

 

<?php
Include("css/layout1.php");
?>

   
     <center><?php
	Echo 'test';
		 ?>
 </center>

 <p> </p>
   
   

<?php
Include("css/layout2.php");
?>

 

I do this because the center column is the only column that changes and the end of layout 1 has:

 

<div id="centercolumn">
<div id="centercontent">

 

The other layout 2 has at the start:

 

</div>
</div>

 

 

So i am sure you can see what is "suppose" to happen is the php is meant to go withing the center column divs  ..... how ever the two includes do not work .. and the the page is completely white except it does echo "Test".

 

So my includes did not work ...any suggestions?  The CSS works when in one giant file so i think its only php related...

 

 

 

Chances are you don't have the path correct. Includes like this can be tricky as I learned the hard way. Here is my 2 cents. Anytime you use an include, do it like so....

 

<?php 
     include($_SERVER['DOCUMENT_ROOT'].'/start/of/path/to/file');
?>

 

In HTML, you can denote "start at root" by using /  ... e.g

<a href="/lvl1/lvl2/file.php">link</a>

 

using $_SERVER['DOCUMENT_ROOT'] is the "php equivalent" so to speak.

 

You can do the same exact layout thing with 1 file.

 

Here's how..

 

I call it header.php in my pages...

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php if(isset($page_title)){echo $page_title;} ?></title>
</head>

<body>
<div id="mainWrapper">
<div id="column1">static on every page</div>
<div id="colum2"><?php function footer(){ // content that changes will go here  ?></div>
<div id="column3">static on every page</div>


</div>
</body>
</html>
<?php } //end our footer function ?>

 

then on each page the first line of each page you want this file to wrap set it up like this

 

<?php
   $page_title='this pages title'; // set the page title
   include($_SERVER['DOCUMENT_ROOT'].'/header.php');
?>

All your html code here.... the above php snippet will include the file at the top and start the layout... 

then we finish the page by calling the footer function

<?php footer(); ?>

 

I use this method in every site I build... it has worked great for me.. and it saves it so you can edit the layout page in a wysiwyg editor and it don't break because of un-closed tags.

 

Please note that I don't have the proper css in there to make it 3 columns... but I think you get the picture.

 

Hope this helps..

Shouldn't include be lowercased or does this not matter? I think it should matter.

 

It doesn't matter. I always use capital.. and to double check i used non capital and its the same ... ive used capital through out my site.

Shouldn't include be lowercased or does this not matter? I think it should matter.

 

It doesn't matter. I always use capital.. and to double check i used non capital and its the same ... ive used capital through out my site.

Okay just checking :)

 

Can you possibly post the code for the layouts then?

Shouldn't include be lowercased or does this not matter? I think it should matter.

 

It doesn't matter. I always use capital.. and to double check i used non capital and its the same ... ive used capital through out my site.

 

wow that confused me...always thought php as case sensitive

I can give you general structure:

 

Layout 1 . 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">
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="/css/thefile.css" />
</head>

<body>
<div id="top">
  //stuff
</div>
<div id="wrapper">

  <div id="topheader">
   <div id="barholder">
    //stuff
   </div>
    </div>

<div id="midheader">
//stuff
</div>

<div id="botheader">
//stuff
</div>

<div id="leftcolumn">
   <div id="leftcontent">
//stuff

</div>
   </div>

<div id="centercolumn">
   <div id="centercontent">

 

 

 

 

 

 

 

 

Layout 2 . php

 

</div>
</div>


<div id="rightcolumn">
  	<div id="rightcontent">
// stuff 

</div>
</div>

<div id="footer">
   	//stuff
   </div>
</div>
   
</body>
</html>

 

 

 

Shouldn't include be lowercased or does this not matter? I think it should matter.

 

It doesn't matter. I always use capital.. and to double check i used non capital and its the same ... ive used capital through out my site.

 

wow that confused me...always thought php as case sensitive

 

Try it yourself it should still work with capital I and non capital also.

Since when ? Because thats new to me..I been including HTML in php files with them being in PHP tags since the beginning ..with no problems?

 

Almost 80 % of the layout includes are html there are a few echo'd variables in php but are you 100% sure on that.. cos i've used html in php files many times?

Try this:

 

Layout 1 . php

<?php
$page = <<<EOD
<!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></title>
<link rel="stylesheet" type="text/css" href="/css/thefile.css" />
</head>

<body>
<div id="top">
  //stuff
</div>
<div id="wrapper">

  <div id="topheader">
   <div id="barholder">
    //stuff
   </div>
    </div>

<div id="midheader">
//stuff
</div>

<div id="botheader">
//stuff
</div>

<div id="leftcolumn">
   <div id="leftcontent">
//stuff

</div>
   </div>

<div id="centercolumn">
   <div id="centercontent">
EOD;
echo $page;
?>

 

 

 

 

 

 

 

 

Layout 2 . php

 

<?php
$page = <<<EOD
</div>
</div>


<div id="rightcolumn">
  	<div id="rightcontent">
// stuff 

</div>
</div>

<div id="footer">
   	//stuff
   </div>
</div>
   
</body>
</html>
EOD;
echo $page;
?>

 

Remember to actually fill in the //stuff

Try this:

 

Layout 1 . php

<?php
$page = <<<EOD
<!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></title>
<link rel="stylesheet" type="text/css" href="/css/thefile.css" />
</head>

<body>
<div id="top">
  //stuff
</div>
<div id="wrapper">

  <div id="topheader">
   <div id="barholder">
    //stuff
   </div>
    </div>

<div id="midheader">
//stuff
</div>

<div id="botheader">
//stuff
</div>

<div id="leftcolumn">
   <div id="leftcontent">
//stuff

</div>
   </div>

<div id="centercolumn">
   <div id="centercontent">
EOD;
echo $page;
?>

 

 

 

 

 

 

 

 

Layout 2 . php

 

<?php
$page = <<<EOD
</div>
</div>


<div id="rightcolumn">
  	<div id="rightcontent">
// stuff 

</div>
</div>

<div id="footer">
   	//stuff
   </div>
</div>
   
</body>
</html>
EOD;
echo $page;
?>

 

Remember to actually fill in the //stuff

 

Same result. I really don't think it needs to be in php variable and then echo'd though lol :P I will leave it like you said unless some one tells me it is not needed.

 

Its days like this when i hate being a coder lol. Every thing works for everyone else except for myself :P

You're right. But you still need to echo out the HTML. What about this then:

 

Layout 1 . php

<?php
echo '
<!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></title>
<link rel="stylesheet" type="text/css" href="/css/thefile.css" />
</head>

<body>
<div id="top">
  //stuff
</div>
<div id="wrapper">

  <div id="topheader">
   <div id="barholder">
    //stuff
   </div>
    </div>

<div id="midheader">
//stuff
</div>

<div id="botheader">
//stuff
</div>

<div id="leftcolumn">
   <div id="leftcontent">
//stuff

</div>
   </div>

<div id="centercolumn">
   <div id="centercontent">';
?>

 

 

 

 

 

 

 

 

Layout 2 . php

 

<?php
echo '
</div>
</div>


<div id="rightcolumn">
  	<div id="rightcontent">
// stuff 

</div>
</div>

<div id="footer">
   	//stuff
   </div>
</div>
   
</body>
</html>';
?>

You're right. But you still need to echo out the HTML. What about this then:

 

Layout 1 . php

<?php
echo '
<!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></title>
<link rel="stylesheet" type="text/css" href="/css/thefile.css" />
</head>

<body>
<div id="top">
  //stuff
</div>
<div id="wrapper">

  <div id="topheader">
   <div id="barholder">
    //stuff
   </div>
    </div>

<div id="midheader">
//stuff
</div>

<div id="botheader">
//stuff
</div>

<div id="leftcolumn">
   <div id="leftcontent">
//stuff

</div>
   </div>

<div id="centercolumn">
   <div id="centercontent">';
?>

 

 

 

 

 

 

 

 

Layout 2 . php

 

<?php
echo '
</div>
</div>


<div id="rightcolumn">
  	<div id="rightcontent">
// stuff 

</div>
</div>

<div id="footer">
   	//stuff
   </div>
</div>
   
</body>
</html>';
?>

 

Same result again, and i thought you only have to echo html out if its inside <? ?> tags... which my html wasn't originally.

well when i go to:

 

localhost/css/layout1.php

 

it loads so the directory is correct ? Right ?

 

the path will be correct if the file your viewing is in line with the /css/layout1.php path. If the file is in a /other/folder/mainpage.php, the include as you have it is not in the correct path line.

 

thats why I suggested using $_SERVER['DOCUMENT_ROOT'] with the include then it does not matter where the path line is, $_SERVER['DOCUMENT_ROOT'] is kinda like a "start at root" type thing.

 

All this echo'ing html inside php files is un-needed and messy. You can drop in and out of PHP at will. Thats one of the wonderful things of PHP. If it is HTML, don't echo it with php unless you HAVE TO. The way the posts here have been echoing a full block of HTML is not efficient and makes things hard to edit.

 

If echo file_get_contents("css/layout1.php"); did not echo anything out, then either there is an error that is giving a blank page or the layout1.php file does not exist in the specified path.. (see above message).

 

Turn Error reporting on so you can get error messages, and CONFIRM 100% that your include file is set in the right path, keeping in mind PHP and HTML handle paths a little differently.

 

Trust me, I fought this kind of problem on my own for a LONG time before I figured out that $_SERVER['DOCUMENT_ROOT'] pretty well kills the problems.

 

Nate

well when i go to:

 

localhost/css/layout1.php

 

it loads so the directory is correct ? Right ?

 

the path will be correct if the file your viewing is in line with the /css/layout1.php path. If the file is in a /other/folder/mainpage.php, the include as you have it is not in the correct path line.

 

thats why I suggested using $_SERVER['DOCUMENT_ROOT'] with the include then it does not matter where the path line is, $_SERVER['DOCUMENT_ROOT'] is kinda like a "start at root" type thing.

 

All this echo'ing html inside php files is un-needed and messy. You can drop in and out of PHP at will. Thats one of the wonderful things of PHP. If it is HTML, don't echo it with php unless you HAVE TO. The way the posts here have been echoing a full block of HTML is not efficient and makes things hard to edit.

 

If echo file_get_contents("css/layout1.php"); did not echo anything out, then either there is an error that is giving a blank page or the layout1.php file does not exist in the specified path.. (see above message).

 

Turn Error reporting on so you can get error messages, and CONFIRM 100% that your include file is set in the right path, keeping in mind PHP and HTML handle paths a little differently.

 

Trust me, I fought this kind of problem on my own for a LONG time before I figured out that $_SERVER['DOCUMENT_ROOT'] pretty well kills the problems.

 

Nate

 

I did try this ... but it didn't work. As i said when i go to the url that the include has:

localhost/css/layout1.php it loads so the directory is not wrong. But i did try your suggestion but i didnt have additional folders infront of it so i did not see the need?

 

And the output is complete blank and the only thing it echo's is the echo in the php file..as shown in the first post of this thread.

Ok ... I think christer is correct. You are not getting the right file on your include. I run your script and gave me this output.

 

<!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></title>
<link rel="stylesheet" type="text/css" href="/css/thefile.css" />
</head>

<body>
<div id="top">
  //stuff
</div>
<div id="wrapper">

  <div id="topheader">
   <div id="barholder">
    //stuff
   </div>
    </div>

<div id="midheader">
//stuff
</div>

<div id="botheader">
//stuff
</div>

<div id="leftcolumn">
   <div id="leftcontent">
//stuff

</div>
   </div>

<div id="centercolumn">
   <div id="centercontent">
   
     <center>test	</center>

<p> </p>
   
   

</div>
</div>


<div id="rightcolumn">
  	<div id="rightcontent">
// stuff 

</div>
</div>

<div id="footer">
   	//stuff
   </div>
</div>
   
</body>
</html>

going to the url in your browser is not the same as the path in PHP. PHP starts from where the file is not at the root of the server. doing include('/path/to/file.php') does not work either. the / in the front is not read as "start at root".

 

I may be wrong, but most include problems I have found were fixed by correcting the path.

 

get error reporting going and see if you have any errors

 

put  ini_set('error_reporting', E_ALL);  on the page in question and see what you get.

 

Nate

Well if this url :

 

localhost/css/layout1.php

 

Loads... then what should the url be for the include?

 

Nope, thats what I have been saying... the html path to the file is different than the php path you have to define.

 

the path should be include($_SERVER['DOCUMENT_ROOT'].'/css/layout1.php')

 

if your linking to the file in HTML then yes /css/layout1.php will be correct for all files no matter where they live. But in PHP a file in /folder1/folder2/file.php cannot use the same path as a file that lives in the root /file.php   

 

/file.php would find the include calling it the way you did, but /folder1/folder2/file.php will not find the path as it will start looking in /folder1/folder2/<begin looking here>

 

Nate

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.