View Full Version : incudes
elbyweb
16-04-2006, 09:41/09:41AM
hello guys
im trying to insert a .php file into a php/database driven website.
im putting the code into the theme.inc file
<? require("filename.php"); ?>
but its not doing anything at all, the file its inserted into is in html format.
how can i get this to work? Just started to learn php cant wait to move onto some hardcore scripting
Blue
16-04-2006, 11:01/11:01AM
What file extension does the file you are trying to include the php into have?
.html?
.php?
I would suggest using .php file extensions.
Then use this code:
<?php include("filename.php"); ?>
elbyweb
16-04-2006, 13:23/01:23PM
the pages are mainly .php
the actual file is a ".inc" its then included in the output html.
/public_html/theme/theme-name/theme.inc
is the file im inserting the php code
i have made the mods to the code and still no joy. it should be showing up by the rss feed button on every page
JohnC
16-04-2006, 13:41/01:41PM
I might be reading what you are doing wrong, but if you are including a file into a page that is going to be an include itself this could be an issue.
I am not sure about .php, but I have run across problems trying to include files into files that are to be included into other pages.
1.inc --> 2.inc --> page.shtml (.php etc.)
1.inc doesnt display properly.
I hope that makes sense. If that is not what you are doing, please ignore ... hehe
Blue
16-04-2006, 13:50/01:50PM
Also, make sure the path is correct, in the include, like so:
<?php include("path/to/filename.php"); ?>
elbyweb
16-04-2006, 13:50/01:50PM
Originally posted by JohnC
I might be reading what you are doing wrong, but if you are including a file into a page that is going to be an include itself this could be an issue.
I am not sure about .php, but I have run across problems trying to include files into files that are to be included into other pages.
1.inc --> 2.inc --> page.shtml (.php etc.)
1.inc doesnt display properly.
I hope that makes sense. If that is not what you are doing, please ignore ... hehe
you got it bang on! john
now (u know what im going to say :p ) how do i get around it? should i just put the coding in "filename.php" in theme.inc rather than using a include?
elbyweb
16-04-2006, 13:54/01:54PM
Originally posted by Blue
Also, make sure the path is correct, in the include, like so:
<?php include("path/to/filename.php"); ?>
tryed that but dint work :(
<?php include("home/seo/public_html/didyouknow/filename.php"); ?>
also tryed adding a "/" before the home path
JohnC
16-04-2006, 14:14/02:14PM
Originally posted by elbyweb
should i just put the coding in "filename.php" in theme.inc rather than using a include? thats one way, I have also done ...
1.inc --> page.
2.inc --> page.
I dont know if there is a better option in PHP. Hopefully some of the great coders who read here will chime in on that one . :)
g1smd
16-04-2006, 14:24/02:24PM
Single quotes, and web path (not server path) to file:
<?php include('/didyouknow/filename.php'); ?>
elbyweb
16-04-2006, 14:35/02:35PM
Originally posted by g1smd
Single quotes, and web path (not server path) to file:
<?php include('/didyouknow/filename.php'); ?>
nope sorry,
i looked at the output and it looks like the php code isnt going through the script engine thingy
Blue
16-04-2006, 15:04/03:04PM
Did you read this (http://us2.php.net/include/)?
WebSavvy
17-04-2006, 07:14/07:14AM
If the file has a .inc.php extension you have to include the whole file name.
Try this:
<?php
@include("{$_SERVER['DOCUMENT_ROOT']}/didyouknow/filename.inc.php");
?>
If the .inc.php file has open and close php tags (e.g., <?php ?> ) try removing them. Sometimes the file they're included in already has both open and close tags and some programmers write the file w/o the second set in the .inc.php file as it'd be unneeded.
elbyweb
17-04-2006, 16:24/04:24PM
thank you for your time deb, the code im using now is
<?php
@include("{$_server_name['/home/USERNAME/public_html']}/didyouknow/FILENAME.php");
?>
but its not including the "FILENAME.php" and yes it is going into a .inc.php file name.
WebSavvy
17-04-2006, 17:10/05:10PM
Try using the code I suggested in my previous post.
If it's still not working, go to the folder that the /didyouknow/filename.php file resides in, and create a test file.
Name the test file test.php and at the top of the file add the following:
<?php
$mypath = $_SERVER['DOCUMENT_ROOT'];
$location = $_SERVER['REQUEST_URI'];
echo "$mypath \n";
echo "$location \n";
?>
The first line ($mypath) will tell you the root file path.
The second line ($location) will tell you the physical location of the file.
Look at what those lines return as it may help you to determine where you're going wrong.
HTH
WebSavvy
17-04-2006, 17:21/05:21PM
Originally posted by elbyweb now (u know what im going to say :p ) how do i get around it? should i just put the coding in "filename.php" in theme.inc rather than using a include?
OK, rereading this, I think I see your problem. :)
The theme.inc file is what needs to be included into the "filename.php" file -- not the other way around. I hadn't realized that that's what you were doing.
The include should be done like this:
In filename.php add the codes to include theme.inc
Here's the code you'd need to use inside filename.php:
<?php
@include("{$_SERVER['DOCUMENT_ROOT']}/didyouknow/theme.inc");
?>
Now try it, and let me know if it works or not?
elbyweb
18-04-2006, 04:53/04:53AM
i think im going to fully explain what im doing, might help a bit :)
i wanted to ad random html to certain pages to advertise my other pages,
the script (which includes ip tracking) is in /didyouknow/ and im trying to pull in the file that has the finished html "random.php"
the theme.inc.php file is the layout file for the Content Managment script.
so with that in mind what should i do?
elbyweb
18-04-2006, 04:55/04:55AM
[PHP:--------------------------------------------------------------------------------
<?php
$mypath = $_SERVER['DOCUMENT_ROOT'];
$location = $_SERVER['REQUEST_URI'];
echo "$mypath \n";
echo "$location \n";
?>
--------------------------------------------------------------------------------
QUOTE]
thanks for the lil php script, hope you dont mind me using it, its going to save me time and impress my tutor lol
WebSavvy
18-04-2006, 05:23/05:23AM
OK, rather than trying to get someone else's dippy script to work, why not do this?
Create a table in your MySQL db called rotate
In the table called rotate add the following fields:
id (int 22 / primary / auto_increment)
url (text / not null)
title (varchar 255 / not null)
content (varchar 255 / not null)
The id field will be used to identify each item as unique.
The title field is where you'll insert the title of the other site.
The url field is where you'll insert the url of your other site.
The content field is where you'll insert the content snippet you want displayed.
Create an entry for each site you want listed in the random script.
Create a file called random.php
Connect to your db then ...
Add your SQL query
$sql = "SELECT id, title, url, content FROM table ORDER BY RANDOM() LIMIT 1";
$sql_result = mysql_query($sql , $connection) or die ("Couldn't execute query.");
while ($row = mysql_fetch_array($sql_result))
{
$id = $row["id"];
$title = $row["title"];
$url = $row["url"];
$content = $row["content"];
echo <<<EOF
<p><a href="$url">$title</a><br>
$content</p>
EOF;
} // close db while loop
Now, call random.php as an include into your pages and it should display each entry from your rotate table in your db one at a time randomly upon page load/refresh.
Let me know if it solves your problem?
elbyweb
18-04-2006, 08:32/08:32AM
thanks deb,
it is a very dippy script lol
i used phpadmin for the first time and made the database "rotate " and the tables within
i made the "random.php" and sorted out the conection on the second line of the mysql comand i get this error.
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/seo/public_html/random.php on line 11
Couldn't execute query.
i am working to fix it myself (learning what MYsql as i go :) ) just thought if its a quick fix, what the point in doing loads of reasearch.
thanks so much deb your a star!! :cheers:
WebSavvy
18-04-2006, 11:07/11:07AM
You need to make sure you've added a user & password for the db.
Use the MySQL section of CPanel and add a user/pass.
Then, there's a drop menu to add that newly created user to the db to give permissions to that user for the db. Select that user, and add them to the db.
When mysql tables are accessed it uses your root user as the prefix to the db.
So, if your path from root was something like:
/home/elbyweb/public_html/
Your mysql db would use elbyweb_ as the db prefix.
So, if your db is named www it becomes elbyweb_www
The user you create for the db would become elbyweb_user
(replacing user with whatever username you've created for use in that db).
So, now from start to finish in random.php we'd have all of this:
<?php
$connection = @mysql_connect("localhost" , "elbyweb_user" , "pass") or die (" Could not connect to the server. ");
$db = @mysql_select_db("elbyweb_db" , $connection) or die (" Could not select database. ");
$sql = "SELECT id, title, url, content FROM rotate ORDER BY RANDOM() LIMIT 1";
$sql_result = mysql_query($sql , $connection) or die ("Couldn't execute query.");
while ($row = mysql_fetch_array($sql_result))
{
$id = $row["id"];
$title = $row["title"];
$url = $row["url"];
$content = $row["content"];
echo <<<EOF
<p><a href="$url">$title</a><br>
$content</p>
EOF;
} // close db while loop
?>
Change from above "elbyweb_user" to reflect your root path user, and db user that you created.
Change from above "elbyweb_db" to reflect your root path user, and the db name you've created (e.g., www from our example).
Change from above "pass" to reflect the password you've assigned to the elbyweb_user in the elbyweb_db where the table rotate lives.
Create a folder on your site in the root, that you'll call /inc/ and place random.php there.
Then in your pages, call random.php into the page as follows:
<?php
@include("{$_SERVER['DOCUMENT_ROOT']}/inc/random.php");
?>
This should work. Let me know if there's any more issues?
WebSavvy
18-04-2006, 11:23/11:23AM
Just reread your post ... you made 1 db and created 1 table in it called rotate, correct?
If so, then this is correct. If not, then please explain what you did do?
In the 1 db there is 1 table called rotate.
In the table called rotate there are 4 fields called:
[list=1]
id
title
url
content
[/list=1]
elbyweb
18-04-2006, 18:14/06:14PM
yes thats 100% correct. im going to try that bit of code now. thanks again
WebSavvy
03-05-2006, 23:03/11:03PM
Hey elbyweb, were you able to get this working? :)
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.