I am trying to take data from my CSV file and use php to display it in an html table, sorted by customer last name. I have tried a couple things and it doesn't seem to be working.
The output I got is: Right now the format is last, first,address,city,district,postal code How would i import this to an html table using php?
using this code.
if( ($handle = fopen( 'input.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;
In your last comment you ask for table header so you can write code like below,
echo '<table border="1">';
echo '<thead>';
echo '<tr>';
echo '<th>last</th>';
echo '<th>first</th>';
echo '<th>address</th>';
echo '<th>.....</th>';
echo '<th>......</th>';
echo '<th>.....</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
echo '<tr>';
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
} else {
$value = $data[$c];
}
echo '<td>'.$value.'</td>';
}
echo '</tr>';
$row++;
}
echo '</tbody></table>';
try this code maybe this one helps you
<?php
$row = 1;
if (($handle = fopen("info.csv", "r")) !== FALSE) {
echo '<table border="1">';
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.