Gary Waudby
Gary Waudby

Reputation: 35

Creating a html table from a text file in PHP

i am trying to create a 5 column html table from a list of names stored in a text file which are comma separated.

I have got this far but I am far from a competent coder and need some help please. At the moment its displaying the table in one long column.

<?php
$f = fopen("names.txt", "r");
while (!feof($f)) { 
$arrM = explode(",",fgets($f));   

$val = current  ( $arrM )  ;
print "<table border=1>";
while ( $val )
    {
      print "<tr> <td> $val </td> ";
      $val = next ( $arrM) ;
      print "<td> $val </td> </tr> ";
      print "\n";
      $val = next ( $arrM );
    }

print "</table>";
}
    ?>

Thanks very much in advance

RESOLVED... Heres the code for any Googlers looking for the same help..

<?php 
    $tdcount = 1; $numtd = 3; // number of cells per row 
    print "<table>"; 
    $f = fopen("names.txt", "r"); 
    while (!feof($f)) { 
        $arrM = explode(",",fgets($f)); 
        $row = current ( $arrM ); 
        if ($tdcount == 1) 
            print "<tr>"; print "<td>$row </td>"; 
        if ($tdcount == $numtd) { 
            print "</tr>"; 
            $tdcount = 1; 
        } else { 
            $tdcount++; 
        } 
    } 
    if ($tdcount!= 1) { 
        while ($tdcount <= $numtd) { 
            print "<td>&nbsp;</td>"; $tdcount++; 
        } print "</tr>"; 
    } 
    print "</table>"; 
?>

Upvotes: 0

Views: 8256

Answers (2)

feeela
feeela

Reputation: 29922

Print a CSV file as HTML table, regardless of how many columns it has, using fgetcsv():

if( ($handle = fopen( 'test.csv', 'r' )) !== false )
{
    $output = '<table>';
    while( ($data = fgetcsv( $handle )) !== false )
    {
        $output .= '<tr>';
        foreach( $data as $value )
        {
            $output .= sprintf( '<td>%s</td>', $value );
        }
        $output .= '</tr>';
    }
    fclose( $handle );
    $output .= '</table>';
}
echo $output;

Upvotes: 2

Matt
Matt

Reputation: 7040

If $arrM contains an array derived from the explode() performed on your comma-separated string of data all you have to do is a foreach() on $arrM

echo "<table border='1'>";
foreach ($arrM as $val) {
    echo "<tr><td>" . $val . "</td></tr>";
}
echo "</table>";

Of course, this is if you want to create a vertical table containing one column, and multiple rows. However, if this is what you're looking to accomplish, it sounds more like a list than a table. In that case, you can try this:

echo "<ul>";
foreach ($arrM as $val) {
    echo "<li>" . $val . "</li>";
}
echo "</ul>";

Then you can style it using CSS (cascading style sheets).

UPDATE: If you want do display all the names in columns, just separate out the <tr> tags:

echo "<table border='1'><tr>";
foreach($arrM as $val) {
    echo "<td>" . $val . "</td>";
}
echo "</tr></table>";

If, instead you only want x number of columns, there's also a way to do that:

$maxCols = 10;
$counter = 0;

echo "<table border='1'>";
foreach ($arrM as $val) {
    $newRow = ($counter++ % $maxCols == 0);
    if ($newRow) {
        echo "<tr>";
    }
    echo "<td>" . $val . "</td>";
    if ($newRow) {
        echo "</tr>";
    }
}
// fill out the rest of the table
$remainingCols = $maxCols - (count($arrM) % $maxCols);
for ($i = 0; $i < $remainingCols; $i++) {
    echo "<td>&nbsp;</td>";
}
echo "</table>";

My math may be off on this, but you should be able to at least use this code and debug it.

Upvotes: 1

Related Questions