Jump to content

loading array from txt file into tables


ee

Recommended Posts

If i have a text file with lines of code submitted: whilst i can break it line by line in a table...Is it possible to load each upload from the txt file into a new table????

 

 

 

e.g.

 

<?

$test = file("details.txt");

$number_of_adverts = count($test);

if ($number_of_adverts == 0)

{

echo "<p>No Adverts currently</p>";

}

echo "<table width = '40%' border=0 bgcolor = \"#000000\"> \n";

for ($i=0; $i<$number_of_adverts; $i++)

$line = explode ( "\t", $test[$i] );

{

echo "<tr><th align = 'left'>First Name</td><td align = 'left'>$line[0]</td></tr>

<th align = 'left'>Surname</td><td>$line[1]</td></tr>

<tr><td><br></td><td></td></tr>

<th align = 'left'>Phone No</td><td>$line[2]</td></tr>

<tr><td><br></td><td></td></tr>

<th align = 'left'>Position Type</td> <td>$line[3]</td></tr>

<tr><td><br></td><td></td></tr>

<th align = 'left'>Details</td><td>$line[4] </td></tr>

<tr><td><br></td><td></td></tr>

<th align = 'left'>Education</td><td>$line[5]</td></tr>

<tr><td><br></td><td></td></tr>

<th align = 'left'>Time of Day to Contact</td><td>$line[6]</td>

<tr><td><br></td><td></td></tr>

<tr><td><br></td><td></td></tr>

      </tr>";

 

}

echo "</table>";

?>

 

so, every line of record in the tetx file creates a new table down the screen???

Currently..this is only showing one of the lines from the text file????

Link to comment
https://forums.phpfreaks.com/topic/99512-loading-array-from-txt-file-into-tables/
Share on other sites

try this...

 

<?php
$file = fopen("/path/to/file.txt");
while($line = fgets($file)){
$items = explode ( "\t", $line );
echo "<table>
     <tr>
          <th align = 'left'>First Name</th> #(watch your open/closing tags, i changed from /td to /th)
          <td align = 'left'>$items[0]</td>
     </tr>
          <th align = 'left'>Surname</th> #(watch your open/closing tags, i changed from /td to /th)
          <td>$line[1]</td>
     </tr>
...</table>
}

This is completely straight off the top of my head so I don't know if this will work but I hope it gives you the basic idea...

$mydata=file("mydatafile.txt");
if (count($mydata)<1) {
  echo 'No data exists.';
} else {
  echo '<table width="40%" border="0" bgcolor="#000000">';
  for ($i=0;$i<count($mydata);$i++) {
    $line=explode("\t",$mydata[$i]);
    echo '<tr><td>Forename</td><td>'.$line[0].'</td></tr>';
    echo '<tr><td>Surname</td><td>'.$line[1].'</td></tr>';
    echo '<tr><td>Phone</td><td>'.$line[2].'</td></tr>';
    echo '<tr><td>Position</td><td>'.$line[3].'</td></tr>';
    echo '<tr><td>Details</td><td>'.$line[4].'</td></tr>';
    echo '<tr><td>Education</td><td>'.$line[5].'</td></tr>';
    echo '<tr><td>Contact Time</td><td>'.$line[6].'</td></tr>';
  }
  echo '</table>';
}

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.