An include within an 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
SoreE yes
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 3:59 am

An include within an include

Post by SoreE yes »

I am new to php and have just designed a website which works on my local server, but brings up an error, once uploaded to a remote hosting service. Upon speaking with them, they say there must be something wrong with my code, and will only fix it if I come up with bags of dosh.

I have upgraded to the latest versions of php, mysql and Apache, hoping to bring my local server into synch with theirs, but whilst throwing up some changes to the appearance of the site, it has not resolved this issue. So what can be done to make my local server replicate the errors the remote server is bringing up, as debugging errors from a remote server is a pain? As I said, I am new to this, so please forgive if I am missing the obvious.

Upon some testing, it appears that the error is caused by using an include within an include. The following script is itself an include file:

Code: Select all

<?
 if(isset($_POST['Show_All_Articles']))
{ 
 include ("RightColumnListingsArticles.php");
}

else if(isset($_POST['Show_All_Recommendations']))


This brings up the page
 {
include ("RightColumnListingsRecommendations.php"); 
}

else {


 /*List Articles*/
$CategoriesID= $_POST['CategoriesID'];
$query2 = "SELECT categories FROM categories WHERE categoriesID=$cc";
$result2=mysql_query($query2) or die(mysql_error()); 
while ($record=mysql_fetch_array($result2)) {
	$category=$record['categories'];
   	}
echo "<h3>Articles</h3>";
 $query = "SELECT  title, description, website, articleshelptipsID, priority
          FROM articleshelptips WHERE CategoryID=$cc
          ORDER by priority"; 
		  
$result=mysql_query($query) or die(mysql_error());



/*List a limited number of rows*/
$num_rows=mysql_num_rows($result);
for ($i = 0; $i <= 3; $i++) 
{
$row=mysql_fetch_assoc($result);

/*echo '<a href=http://'.$row["website"].' target="_blank">'.$row['title'].'</a>';*/

echo ("<a href=http://".$row['website'].' target="_blank">'.$row["title"]."</a>");
	
   echo "</br></br>";
}
?>



<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="Show_All_Articles"   src="../Graphics/buttonrightarrow.gif" value="More" />



</form>

<?
/*List Recommendations*/
echo "<h3>Recommendations</h3>";
$query2 = "SELECT categories FROM categories WHERE categoriesID=$cc";
$result2=mysql_query($query2) or die(mysql_error()); 
while ($record=mysql_fetch_array($result2)) {
	$category=$record['categories'];
   	}
 $query = "SELECT  Title, Recommendation, Name, ReadersCommentsID, CompanyName, DateCreated, Approved
          FROM readerscomments WHERE CategoryID=$cc AND Approved='Yes'
          ORDER by DateCreated DESC"; 
		  
$result=mysql_query($query) or die(mysql_error());

/*List a limited number of rows*/
$num_rows=mysql_num_rows($result);
for ($i = 0; $i <= 3; $i++) 
{
$row=mysql_fetch_assoc($result);
echo '<font color="green">'.$row['Title'].'</font>';
   echo "</br></br>";
}
if ($num_rows == 0){echo "There are no recommendations, so far, for this cateogory. Why not write one? Simply choose 'recommend' from the menu panel above and complete.";}



?> 
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" value="More" name="Show_All_Recommendations"><br />
</form>

<? } ?>
This brings up the 'Show all articles file', but when submitted brings up the following error

Code: Select all

Warning: Failed opening 'RightColumnListingsArticles.php' for inclusion (include_path='') in /home/.sites/120/site35/web/Templates/InitialRightColumnListing.php on line 7
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

include ("RightColumnListingsArticles.php");
relative paths are relative to the current working directory. The cwd is initially set to the "main" script's directory when php starts processing, but include() does not change the cwd. Check the cwd via

Code: Select all

echo '<p>current working directory: ', getcwd(), "</p>\n";

if(isset($_POST['Show_All_Articles']))
{
 include ("RightColumnListingsArticles.php");
}
SoreE yes
Forum Commoner
Posts: 32
Joined: Wed Oct 11, 2006 3:59 am

Solved

Post by SoreE yes »

Many Thanks Volka, problem solved. And thanks for including that small piece of coding to check current directory.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

check out __FILE__ as well, which will give you the absolute path to the included file you're calling it from:

http://ca3.php.net/manual/en/language.c ... efined.php

Something like include(dirname(__FILE__).'./some_other_relative_include.php'); might be what you're looking for.. maybe make it a function like "include_rel()"
Post Reply