LuckY07 Posted December 2, 2007 Share Posted December 2, 2007 I have a text file called 'input.txt' that I want to put into an array and display each row. The setup I have now is only displaying the first letter from each row for some reason.. here is my code: (i want to print the entire row, not just the 1st letter) $array = file("input.txt"); foreach($array as $row) { $i = 0; print "$row[i]"; print "<br/>"; $i++; } Link to comment https://forums.phpfreaks.com/topic/79778-how-do-i-display-a-text-file-as-an-array/ Share on other sites More sharing options...
pocobueno1388 Posted December 2, 2007 Share Posted December 2, 2007 <?php $array = file('input.txt'); foreach ($array as $row) { echo $row.'<br>'; } ?> Link to comment https://forums.phpfreaks.com/topic/79778-how-do-i-display-a-text-file-as-an-array/#findComment-404005 Share on other sites More sharing options...
marcus Posted December 2, 2007 Share Posted December 2, 2007 one.txt hello my name is sexy what is yours? here.php <?php $lines = file("./one.txt"); $x=1; foreach($lines AS $line){ echo $x . " " . $line . "<br>\n"; $x++; } ?> output 1 hello 2 my 3 name 4 is 5 sexy 6 what 7 is 8 yours? Link to comment https://forums.phpfreaks.com/topic/79778-how-do-i-display-a-text-file-as-an-array/#findComment-404007 Share on other sites More sharing options...
LuckY07 Posted December 2, 2007 Author Share Posted December 2, 2007 thanks! that worked. i wonder why that only showed 1 letter before.. maybe because i was using print with double quotes? i am going to be using this text file eventually for a 'user comments' box on my website. is displaying dynamic html from a text file for user comments practical? Link to comment https://forums.phpfreaks.com/topic/79778-how-do-i-display-a-text-file-as-an-array/#findComment-404008 Share on other sites More sharing options...
Barand Posted December 2, 2007 Share Posted December 2, 2007 i wonder why that only showed 1 letter before.. maybe because i was using print with double quotes? No, it was because you were just printing $row[0], which is the the first letter of $row. Link to comment https://forums.phpfreaks.com/topic/79778-how-do-i-display-a-text-file-as-an-array/#findComment-404179 Share on other sites More sharing options...
LuckY07 Posted December 2, 2007 Author Share Posted December 2, 2007 interesting. so if i wanted to just print for example, row 2, how would i do that? i guess i dont know how to move the curser to the 2nd line. basically i have an html page.. and i want to print these names and emails, along with a comment box eventually. I want to post the comments under the right date of a simple 1 page blog. for example) here is how i want the page to look: Blog Entry #1 <names for each comment> <comments from that date> Blog Entry #2 <names for each comment> <comments from that date> what would be the best way to do this? should i create a new text file for each date? or store all the comments/names/email in 1 text? any help is appreciated. i am basically trying to add a 'comment box' to a simple website. tks. Link to comment https://forums.phpfreaks.com/topic/79778-how-do-i-display-a-text-file-as-an-array/#findComment-404255 Share on other sites More sharing options...
Barand Posted December 2, 2007 Share Posted December 2, 2007 $array = file("input.txt"); to print the second line in the file echo $array[1]; Link to comment https://forums.phpfreaks.com/topic/79778-how-do-i-display-a-text-file-as-an-array/#findComment-404260 Share on other sites More sharing options...
LuckY07 Posted December 2, 2007 Author Share Posted December 2, 2007 Thanks. Do you think that I should be using MySQL for this? Basically I want to create a simple website with blog entries on the main page. When you click the blog entry it takes you to another page where it displays the entry, and then a form where users can comment. I then want to display just the name/email/comments that user leave on that page, like the following: ---------------------------------------------------------------------------------------------------- Blog Entry: <user name/email> <comments> <user name2/email> <comments> Blog Entry2: <user name/email> <comments> <user name2/email> <comments> ---------------------------------------------------------------------------------------------------- any help is greatly appreciated! this is the firt time i'm creating a comments box, and using PHP. Link to comment https://forums.phpfreaks.com/topic/79778-how-do-i-display-a-text-file-as-an-array/#findComment-404291 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.