Jump to content

Reading TXT File - almost there!


swamp

Recommended Posts

Hello, I've got this script:

 

 <?php
echo '
<table>
<tr>

</tr>';

$fp = fopen('dep.txt','r');
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}

while (!feof($fp)) {
$line = fgets($fp,1024); //use 2048 if very long lines
$row++;
list ($project, $destination, $gate, $time, $remarks) = split ('\|', $line);
$col[$row] = array($project, $destination, $gate, $time, $remarks);
}

fclose($fp);

sort($col);
reset ($col);

$arrays = count($col) - 1;

$loop = -1;
while ($loop < $arrays) {
$loop++;
echo '
<tr>
<td>'.$col[$loop][1].'</td>
<td>'.$col[$loop][2].'</td>
<td>'.$col[$loop][3].'</td>
<td>'.$col[$loop][4].'</td>
<td>'.$col[$loop][5].'</td>
<td>'.$col[$loop][6].'</td>
<td>'.$col[$loop][7].'</td>
</tr>';
}

echo '
</table>

'?> 

 

The text file 'dep.txt' looks like this:

 

Project1|Destination1|Gate1|Time1|Remark1

Project2|Destination2|Gate2|Time2|Remark2

 

I was wondering how I would output data seperatley so that I could put just the $project entry into a place or just the $destination into a place, not all just going into the table.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/101732-reading-txt-file-almost-there/
Share on other sites

I was wondering how I would output data seperatley so that I could put just the $project entry into a place or just the $destination into a place, not all just going into the table.

Can you explain a bit more. I am not understanding your question.

You already have it setup like this:

echo $col[$row][0]; // = 'Project';
echo $col[$row][1]; // = 'Destination';
echo $col[$row][2]; // = 'Gate';
echo $col[$row][3]; // = 'Time';
echo $col[$row][4]; // = 'Remarks';

 

So if you want to display the destination for row 3, you'd have:

echo $col[3][1];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.