how to create html table in php



PHP Snippet 1:

<?php

$lines = preg_split('~\s*[\r\n]+\s*~', file_get_contents('prod.txt'));

foreach($lines as $i => $line) {
    $pairs = explode(';', $line);
    foreach($pairs as $pair) {
        list($column, $value) = explode('=', $pair, 2);
        $columns[$column] = true;
        $rows[$i][$column] = $value;
    }
}

$columns = array_keys($columns);


echo '<table><thead><tr>';

foreach($columns as $column) {
    echo '<th>'.$column.'</th>';
}

echo '</tr></thead><tbody>';

foreach($rows as $row) {
    echo '<tr>';
    foreach($columns as $column) {
        echo '<td>'.$row[$column].'</td>';
    }
    echo '</tr>';
}
echo '</tbody></table>';

?>

PHP Snippet 2:

<?php
$inputfile = file("prod.txt");

$data_lines = array();
foreach ($inputfile as $line)
{
    $data_lines[] = explode(";", $line);
}

//Get column headers.
$first_line = array();
foreach ($data_lines[0] as $dl)
{
    $first_line[] = explode("=", $dl);
}
$headers = array();
foreach ($first_line as $fl)
{
    $headers[] = $fl[0];
}

// Get row content.
$data_cells = array();
for ($i = 0; $i < count($data_lines); $i++)
{
    $data_cell = array();
    for ($j = 0; $j < count($headers); $j++)
    {
        $data_cell[$j] = substr($data_lines[$i][$j], strpos($data_lines[$i][$j], "=")+1);
    }
    $data_cells[$i] = $data_cell;
    unset($data_cell);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>HTML Table With PHP</title>
    </head>
    <body>
        <table border="1">
            <tr>
            <?php foreach ($headers as $header): ?>
                <th><?php echo $header; ?></th>
            <?php endforeach; ?>
            </tr>
        <?php foreach ($data_cells as $data_cell): ?>
            <tr>
            <?php for ($k = 0; $k < count($headers); $k++): ?>
                <td><?php echo $data_cell[$k]; ?></td>
            <?php endfor; ?>
            </tr>
        <?php endforeach; ?>
        </table>
    </body>
</html>

PHP Snippet 3:

echo '<table><tr><td>PREFIX</td><td>Part</td>File</td></tr>';
foreach ($Inputfile as $line){
   $pieces = explode(";", $line);
   $count=count($pieces);

   echo '<tr>';
   for ($counter=0; $counter <$count; $counter++)
   {
     echo '<td>'.$pieces[$counter].'<td>';
   } 
  echo '</tr>';
}

PHP Snippet 4:

echo '<table><tr><td>PREFIX</td><td>PART</td><td>FILE</td></tr>';
foreach ($Inputfile as $line)
{
    $pieces = explode(';', $line);
    echo sprintf(
        '<tr><td>%s</td><td>%s</td><td>%s</td></tr>',
        ltrim($pieces[0], 'PREFIX='),
        ltrim($pieces[1], 'PART='),
        ltrim($pieces[2], 'FILE=')
    );
}
echo '</table>';

PHP Snippet 5:

function line2record($line) {
   $recordAA = array();
   $keyValuePairs = explode(';',$line);
   foreach($keyValuePairs as $kvp) {
       $pieces = explode('=',$kvp);
       $recordAA[$pieces[0]] = $pieces[1];
   }
   return $recordAA;
}

function record2TR($recordAA) {
   $str = implode('</td><td>',$recordAA);
   return "<tr><td>$str</td></tr>";
}

PHP Snippet 6:

$Inputfile = file("prod.txt");
foreach ($Inputfile as $line)
    echo record2TR(line2record($line));

PHP Snippet 7:

function record2TH($recordAA) {
    $headers = array_keys($recordAA);
    $str = implode('</th><th>',$headers);
    return "<tr><th>$str</th></tr>";
}

PHP Snippet 8:

$fp = fopen('prod.txt','r');
$line = fgets($fp,MAX_LINE_LENGTH);  // you'd set this constant
$firstRecord = line2record($line);
echo "<table>";
echo record2TH($firstRecord);
echo record2TR($firstRecord);
while($line = fgets($fp,MAX_LINE_LENGTH))
    echo record2TR(line2record($line));
echo "</table>";

PHP Snippet 9:

echo '<table id="time-table" border="2"><thead><tr >';
echo '<th style="width:40%; padding: 7px;">Course Code</th>';
echo '<th style="width:30%">Course Name</th>';
echo '<th style="width:40%">Slot</th>';
for ($x = 0; $x < 5; $x++) {
   echo '<tr id=row'.$x.'>';
   echo '<th style="width:40%; padding: 15px;" >',$xml_data->children()[$x]->coursecode .'</th>';
   echo '<th style="width:30%">',$xml_data->children()[$x]->coursename .'</th>';
   echo '<th style="width:20%">',$xml_data->children()[$x]->slot .'</th>';
 }
echo '</tbody></table>';