Possible Duplicate:
Headers already sent by PHP
I fixed this problem, but since I don't understand what caused it, I can't be sure it's really fixed.
My PHP site displays the latest activity on the home page, before you log in. I recently modified the logic to include more types of activity. It looked like it worked, but I got the following error upon logging in:
Warning: Cannot modify header information - headers already sent by
(output started at header.php:75)
in index.php on line 26
I think this error message is misleading, because the way I fixed it was by changing "LIMIT 10" to "LIMIT 9" in the MySQL query that gets the activity to be displayed on the home page.
public function getLatestActivity()
{
$sql = "SELECT 'Debate' AS Type, d.Debate_ID AS ID, CONCAT('debate.php?debate_id=', d.Debate_ID) AS URL, d.Title, d.Add_Date
FROM debates d
UNION SELECT 'Discussion' AS Type, d.Discussion_ID AS ID, CONCAT('discussion.php?discussion_id=', d.Discussion_ID) AS URL, d.Title, d.Add_Date
FROM discussions d
UNION SELECT 'Petition' AS Type, p.Petition_ID AS ID, CONCAT('petition.php?petition_id=', p.Petition_ID) AS URL, p.Petition_Title AS Title, p.Add_Date
FROM petitions p
ORDER BY Add_Date DESC
LIMIT 9";
try
{
$stmt = $this->_db->prepare($sql);
$stmt->execute();
$activity = array();
while ($row = $stmt->fetch())
{
$activity[] = '<span style="font-size: x-large"><strong>Latest activity</strong></span><br /><span style="font-size: large">' . $row['Type'] . ': <span style="color: #900"><a href="' . $row['URL'] . '" style="color: #900">' . $row['Title'] . '</a></span></span>';
}
$stmt->closeCursor();
return $activity;
}
catch(PDOException $e)
{
return FALSE;
}
}
And here's what I'm doing with the data returned by that function. It loops through the array and shows a new item every 4 seconds.
<?php $latest_activity = $pdb->getLatestActivity(); ?>
<script type="text/javascript">
var activity = <?php echo json_encode($latest_activity); ?>;
var index = -1;
$(function()
{
getLatestActivity();
});
function getLatestActivity()
{
index = (index + 1) % activity.length;
var div = document.getElementById('divLatestActivity');
if (index < activity.length)
{
div.innerHTML = activity[index];
}
setTimeout("getLatestActivity()", 4000);
}
</script>
Why did changing "LIMIT 10" to "LIMIT 9" fix the "cannot modify header information" problem?