2016-12-03 100 views
1

我想从我的CSV文件中提取数据,并使用php将其显示在一个html表格中,并按客户姓氏排序。我已经尝试了几件事情,它似乎并没有工作。使用php将CSV转换为HTML表格

我得到的输出是:enter image description here 眼下格式最后,第一,地址,市,区,邮政编码 我将如何导入此使用PHP的HTML表?

使用此代码。

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; 
+1

那你试试?问题究竟在哪里? – Dekel

+0

我提交了我尝试使用的代码。 @Dekel – BoostedMonkey

+0

好得多:)请添加**输出**并解释问题出在哪里/是什么。 – Dekel

回答

1

在你最后的评论你问表头,所以你可以像下面写代码,

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 = "&nbsp;"; 
      } else { 
       $value = $data[$c]; 
      }     
      echo '<td>'.$value.'</td>'; 

     } 
     echo '</tr>'; 

     $row++; 
    } 

    echo '</tbody></table>';