-2

Now a days I am designing a website in php and I want to add more than 5 pages which should have same styles for ex- All the pages should have same header, footer, left side bar, right sidebar etc.

I know that I can do this using include() method but I am unable to use it effectively.

I have created some pages like below.

header.php-

<html><body><div><img src="logo.png"/></div></body></html>

leftsidebar.php

<html><body><div>Categories<ul><li>business</li><li>management</li><ul>div></body></html>

index.php- this is the main file where I wan to include files.

<html>
  <body>
    
    <div class="top">
    
    <?php include("header.php"):
?>
    </div>
    <div class ="leftsidebar">
      <?php include("leftsidebar.php"):
?>
    </div>
    
    </body>
</html>

the problem is that layout is not proper. I dont know where I have to use width and height style proprties for div whether I should use div width in index.php or header.php. And one problem is when I view the source code I can see some duplicate elements like html,head,body etc.

I would appreciate if somebody know how to use this effectively.

Looking forward to your answers.

kittykittybangbang
  • 2,380
  • 4
  • 16
  • 27
RajSharma
  • 1,941
  • 3
  • 21
  • 34
  • 1
    Well there are several ways to include those files. Personally I would use a different approach. Create one index file and use jQuery ajax load function to load the changing parts into a seperate div. That way you only need your header / footer / sidebar once and simply load the other pages in a div if the user requests them. It's also more user friendly as you no longer have to refresh your site everytime. – icecub Jun 05 '15 at 01:04
  • This is why we have an HTML validator. – cimmanon Jun 05 '15 at 01:20

2 Answers2

0

Your include files should not re-open the <html> and <body> tags! Wherever you use the <?php include 'something.php' ?>, imagine what's contained in that included file sitting directly in your code -- because that's all that's happening when you include via PHP.

Additionally, a PHP statement should end with a semi-colon (;), not a colon (:), like this:

<?php include("header.php"); ?>
kittykittybangbang
  • 2,380
  • 4
  • 16
  • 27
0

First point:

you are including php files that contain html and body tags that are also present in the page where you are including them... keep them only in the initial page where you put the includables.

header.php should be:

<div><img src="logo.png"/></div>

leftsidebar.php should be:

<div>Categories<ul><li>business</li><li>management</li></ul></div>

Second point:

to have styles across the different pages you should use css external stylesheets and not inline styles. Have a look here

Daniel Cardoso
  • 478
  • 3
  • 14