Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSV to HTML Script edit 1

Status
Not open for further replies.

Yaik

Programmer
Oct 1, 2010
36
US
I have this script that successfully converts my CSV file to an HTML table, but since I need help working out the format.

If anyone can help me get the table format to
Code:
<table id="table_id">
	<thead>
		<tr>
			<th>Column 1</th>
			<th>Column 2</th>
			<th>etc</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>Row 1 Data 1</td>
			<td>Row 1 Data 2</td>
			<td>etc</td>
		</tr>
		<tr>
			<td>Row 2 Data 1</td>
			<td>Row 2 Data 2</td>
			<td>etc</td>
		</tr>
	</tbody>
</table>

It would be really helpful.

Code:
<?php

$filename = "CompleteSoftInfo.csv";

function viewlog($filename) {
$fp = fopen($filename,"r");
$file = fread($fp,65535);
$replaced = eregi_replace(",", "<td>", $file);
$replaced2 = eregi_replace("\n", "<tr><td>", $replaced);
$replaced3 = eregi_replace("\r", "<tr><td>", $replaced2);
fclose($fp);

return $replaced3;
}
echo "<body>";

echo "<table id='table_id'>";

echo viewlog($filename);
echo "</table></body>";
exit;
?>
 
I prefer the file() function instead of fopen for this, I laos don;t see the need for eregi_replaces here.
Assuming your CSV file has the column headings in the first row:

Code:
function viewlog($filename) {
  [green]/* Open CSV file */[/green]
  $contents=file($filename);
  [green]/* Extract Header Row*/[/green]
  $headers=explode(",",$contents[0]);
  [green]/* Setup Headers*/[/green]
  $table="<thead><tr>";
  foreach($headers as $column){
    $table.="<th>$column</th>";
   }
     [green]/* Remove header row from array to begin parsing  */[/green]
  $r=array_shift($contents);
  $table.="</thead><tbody>";
    [green]/* Loop through each row*/[/green]
  foreach($contents as $row){
     $table.="<tr>";
     $cells=explode(",",$row);
       [green]/* Separate Cells and construct table rows  */[/green]
     foreach($cells as $cell){
        $table.="<td>$cell</td>";
   }
  $table.="</tr>";
}
  [green]/* Close Body */[/green]
$table.="</tbody>";
return $table;
}

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
I LOVE YOU MAN.

Worked like a charm. Thank you very much.
 
and as an alternative I think I would use fgetcsv as it gives flexibility over the incoming csv format.

Code:
echo "<table>\n";
$fh = fopen($filename);
while (FALSE !== $row = fgetcsv($fh)):
	echo "\t<tr>\n\t\t<td>" . implode("</td>\n\t\t<td>",array_map('parse', $row)) . "</td>\n\t</tr>\n";
endwhile;
fclose($fh);
function parse($val){
	if (preg_match('/\d{4}-\d{2}-\d{2}', $val)):
		return htmlspecialchars(date ('d/m/Y', strtotime($val)));
	endif;
	return nl2br(htmlspecialchars($val));
}
echo '</table>';


but if I knew the format in all cases I would probably short circuit and go for file() or file_get_contents() as Phil says.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top