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!
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!
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
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.
<?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;
}
}
?>
<?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. This code *does* make things much simpler. It looks nicer too. I'll try it out. Thanks again!