Click to See Complete Forum and Search --> : echo divs inside of div, with includes


curious_george
01-29-2007, 11:02 AM
Well, what I have is very basic, but since I am not very good at PHP I am having a few problems. Most I have been able to figure out, but this one is giving me a headache. What I have is an HTML page consisting of 5 divs, one container, and the rest inside the container. Each of those divs has a php include in them. Here is a basic example:

<html>
<body>
<div id="container">
<div id="header">
<? include('header.php'); ?>
</div>
<div id="left">
<? include('left.php'); ?>
</div>
<div id="main">
<? include('main.php'); ?>
</div>
<div id="right">
<? include('right.php'); ?>
</div>
</div>
</body>
</html>

Like I said, very basic. Now, the problem is with my PHP in the right and left DIVs. The pages included in those DIVs have code that echos more divs, all of which contain php includes. I have done this before without any problems, but the files included in the inner DIVs were always text, so there was no problem. When I have the page include a page that contains php echo statements (echoing HTML) though, then only the first page gets included. Here is the code for left.php (the code for left and right.php are the same, just switched to access different tables):


<?php
include("dbinfo.inc.php");
mysql_connect(localhost,"$user","$password") or die("Could not connect");
@mysql_select_db($database) or die("The database is currently unavailable. Please try again later!");

$query=" SELECT * FROM site_left WHERE active = 0";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

for($i=0;$i<$num;$i++)
{
$title=mysql_result($result,$i,"title");
$place=mysql_result($result,$i,"place");
$active_left=mysql_result($result,$i,"active");
$path='site/'.$place;
if($active_left==0)
{
if (file_exists($path))
{
echo '<div class="tt">
</div>
<div class="mid">
<font size="5">'.$title.'</font>
</div>
<div class="tb">
</div>
<div class="mid">';
include($path);
echo '</div>
<div class="b">
</div>';
}
}
}
?>


Like I said, if the page saved in $path is all text then it displays all of the files, but when it contains php echo statements (echoing HTML) it only shows the first occurence. I created a few test pages to run through the script and they all had the same problem. Here is a basic test page that I tried to use:

<?php
include("dbinfo.inc.php");
mysql_connect(localhost,"$user","$password") or die("Could not connect");
@mysql_select_db($database) or die("The database is currently unavailable. Please try again later!");

$query=" SELECT * FROM members ORDER BY id DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

if($num<1)
{
die($num);
}

$end=10;

if($num<$end)
$end=$num;

echo "<b><u>10 Newest Members</u></b>";

for($i=0;$i<$end;$i++)
{
$mem=mysql_result($result,$i,"id");
echo "<br>".($i+1).". <a href='member.php?id=".$mem."'>";
$mem=mysql_result($result,$i,"user");
echo $mem."</a>";
}
?>


And then I also tried another page too, so that there would be two pages listed in the site_left DB table accessed in left.php, as shown above. I have yet to find a problem with the syntax, so I am not sure why the for-loop in left.php stops after the first instance, especillay since when I insert 5 text pages into the site_left table all 5 show up. Any ideas? Thanks in advance.

SpectreReturns
01-29-2007, 09:34 PM
I think your problem is that the pages are designed to be run singularly, and not to be part of a group. You're including "dbinfo.inc.php" once in each page, and if this isn't protected against multiple declarations, it'll crash the page. This is probably what's going on. Move all of files which would be included more than once into the header and try again.

NightShift58
01-30-2007, 12:19 AM
You're including "dbinfo.inc.php" once in each page, and if this isn't protected against multiple declarations, it'll crash the page.By the looks of it, dbinfo.inc.php only contains the user name, password and database name. that can be included thousands of times without problems.

One can can question the need to do so but that, in and of itself, will not lead to a page crash.

Even including the file "$path" will not cause a page crash. My guess is that the included is not named with a .php extension and doesn't get parsed by PHP. Double check the names of your generated include name...

curious_george
01-30-2007, 12:52 PM
Thank you for your responses. As for the multiple includes of dbinfo.inc.php, as NightSHift said, the page only contains the username, password, and DB name. AFter posting yesterday I did redo some of the pages, and removed all of the includes to that file, instead using a basic include_once() call to the file in my header. As for the include extensions, all of the paths/filenames have the .php extension, and all of the files exist in the specified locations. Any other ideas?

NightShift58
01-30-2007, 03:53 PM
<?php
include("dbinfo.inc.php");
mysql_connect(localhost,"$user","$password") or die("Could not connect");
@mysql_select_db($database) or die("The database is currently unavailable. Please try again later!");

$end = 10;
$query = " SELECT * FROM members ORDER BY id DESC LIMIT $end";
$result = mysql_query($query) or die("SQL Error: $query<br>" . mysql_error());

IF (mysql_num_rows($result) > 0) :
$i = 0;
WHILE ($row = mysql_fetch_array($result) and ++$i) :
echo "<br>".($i+1).". <a href='member.php?id=" . $row['id'] . "'>" . $row['user'] . "</a>";
ENDWHILE;
ENDIF;
mysql_close();
?>