reading a text file with the correct while loop

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
ocd
Forum Newbie
Posts: 2
Joined: Tue Feb 23, 2010 8:36 am

reading a text file with the correct while loop

Post by ocd »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


The goal of this file is to read in from a text file that has entered information from a forum in the following format.

Code: Select all

 
Male:Yes:No:School:
Female:No:No:Work:
Male:Yes:Yes:Student:
 
Each time a user clicks submit on the survery, a new line is added to the text file using another PHP program that I understand and therefore omitted. I need to take the information in the text file above and present it on a web page in a nice table... with "Number (1-15)" before it on the same <tr> but not <td>.

My code below basically prints one long table with all the information and question numbers that go on forever. I know why this happens in terms of the code, but I just don't know how to stop the code after each line of reading from the text file, so that a new table can be made. Can anyone help me with this? I greatly appreciate it! Thank you.

Code: Select all

 
<?php
 
$page = file_get_contents('./output.txt');
$tok = strtok($page, ":");
 
$i = 1;
echo "<table border=1>";
 
while ($tok) 
{
echo "<tr><td>Number $i</td>";
echo "<td>$tok\n</td></tr>";
$i++;
$tok = strtok(":\r\n");
}
echo "</table>";
 
?>
 

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
dzelenika
Forum Newbie
Posts: 10
Joined: Sat Feb 20, 2010 2:29 pm

Re: reading a text file with the correct while loop

Post by dzelenika »

Use file function instad of file_get_contents.
This function returns an array of file rows. So You can know in which row you are.
ocd
Forum Newbie
Posts: 2
Joined: Tue Feb 23, 2010 8:36 am

Re: reading a text file with the correct while loop

Post by ocd »

Thank you! You were very helpful. Was able to loop through each line as an array.
Post Reply