Page 1 of 1

Can't include a file twice? [SOLVED]

Posted: Wed Jan 04, 2006 12:14 am
by charp
I believe this should work, but for the life of me, I cannot see the problem. When I try to include the same file twice, the parsing stops at the inclusion of the second file. In other words, the contents of the page are normal right up to the end of the first included file and then it's just blank after that -- no more html, not even the closing body and html tags.

Here's the essential code where I wish to include a file twice (actually, I want to include two different files that are nearly clones of one another -- I get the same results trying to include these two files in any combination):

Code: Select all

<hr />
<?php
include ('headline_news.php');
?>
<hr />
<?php
include ('headline_news.php'); 
?>
<hr />
The horizontal rules are there just to help me spot things.

Now here's the code for the included file (and I feel the problem must be in here someplace):

Code: Select all

<?php
$configfile = "news_config.php";
include $configfile;

//=========================================
// FUNCTION: ROW COLOR
//=========================================

function row_color($i, $nrowcolor1, $nrowcolor2) {
  $bgcolor1 = $nrowcolor1;
  $bgcolor2 = $nrowcolor2;
  if ( ($i % 2) == 0 ) {
    return $bgcolor1;
  } else {
    return $bgcolor2;
  }
}// END ROW COLOR FUNCTION

echo '<table width="'.$ntable.'" border="'.$nborder.'" cellspacing="'.$ncellspacing.'" cellpadding="'.$ncellpadding.'">'."\n";

$results = @ mysql_query("SELECT * FROM $table WHERE type=1 ORDER BY post_date DESC",$db);

if ($results) {
$i=0;
while ($therow = mysql_fetch_array($results)) {
$color = row_color($i, $nrowcolor1, $nrowcolor2);
$id = $therow["id"];
$post_date = date("m/d", $therow["post_date"]);
$title = stripslashes($therow["title"]);
$shortver = stripslashes($therow["shortver"]);
$longver = stripslashes($therow["longver"]);

echo '
<tr valign="top" bgcolor="'. $color .'">
<td width="1%" class="'.$ndate.'">'.$post_date.'</td>
<td class="'.$nitem.'"><b>'.$title.': </b>'.$shortver;

	if (($longver)!="") {
		echo '&nbsp;<a class="'.$nlink.'" href="news-events_single.php?id='.$id.'">'.$nmorelink.'</a>';
	}

echo '</td></tr>'."\n";
$i++;
}
} else {
	echo '<tr><td class="'.$nnone.'">'.$nnoposts.'</td></tr>';
}

echo "</table>\n";
?>
Can anyone please help? Thanks in advance!

Posted: Wed Jan 04, 2006 1:00 am
by BruceT
is it because you have a function declaration in the included chunk, so once the inclusions happen, you have a redundant function def??

Posted: Wed Jan 04, 2006 5:13 am
by twigletmac
As BruceT noted, you can't declare a function twice so that is causing your script to fail on the second inclusion. IMHO it is much better to put your functions into a separate file so that you can include it separately from the rest of the code.

Mac

Posted: Wed Jan 04, 2006 6:40 am
by Chris Corbyn
Oh yes... this is good practise to keep class definitions, interfaces and functions in separate files. It helps prevent such collisions and also allows for reusability.

include_once() is intended to preventing this sort of problem but obviously it wont work here since you're deliberately including the same file twice.

Posted: Wed Jan 04, 2006 9:49 am
by pickle
Fortunately, you don't need to declare that function - you can do all that functionality in one line:

Code: Select all

//replace
$color = row_color($i, $nrowcolor1, $nrowcolor2);
//and the whole "row_colour()" function, with...


//this
$colour = ($color == $nrowcolor1) ? $nrowcolor2 : $nrowcolor1;

Posted: Wed Jan 04, 2006 10:15 am
by charp
This forum never ceases to amaze me. Whether I'm posting a problem, searching for answers, or just browsing, I am guaranteed to learn something new from the many knowledgeable members of this forum.

Indeed, one cannot include the same function twice. I would never have considered that possibility on my own. Thank you to BruceT, twigletmac, and d11wtq -- you guys are awesome. And pickle, thank you for the simplified code to replace my function. It's such a kick to see something so simple and elegant.