help in using the php include

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
carm
Forum Newbie
Posts: 5
Joined: Mon Dec 12, 2011 12:14 pm

help in using the php include

Post by carm »

I am wanting to use the "PHP Include" to link a left column file to all the different web pages.
I have checked many forums for the proper code and I still can't get it to work.
I'm new to coding with php, doing many HTML & css.
Any help would be great.
Thanks in advance.

URL's to files
http://www.cowichanmen.org/westcoastmen.org/indexw2.php
URL for php include file
http://www.cowichanmen.org/westcoastmen ... olumn.html


Coding:

<!-- end #header --></div>

<!--lftcolumn -->

<?php include(“www.cowichanmen.org/westcoastmen.org/lftcolumn.php”);?>

<!--lftcolumn -->

<div id="mainContent">

-----------============---------==============----------

Not sure whether I need to include the div's for the sidebar

<!-- end #header --></div>

<div id="sidebar2">
<div id="sidebar1">

<!--lftcolumn -->

<?php include(“www.cowichanmen.org/westcoastmen.org/lftcolumn.php”);?>

<!--lftcolumn -->


<!-- end #sidebar1 --></div>
<!-- end #sidebar2 --></div>


<div id="mainContent">
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: help in using the php include

Post by Celauran »

You can just use a relative path.

Code: Select all

include 'lftcolumn.php';
Your link at the top points to a .html file but the filename referenced in your code examples is a .php file.
carm
Forum Newbie
Posts: 5
Joined: Mon Dec 12, 2011 12:14 pm

Re: help in using the php include

Post by carm »

Actually I had a copy of both, wondering if it made a difference. Still can seem to see it recognizing the file.

Thanks for your help
carm
Forum Newbie
Posts: 5
Joined: Mon Dec 12, 2011 12:14 pm

Re: help in using the php include

Post by carm »

These are the warnings I got.


Warning: Division by zero in /home/cowi5783/public_html/westcoastmen.org/indexw2.php on line 64

Warning: include(“wwwcowichanmenphp”) [function.include]: failed to open stream: No such file or directory in /home/cowi5783/public_html/westcoastmen.org/indexw2.php on line 64

Warning: include() [function.include]: Failed opening '“wwwcowichanmenphp”' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/cowi5783/public_html/westcoastmen.org/indexw2.php on line 64


This last line --> (include_path='.:/usr/lib/php:/usr/local/lib/php')
I'm not sure what this means, but could that be part of the problem if I'm not on a windows server?
Thanks
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: help in using the php include

Post by Celauran »

That last line is the include path; it's where PHP looks for files specified by the include function.

Did you change the include command as per my previous comment? The errors you've posted suggest otherwise.
Amanda1998
Forum Newbie
Posts: 23
Joined: Tue Dec 13, 2011 10:25 am

Re: help in using the php include

Post by Amanda1998 »

Are you including from same server ?... or external source ...?

If you are trying to include a php script from another site, either that the remote server that host the script is not given you permission to do that or that the server where you are does not give you permission for inclusion of external files...

Second, will be my guess(on the external source case), since file inclusion (from external sources) has been denied from almost every host server (security reason)

But as @Celauran said : You can just use a relative path.
Here is my way:

Code: Select all

<?php
//begin of my code
include("path/path/path/need_file_1.php"); 
//body of mycode
include("../path/need_file_2.php");
// rest of the code
?>
carm
Forum Newbie
Posts: 5
Joined: Mon Dec 12, 2011 12:14 pm

Re: help in using the php include

Post by carm »

Thanks Amanda1998 & Celauran, your code works!!! ;-)
So this is what I have:

<!-- end #header --></div>
<!--lftcolumn -->
<?php
//begin of my code
include("lftcolumn.php");
?>
<!--lftcolumn -->
<div id="mainContent">

URL's to files
http://www.cowichanmen.org/westcoastmen.org/indexw2.php
URL for php include file
http://www.cowichanmen.org/westcoastmen ... column.php

----------------==============---------------

Now what is happening is my css for <div id="mainContent"> is not working.
It works for HTML, but when I change it to php and add the inclose function, it seems to break the css for the <div id="mainContent">.

Any thoughts around this would be great.

Thanks for all your help
Chris
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: help in using the php include

Post by Live24x7 »

your css will obviously break..

your indexw2.php page has one <html><body> </body></html>tags.. and so does your column.php
include statements bring the entire content of a file into your script.
by including column.php, you are including the <html><body> tags twice into your page.


to avoid breaking of css, you need to remove anything and everything from column.php that has already been included in the indexw2.php
belstaffleather
Forum Newbie
Posts: 1
Joined: Sat Dec 17, 2011 2:04 am

Re: help in using the php include

Post by belstaffleather »

Thanks for your help


belstaff jacket
carm
Forum Newbie
Posts: 5
Joined: Mon Dec 12, 2011 12:14 pm

Re: help in using the php include

Post by carm »

Thank-you all for your help.
I now have a working/validated W3C webpage.

What I learned from all this is:
Make sure your code is right
<?php
include("lftcolumn.php");
?>

& not

<?php
//begin of my code
include('lftcolumn.php');
?>

Difference " not '

In your include page,
No duplicate code that exists from your main page, including opening tags

<html>
</html>

I did find that by trying to validate it through W3C, I was able to figure out the remaining errors, once you folks got me heading in the right direction.

Again thanks for all your help.
Cheers ;-)


Final URL:

http://www.cowichanmen.org/westcoastmen.org/indexw4.php
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: help in using the php include

Post by Celauran »

carm wrote:What I learned from all this is:
Make sure your code is right
<?php
include("lftcolumn.php");
?>

& not

<?php
//begin of my code
include('lftcolumn.php');
?>

Difference " not '
Sorry, but that's incorrect. You can use single or double quotes in an include.
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: help in using the php include

Post by Vegan »

I use PHP Include extensively

look at the document as a whole, and you will notice many parts are the same across my gaming site for example. http://www.hardcore-games.tk

now the banner and the menu are always present on each page :mrgreen:

so snipped the menu from the index.php and saved it as a PHP file even tough its an HTML snippet

then I used the include function to add the block back into the document

I repeated the process for the bottom copyright and so on

in each include, there is a mix of HTML, JavaScript and PHP but the include files are always PHP simply to avoid problems

after assembly the original document is as it was before I use include

then after the index.php was looking the way I wanted, I rolled out the include to all documents :D
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
Amanda1998
Forum Newbie
Posts: 23
Joined: Tue Dec 13, 2011 10:25 am

Re: help in using the php include

Post by Amanda1998 »

Light web, easy loading... good for @Vegan
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: help in using the php include

Post by Vegan »

thanks
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
Post Reply