Page 1 of 1

displaying comments

Posted: Tue May 12, 2009 4:47 pm
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?

Re: displaying comments

Posted: Tue May 12, 2009 6:38 pm
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.

Re: displaying comments

Posted: Tue May 12, 2009 6:54 pm
by misfitxnet
Is there anyway to create new table rows for each new line?

Re: displaying comments

Posted: Tue May 12, 2009 7:19 pm
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

Re: displaying comments

Posted: Tue May 12, 2009 7:28 pm
by misfitxnet
where can I learn about "For each" statements?

Re: displaying comments

Posted: Tue May 12, 2009 11:14 pm
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

Re: displaying comments

Posted: Tue May 12, 2009 11:21 pm
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?

Re: displaying comments

Posted: Tue May 12, 2009 11:22 pm
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?

Re: displaying comments

Posted: Wed May 13, 2009 7:36 pm
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.