-1

The code below works fine, it returns a list of navagation links and gives them unique colors, but why does adding

 '<div title="Home" id="logo" onclick="window.location = \''.SITE_ROOT.'\';"></div>'

give me header output errors?

Removing it makes it work fine. Even <div></div> would give me an error. I know it's because I'm outputting early, but if I remove this <div> there is still a div before it and it doesn't cause any problem.

Hmm, well here is my code:

<?php
function printNavagation($this){
$i = 0; $n = 0; $x = count($this);          
echo '<div class="navagation"><div title="Home" id="logo" onclick="window.location = \''.SITE_ROOT.'\';"></div><ol><li class="libreak">|</li>'; 
while ($i < $x){

    $link[$i] = $this[$i]; // Link is same as name
    // Except theese:
    if ($link[$i] == 'home'){$link[$i] = '';} else if ($link[$i] == 'djs'){$this[$i] = 'DJs'; $link[$i] = 'djs/home';}
    // set onhover colors
    if('1' == $n){$color = 'pink';} else if('2' == $n){$color = 'green';} else if('3' == $n){$color = 'yellow';} else {$color = 'blue';} 

    echo '<li class="'.$color.'"><a href="'.SITE_ROOT.'/'.$link[$i].'">'.ucfirst($this[$i]).'</a></li><li class="libreak">|</li>';  //wrap results
    $i++; $n++; if($n >= 4){$n = 0;} // reset alternating color counter
}
echo'</ol></div>'; 
} 
?>
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • Probably PHP is issuing a warning of some sort, which will not get caught by output buffering. Check your error logs and/or dial up the error reporting/display_errors. it's never a good idea to have those off/suppressed during development anyways. – Marc B Oct 18 '11 at 14:49

1 Answers1

2

The buffer is filling up, and is sent to the server, and out to the client.

Removing that line of code probably makes the output just small enough that the buffer isn't full yet.

Do not output headers after outputting content. While you can reconfigure output buffering to make it work, it is bad practice. Later on, your server will get reconfigured, and you'll wonder why your code is broken.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Brad, my issue now is that I need to output the navagation before the rest of my site? For example, it will run the navagation, then fetch page 'x's contents. Page x may contain functions that use headers, which is why I run into errors. I can't simply output my navagation after, as it would mess up the pshyical layout. What is the best method for outputting my navagation first without filling up the buffer? – Billy Jake O'Connor Oct 18 '11 at 15:04
  • @BillyJakeO'Connor, there actually isn't any `header()` call in your code above. In any case, you should be outputting headers for anything else. If your pages included within something else have `header()` in them, then you are structuring your site in a really odd way. – Brad Oct 18 '11 at 16:36
  • I am currently reworking my site so that any functions that call header requests during output are removed or called beforehand. Thank you. – Billy Jake O'Connor Oct 20 '11 at 09:04