0

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>"; 
?>
2
  • You need to close your <tr> tags. Commented Jul 31, 2012 at 15:39
  • Please please PLEASE put this as an edit to your question, rather than an unreadable comment. Commented Jul 31, 2012 at 16:24

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

Thanks for answering. Your code is much simpler than mine but has achieved the same thing. Is it possible to expand on your code to produce say 5 columns. The array contains several hundred names so more than 1 column is needed
@GaryWaudby Is this table expected to fit within the width of one web page? Because if you have hundreds of columns, it most certainly will not.
Cheers for the help. Added the solution above using your help and suggestions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.