I didn't look at the whole thing, but this usually happens when you process the information AFTER you actually display it.
for example:
<?
echo $name;
$name = "juschillin";
?>
first time that somone loads the page, $name won't show.
whereas the script should read:
<?
$name = "juschillin";
echo $name;
?>
Maybe that might help.
I looked like some of your if statements, were below where you actually displayed the results