Script won't display ANYTHING! Why?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
~fleece~
Forum Newbie
Posts: 12
Joined: Sun Apr 04, 2004 10:18 pm

Script won't display ANYTHING! Why?

Post 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;
    }

}
?>
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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
~fleece~
Forum Newbie
Posts: 12
Joined: Sun Apr 04, 2004 10:18 pm

Post 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. :?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

oh, ok that makes sense. So, the loop doesn't run once, or does it run and print everything but the variables?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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--)
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Oh yea - good eye!
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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.
~fleece~
Forum Newbie
Posts: 12
Joined: Sun Apr 04, 2004 10:18 pm

Post 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! :)
~fleece~
Forum Newbie
Posts: 12
Joined: Sun Apr 04, 2004 10:18 pm

Post 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! :)
~fleece~
Forum Newbie
Posts: 12
Joined: Sun Apr 04, 2004 10:18 pm

Post 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
Post Reply