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
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>
}

Link to comment
Share on other sites

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>';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.