include function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
david_yg
Forum Newbie
Posts: 20
Joined: Tue Aug 09, 2011 10:19 am

include function

Post by david_yg »

Hi,

I have this codes:

(index.html)

Code: Select all


 <body>  
    
         <div id="wrapper">  
    
<?php include('includes/header.php'); ?> 
    
     <div id="nav"> 
    
     <a href="#">Home</a>  
     <a href="#">About</a>  
     <a href="#">Portfolio</a>  
     <a href="#">Contact</a>  
    
  </div> <!-- end #nav -->


I wonder why
<?php include('includes/header.php'); ?> does not works. In other words, the codes that I have place in header.php does not appears in index.html when I run it in IE.

includes/header.php

Code: Select all


<div id="header">  
    
     <h2>1stWebDesigner PHP Template</h2>  
    
</div>

What could be wrong?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: include function

Post by Christopher »

Do you get an error? The error could be displayed or written to the web server's log.
(#10850)
david_yg
Forum Newbie
Posts: 20
Joined: Tue Aug 09, 2011 10:19 am

Re: include function

Post by david_yg »

no errors. It's just after I cut and paste the codes from index.html and move it header.php and writes: <?php include('includes/header.php'); ?> , the header disappeared.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: include function

Post by phphelpme »

You stated above that you have a page called index.html so how do you expect to run PHP code from a HTML format document?

What you need to do is change the format of your index.html page to index.php first and then you will be able to use php attributes.

Also, can you view php files on your remote server or localhost etc? Is php setup and running etc?

Best wishes
david_yg
Forum Newbie
Posts: 20
Joined: Tue Aug 09, 2011 10:19 am

Re: include function

Post by david_yg »

I change index.html to index.php

The following errors appears:

Warning: include(../includes/header.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\php_template\index.php on line 29

Warning: include() [function.include]: Failed opening '../includes/header.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\php_template\index.php on line 29

Warning: include(../includes/nav.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\php_template\index.php on line 31

Warning: include() [function.include]: Failed opening '../includes/nav.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\php_template\index.php on line 31

strange I have the file path correctly but the errors still appears
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: include function

Post by phphelpme »

How exactly are you coding your include directory link as sometimes Xampp does not like it to have ./ or / before your folder name. So take out those if you have them first.

Best wishes
david_yg
Forum Newbie
Posts: 20
Joined: Tue Aug 09, 2011 10:19 am

Re: include function

Post by david_yg »

I took off the "../" now this is another error:

Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\php_template\includes\header.php on line 4

header.php

Code: Select all


<div id="header">  
    
     <h2>1stWebDesigner PHP Template</h2>  
    
</div>

line 4 is: <h2>1stWebDesigner PHP Template</h2>
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: include function

Post by phphelpme »

I am just guessing here all the time... lol

You need to post all your code so I can see exactly what you have and are working with.

I want to help but I am a little confused.

The error codes above mention a nav.php yet you have not stated anything about his file in your code.

Also, what folder is your index.php file in and where is your include folder compared to your folder with the index.php file in?

Is it in php_template directory and you have an includes folder in that because you surely would not be telling the server to look back a folder which would take it into the htdocs folder where your php_template directory sits.

../ takes it back one. So it is confusing to see your code say one thing but your error say another. lol

Best wishes
david_yg
Forum Newbie
Posts: 20
Joined: Tue Aug 09, 2011 10:19 am

Re: include function

Post by david_yg »

Do I need to post the nav.php codes? since the error only pointing to the header.php line 4

Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\php_template\includes\header.php on line 4


my index.php

Code: Select all

    
 <html>
 
 <head>  
    
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />  
    
 <meta name="description" content="" />  
    
 <meta name="keywords" content="" />  
    
 <meta name="author" content="" />  
    
  <link rel="stylesheet" type="text/css" href="style.css" media="screen" />  
    
 <title>1stWebDesigner PHP Template</title>  
    
 </head>  
        
     <body>  

   
         <div id="wrapper">  
    
<?php

include('includes/header.php'); 

include('includes/nav.php'); 


?>      
    
    
 <div id="content">  
    
 <h1>Heading1</h1>  
 <h2>Heading2</h2>  
 <h3>Heading3</h3>  
 <h4>Heading4</h4>  
 <h5>Heading5</h5>  
    
 <h3>Paragraph Element</h3>  
    
 <p>  
    
 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.  
    
 </p>  
    
 <p>  
    
 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.  
    
 </p>  
    
 <h3>Another Heading Starting Point</h3>  
    
 <p>  
    
 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.  
    
 </p>  
    
 <p>  
    
 Quisque pellentesque sodales aliquam. Morbi mollis neque eget arcu egestas non ultrices neque volutpat. Nam at nunc lectus, id vulputate purus. In et turpis ac mauris viverra iaculis. Cras sed elit a purus ultrices iaculis eget sit amet dolor. Praesent ac libero dolor, id viverra libero. Mauris aliquam nibh vitae eros sodales fermentum. Fusce cursus est varius ante vehicula eget ultrices felis eleifend. Nunc pharetra rutrum nibh et lobortis. Morbi vitae venenatis velit.  
    
 </p>  

 </div> <!-- end #content --> 
    
 <div id="sidebar">  
    
 <h3>Navigation</h3>  
     <li><a href="#">Home</a></li>  
     <li><a href="#">About Us</a></li>  
     <li><a href="#">Links</a></li>  
     <li><a href="#">Portfolio</a></li>  
     <li><a href="#">Contact</a></li>  
    
 <h3>Box Two</h3>  
     <li><a href="#">Link Here</a></li>  
     <li><a href="#">Link Here</a></li>  
     <li><a href="#">Link Here</a></li>  
     <li><a href="#">Link Here</a></li>  
     <li><a href="#">Link Here</a></li>  
    
 <h3>Box Three</h3>  
     <li><a href="#">Link Here</a></li>  
     <li><a href="#">Link Here</a></li>  
     <li><a href="#">Link Here</a></li>  
     <li><a href="#">Link Here</a></li>  
     <li><a href="#">Link Here</a></li>  
    
 </div> <!-- end #sidebar --> 
    
 <div id="footer">  
     <p>Copyright &copy 2010 <a href="http://www.1stwebdesigner.com">1stWebDesigner.com PHP Template</a></p>  
 </div> <!-- end #footer --> 
    
         </div> <!-- End #wrapper --> 
    
     </body>  
     
</html>    

the file directory structure:

C:\xampp\htdocs\php_template\index.php
C:\xampp\htdocs\php_template\includes\header.php


my header.php

Code: Select all



<?php

<div id="header">  
    
     <h2>1stWebDesigner PHP Template</h2>  
    
</div>
  
?>

phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: include function

Post by phphelpme »

Well, your header.php file has php openers and closers yet you use standard HTML in your .php header file.

Your header.php file should be this surely:

Code: Select all

<?php
echo "<div id=\"header\"><h2>1stWebDesigner PHP Template</h2></div>"; 
?>
Or at least this:

Code: Select all

<?php
?>
<div id="header">     
     <h2>1stWebDesigner PHP Template</h2>  
</div>
<?php
?>
If you are going to use something like <div id="header"> in a php statement then you need to either do this: <div id='header'> or this: echo "<div id=\"header\">";

You need to make sure you backslash your quotation marks otherwise you end your statement and start it again which makes the text in the middle of your quotation marks bring up an error as its not a true php statement.

So its either:

Code: Select all

<?php ?><div id="header"> <?php // without being wrapped in php open/close
?>
or this:

Code: Select all

<?php echo "<div id='header'>"; // Single quote marks 
?>
or this:

Code: Select all

<?php echo "<div id=\"header\">"; // added backslash before your html double quotes
?>
Best wishes
Post Reply