Can't include a file twice? [SOLVED]

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
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

Can't include a file twice? [SOLVED]

Post 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!
Last edited by charp on Wed Jan 04, 2006 10:16 am, edited 1 time in total.
BruceT
Forum Newbie
Posts: 14
Joined: Sat Aug 27, 2005 10:23 am

Post 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??
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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;
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

Post 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.
Post Reply