Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating a table from a text file

Status
Not open for further replies.

simonsimon

Programmer
Feb 12, 2003
8
US
A little of topic . . . but I'm trying to populate a table with data from a text file.

The data in the .txt file is something like:

020|Simon|Pearce|Soutport
021|Fred|Bloggs|Manchester
022|Frank|Bruno|London

and I just need to use PHP to display all this in a table on a web page. Any advice greatfully received

Simon

 
This should work:
<?php
echo '<table>';
$lines = file(&quot;file.txt&quot;); // reads the file to an array
foreach ($lines as $line)
{
echo '<tr>';
$columns = explode(&quot;|&quot;, $line); // chunk each line into pieces
foreach ($columns as $column)
echo '<td>' . $column . '</td>'; // print each column
echo '</tr>';
}
echo '</table>';?> //Daniel
 
Back again :)

That was great - how difficult would it be to get the input from the text file and put it into a variable?

Eg:

020|Simon|Pearce|Soutport

020 would be $num
Simon would be $firstname
Pearce would be $lastname
Southport would be $town

Thanks people!

Simon
 
Instead of the foreach($columns as $column), use the array:
Code:
$num=$columns[0];
$firstname=$columns[1];
$lastname=$columns[2];
$town=$columns[3];
then do whatever you wanted to do with the variables...

Kevin
 
Thanks Kevin, Thanks Daniel.

They both work great :)

Top advise.

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top