Page 1 of 1

Script won't display ANYTHING! Why?

Posted: Wed Apr 07, 2004 5:46 am
by ~fleece~
I'm making a blog script that has an add and view comments feature in it. I am able to add comments just fine (I checked the txt file) but when I want to view them, all I get is a blank page! There aren't even any error messages! I don't understand... :? Can anyone help me please? Here is the code for the view comments feature. Thanks! :)

Code: Select all

<?php
$comFile = "comments.txt";

$fpCom = fopen($comFile,"r");
  $comData = fread($fpCom, filesize($comFile));
fclose($fpCom);

$lineCom = explode("\n", $comData);
$i = count($lineCom);

for ($n=i-2; $n > -1; $n--)
 {
  $comment = explode("|", $lineCom[$n]);

  echo <<<END
   <h3>Date: $comment[0]</h3>
   <h3>Name: $comment[1]</h3>
   <h3>Message:</h3> <p>$comment[2]</p>
END;
  if ($comment[3]!="")
    {
     echo <<<END
      <h3>Site:</h3>
      <p>$comment[3]</p>
END;
    }
   if ($comment[5]!="hide")
    {
     echo <<<END
      <h3>E-mail:</h3>
      <p>$comment[4]</p>
      <br/>
END;
    }

}
?>

Posted: Wed Apr 07, 2004 7:15 am
by magicrobotmonkey
why do you do your for loop all weird? I'd check and make sure that $i is returning higher than a 1 or else the for won;t run. Also, are you sure this line works: $lineCom = explode("\n", $comData);
echo some variables and make sure its all working like you think its working

Posted: Wed Apr 07, 2004 8:19 am
by ~fleece~
magicrobotmonkey wrote:why do you do your for loop all weird? I'd check and make sure that $i is returning higher than a 1 or else the for won;t run. Also, are you sure this line works: $lineCom = explode("\n", $comData);
echo some variables and make sure its all working like you think its working
I wrote the for loop that way so that it would echo the last array values first. The $i-2 is there coz if I just write $n=$i for the loops in my other codes, a couple of blank entries show up. They disappear if I start at $i-2.

I've echoed the values of $i, $lineCom[$n]. I've also tried echoing other values and they all seem to work fine until I put them all together or when I place them in a loop. But that's only for *this* file and not for all my previous codes. That's why it's really confusing me. :?

Posted: Wed Apr 07, 2004 8:30 am
by magicrobotmonkey
oh, ok that makes sense. So, the loop doesn't run once, or does it run and print everything but the variables?

Posted: Wed Apr 07, 2004 9:01 am
by twigletmac
I think you could probably simplify your code if you used file(), try this code:

Code: Select all

<?php
$filename = 'comments.txt';

// Get the contents of the file into an array - each line goes in
// a separate array element
$file_contents = file($filename);

// Reorder the array so that the last entry is first in the array
$file_contents = array_reverse($file_contents);

// Now loop through the array and print out each comment
foreach ($file_contents as $line) {
	$comment = explode('|', $line);

	echo <<<END

<h3>Date: $comment[0]</h3>
<h3>Name: $comment[1]</h3>
<h3>Message:</h3> <p>$comment[2]</p>
END;
	
	if (!empty($comment[3])) {
		echo <<<END

<h3>Site:</h3>
<p>$comment[3]</p>
END;
	}

	if ($comment[5] != 'hide') {
		echo <<<END

<h3>E-mail:</h3>
<p>$comment[4]</p>
<br/>
END;
	}
}

?>
Mac

Posted: Wed Apr 07, 2004 10:27 am
by d3ad1ysp0rk
change this:

Code: Select all

for ($n=i-2; $n > -1; $n--)
to this:

Code: Select all

for ($n=$i-2; $n > -1; $n--)

Posted: Wed Apr 07, 2004 10:41 am
by magicrobotmonkey
Oh yea - good eye!

Posted: Wed Apr 07, 2004 10:50 am
by d3ad1ysp0rk
Also, I've always been a fan of heredoc myself, but in your case, simple echo's would do fine, and would also make the code look nicer.

Posted: Thu Apr 08, 2004 10:17 am
by ~fleece~
LiLpunkSkateR wrote:change this:

Code: Select all

for ($n=i-2; $n > -1; $n--)
to this:

Code: Select all

for ($n=$i-2; $n > -1; $n--)
Oh wow, yeah. :lol: I can actually believe I missed that though. :oops:

It works fine now. :D Thanks a bunch! :)

Posted: Thu Apr 08, 2004 10:20 am
by ~fleece~
twigletmac wrote:I think you could probably simplify your code if you used file(), try this code:

Code: Select all

<?php
$filename = 'comments.txt';

// Get the contents of the file into an array - each line goes in
// a separate array element
$file_contents = file($filename);

// Reorder the array so that the last entry is first in the array
$file_contents = array_reverse($file_contents);

// Now loop through the array and print out each comment
foreach ($file_contents as $line) {
	$comment = explode('|', $line);

	echo <<<END

<h3>Date: $comment[0]</h3>
<h3>Name: $comment[1]</h3>
<h3>Message:</h3> <p>$comment[2]</p>
END;
	
	if (!empty($comment[3])) {
		echo <<<END

<h3>Site:</h3>
<p>$comment[3]</p>
END;
	}

	if ($comment[5] != 'hide') {
		echo <<<END

<h3>E-mail:</h3>
<p>$comment[4]</p>
<br/>
END;
	}
}

?>
Mac
Hey thanks. :D This code *does* make things much simpler. It looks nicer too. :D I'll try it out. Thanks again! :)

Posted: Thu Apr 08, 2004 10:21 am
by ~fleece~
magicrobotmonkey wrote:oh, ok that makes sense. So, the loop doesn't run once, or does it run and print everything but the variables?
Hey, thanks for replying as well. :) I appreciate it. :D