Code: Select all
include("header.php");
include("includes/".$_GET[module]."/index.php");
include("footer.php");Is there a better approach to this? What do you do when you include files in this manner?
Moderator: General Moderators
Code: Select all
include("header.php");
include("includes/".$_GET[module]."/index.php");
include("footer.php");I'm not sure it needs to. I think the issue Mr Tech was referring to was that of including the header before including the bulk of the page which includes such controller logic (sessions, redirects etc). Output buffering will help in this case.Kieran Huggins wrote:sadly, output buffering doesn't buffer headers.
Code: Select all
include('controllers/' . $_GET['page'] . '.php');
include('templates/header.php');
include('templates/' . $_GET['page'] . '.php');
include('templates/footer.php');Code: Select all
<table>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
<?php
$query = "select name, email from users";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
echo '<tr><td>';
echo $row['name'];
echo '</td><td>';
echo $row['email'];
echo '</td></tr>';
}
?>
</table>Code: Select all
<?php
$users = array();
$query = "select name, email from users";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$users[] = $row;
}
?>Code: Select all
<table>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
<?php foreach ($users as $user): ?>
<tr>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['email']; ?></td>
</tr>
<?php endforeach; ?>
</table>That is the whole point. Headers are sent and output is held in a string to be echoed later. Doing that is just making PHP templates function the same as templates where parsing/replacement is done. They all share the same idea -- gather output in strings and sent them last.Kieran Huggins wrote:sadly, output buffering doesn't buffer headers.
Did someone say coffee?Kieran Huggins wrote:it just occurred to me that it doesn't matter that headers aren't buffered. The problem lies in the fact that there's output BEFORE some of the headers he wants to send. Ergo, start output buffering before the includes and flush at the end of the script; all headers (including his) will be sent before the output. Bingo!