PHP Working with files and tables

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
hewstone
Forum Newbie
Posts: 14
Joined: Fri Nov 14, 2008 10:57 am

PHP Working with files and tables

Post by hewstone »

Hey,

I have a "exampledata.txt" file contain the following vlaues;

01, Richard, Hewston, 1256,
02, Matt, Mills, 9872,
03, Debbie, Close, 83642,
04, Fred, Wlesh, 1330,
......


I access the file using PHP with the following string;

$fp = fopen("example.txt", "r"); //Reads the file

and read the contains of the file using;
while (!feof($fp)){
$char = fgetc($fp);
echo $char;
}


However this returns all the value in a single line i.e. "01, Richard, Hewston, 1256, 02, Matt, Mills, 9872,......"

How can I get the code to return the values as they are in the "exampledata.txt" file, i.e. when the while loop reaches "/n" (new line) it then echo a new line on screen? (echo "/n";)

Also i want to put these values into a table format or an array? any ideas on how this can be done?

Thanks in advance for you help
hewstone
Forum Newbie
Posts: 14
Joined: Fri Nov 14, 2008 10:57 am

Re: PHP Working with files and tables

Post by hewstone »

Also i could use "$line = fgets($fp);" however it is important that i read each character in the file, hence why im using "$char = fgetc($fp);"
Last edited by hewstone on Wed Nov 26, 2008 1:44 pm, edited 1 time in total.
hewstone
Forum Newbie
Posts: 14
Joined: Fri Nov 14, 2008 10:57 am

Re: PHP Working with files and tables

Post by hewstone »

And further information! - the "exampledata.txt" file contains table table, saparted by the comma - This infor will be useful when discussing how to put the data into tables

Any ideas welcomed!
tamamk
Forum Commoner
Posts: 25
Joined: Sun Aug 24, 2008 3:37 am

Re: PHP Working with files and tables

Post by tamamk »

Code: Select all

<?php
 
$fp = fopen("example.txt", "r");
 
while (!feof($fp)){
$char = fgetc($fp);
$array_of_line_values =explode(", ", $char);
 
foreach($array_of_line_values as $key => $value){
    echo $value."<br />";
    if($value == "\n"){
        echo "***********Finished a line*******<br><br>";
        break;
    }
}
 
}
 
?>
hewstone
Forum Newbie
Posts: 14
Joined: Fri Nov 14, 2008 10:57 am

Re: PHP Working with files and tables

Post by hewstone »

Thank you for the code. Its codes great :D

I now want to but the values in a table or an array. How can this be done? any ideas?
tamamk
Forum Commoner
Posts: 25
Joined: Sun Aug 24, 2008 3:37 am

Re: PHP Working with files and tables

Post by tamamk »

Look at the code! I named the array "$array_of_line_values". Each line is broken into parts and placed in an array. Google what "explode php" is. Then use the logic that is in that code to do what you want.

If you want to put all values in the same array, you just have to know how to append to an array while using a "while loop" and modify the code above.

Note: "\n" is different than '\n'. "" evaluates things and '' doesn't.
Post Reply