displaying comments

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
misfitxnet
Forum Newbie
Posts: 14
Joined: Mon May 11, 2009 8:40 am

displaying comments

Post by misfitxnet »

Does anyone know a good way to display comments, posted using a form into a text file, one by one in table rows?
I have figured out how to display all the comments at once, but how would I seperate each comment
into it's own table cell as each comment is posted?
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: displaying comments

Post by ldougherty »

Use the file() function

http://us3.php.net/manual/en/function.file.php

file; will reads entire file line by line into an array

Once you have the array generated you can loop through the array using a for each statement, then create a table with each table row as a new line from the array.

Hope that helps.
misfitxnet
Forum Newbie
Posts: 14
Joined: Mon May 11, 2009 8:40 am

Re: displaying comments

Post by misfitxnet »

Is there anyway to create new table rows for each new line?
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: displaying comments

Post by ldougherty »

very simple example which will create 10 new lines reading through an array.

Code: Select all

 
<?php
echo "<table border=1>";
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
foreach ($arr as &$value) {
    echo "<tr><td>$value</td></tr>";
}
echo "</table>";
 
http://us2.php.net/manual/en/control-st ... oreach.php
Last edited by Benjamin on Tue May 12, 2009 7:44 pm, edited 1 time in total.
Reason: Changed code type from text to php.
misfitxnet
Forum Newbie
Posts: 14
Joined: Mon May 11, 2009 8:40 am

Re: displaying comments

Post by misfitxnet »

where can I learn about "For each" statements?
misfitxnet
Forum Newbie
Posts: 14
Joined: Mon May 11, 2009 8:40 am

Re: displaying comments

Post by misfitxnet »

Is it possible to do this with a list() = split() array?

ldougherty wrote:very simple example which will create 10 new lines reading through an array.

Code: Select all

 
<?php
echo "<table border=1>";
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
foreach ($arr as &$value) {
    echo "<tr><td>$value</td></tr>";
}
echo "</table>";
 
http://us2.php.net/manual/en/control-st ... oreach.php
misfitxnet
Forum Newbie
Posts: 14
Joined: Mon May 11, 2009 8:40 am

Re: displaying comments

Post by misfitxnet »

If i have a string, brought to an array of two values, $a and $b,
how would I be able to use "foreach" to incorporate the two
values into tables?
Is for each only able to handle one array, or is it able to reference a list() array,
and then use the variables created within the list() function?
misfitxnet
Forum Newbie
Posts: 14
Joined: Mon May 11, 2009 8:40 am

Re: displaying comments

Post by misfitxnet »

Being more specific,
how would I be able to
create a table with two rows,
one variable of an array in each row,
and continue to echo these rows as long as there are variables?
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: displaying comments

Post by ldougherty »

What are the two arrays? The output of the text file would be in one array.

Theoretically you can accomplish anything you need to do you just have to think about how logically it should work and then write the code.
Post Reply