1.CSV文件的第一行是title并非数据
2.CSV文件的列顺序不是固定的,也许可能是 title-a,title-b,title- c也可能是title-b,title-a,title-c
<?php
$filename = 'SampleTech.txt';
@$datafile = file($filename); //把文件读取成数组
$datas = $title_array = array();
if(is_array($datafile)) {
foreach($datafile as $data) {
$datas[] = $data; //把数组存在$datas数组中
}
}
//处理title
$title_array = explode("\t",array_shift($datas)); //将数组开头的单元移出数组
foreach($title_array as $key => $value)
{
$title_array[$key] = str_replace("\r\n","",mb_strtolower(str_replace(" ","",$value))); //替换空格和换行
}
$title_array = array_flip($title_array); //交换数组中的键和值
extract($title_array, EXTR_OVERWRITE); //从数组中将变量导入到当前的符号表
$datas = array_filter($datas,'deletenull'); //deletenull 是回调函数,用来删除是空数组
$total = count($datas); //数组行数
if($total) //如果不为空
{
//显示表头
echo '<table width="100%" border="0" cellpadding="3" cellspacing="2"><tbody>
<tr>
<td width="7%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE3">Row ID</span></td>
<td width="8%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE1">;Product Name</span></td>
<td width="5%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE1">ISBN</span></td>
<td width="5%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE1">UPC</span></td>
<td width="5%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE1">Manufacturer Part Number</span></td>
<td width="4%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE1">Manufacturer</span></td>
<td width="6%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE1">Categorization</span></td>
<td width="7%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE1">Description</span></td>
<td width="3%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE1">Selling Price</span></td>
<td width="5%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE1">Availability</span></td>
<td width="11%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE1">;Product url</span></td>
<td width="11%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE1">Image URL</span></td>
<td width="5%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE1">Shipping Cost</span></td>
<td width="9%" align="center" nowrap="nowrap" bgcolor="#333333"><span class="STYLE1">;Product Condition</span></td>
</tr>';
//显示表头结束
for($i=0;$i<$total;$i++)
{
$row = explode("\t",$datas[$i]);
$count = $i+1;
//循环一行的列
echo '<tr>';
echo '<td align="center" bgcolor="#F3F3F3" valign="top">'.$count.'</td>';
echo '
<td align="center" valign="top" nowrap="nowrap" bgcolor="#F3F3F3">'.$row[$productname].'</td>
<td align="center" valign="top" nowrap="nowrap" bgcolor="#F3F3F3">'.floatval($row[$isbn]).'</td>
<td align="center" valign="top" nowrap="nowrap" bgcolor="#F3F3F3">'.floatval($row[$upc]).'</td>
<td align="center" valign="top" nowrap="nowrap" bgcolor="#F3F3F3">'.floatval($row[$manufacturerpartnumber]).'</td>
<td align="center" valign="top" nowrap="nowrap" bgcolor="#F3F3F3">'.$row[$manufacturer].'</td>
<td align="center" valign="top" nowrap="nowrap" bgcolor="#F3F3F3">'.$row[$categorization].'</td>
<td align="center" valign="top" bgcolor="#F3F3F3">'.$row[$description].'</td>
<td align="center" valign="top" bgcolor="#F3F3F3">'.$row[$sellingprice].'</td>
<td align="center" valign="top" nowrap="nowrap" bgcolor="#F3F3F3">'.$row[$availability].'</td>
<td align="center" valign="top" nowrap="nowrap" bgcolor="#F3F3F3">'.$row[$producturl].'</td>
<td align="center" valign="top" bgcolor="#F3F3F3">'.$row[$imageurl].'</td>
<td align="center" valign="top" bgcolor="#F3F3F3">'.$row[$shippingcost].'</td>
<td align="center" valign="top" nowrap="nowrap" bgcolor="#F3F3F3">'.$row[$productcondition].'</td>
';
echo '</tr>';
//一行显示完毕
}
echo "</tbody></table>";
} else
{
echo 'file is empty';
}
//配合array_filter()删除空数组的回调函数
function deletenull($var)
{
return (trim($var) != "");
}
?>
插入数据就不写了,能显示出来,自然也能插进到数据库