我上传csv文件并验证扩展名。上传文件后,我想在HTML表格中显示CSV文件的所有数据。
我的问题是它显示任何内容,当我使用var_dump时,这就是结果:
array(2) { [0]=> string(10) "bm borj 20" [1]=> NULL }
array(2) { [0]=> string(9) "sm sam 25" [1]=> NULL }
array(2) { [0]=> string(9) "ad med 30" [1]=> NULL }
PHP:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$sel_file = array();
if (isset($_POST['submit'])) {
$result = array();
$fname = $_FILES['sel_file']['name'];
$ext = explode(".", $fname);
if (strtolower(end($ext)) == "csv") {
$filename = $_FILES['sel_file']['tmp_name'];
$handle = fopen($filename, "r");
fgetcsv($handle, 1000, ";");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$result[]=$data;
}
var_dump($result);
fclose($handle);
echo "<div class='alert alert-success alert-dismissable text-center'>
<span class='glyphicon glyphicon-ok'></span> Successfully Imported
</div>";
}
else
{
echo "<div class='alert alert-danger alert-dismissable text-center'>
<span class='glyphicon glyphicon-ok'></span> Successfully Imported
</div>";
}
/* var_dump($result);*/
?>
<form action="/module/getCSV.php" method="post">
<input type="hidden" name="csv_text" id="csv_text">
<input type="submit" value="Export as CSV" onclick="getCSVData()" class="btn btn-success">
</form>
<div class="col-md-7">
<div class="panel">
<div class="panel-body">
<table class="table table-striped table-bordered table-hover display" id="table_with_sorting" style="zoom: 85%">
<thead>
<tr>
<th style="width:10%"></th>
<th style="width:10%"></th>
<th style="width:10%"></th>
<th style="width:10%"></th>
<th style="width:10%"></th>
<th style="width:10%"></th>
</tr>
</thead>
<tbody>
<?php
foreach($result as $a => $p)
?>
<tr style="text-align: center;">
<td><? echo $p['']; ?></td>
<td><? echo $p['']; ?></td>
<td><? echo $p['']; ?></td>
<td><? echo $p['']; ?></td>
<td><? echo $p['']; ?></td>
<td><? echo $p['']; ?></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
</div>
</div>
</div>
<?php
}
?>
这是我的CSV文件:
答案 0 :(得分:0)
我的Csv文件包含三列用户名,电子邮件和电话号码:
$handle = fopen("yourexcel.csv",'r');
if(!$handle) die('Cannot open uploaded file.');
//Read the file as csv
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$username = $data['0'];
$email = $data['1'];
$phonenumber = $data['2'];
//here data[0],data[1] are the columns of the excel sheet, if you want so database it then follow :
if(strlen($username ) || strlen($email) || strlen($phonenumber)){
$query2 = "insert into user_master(username,email,phonenumber) values ('".$username."','".$email."','".$phonenumber."');";
mysql_query($query2);
}
如果你想打印它,那就回声&#34; $ data
答案 1 :(得分:0)
您应该查看手册中的示例:PHP: fgetcsv
<?php
$result=array(); // put the data from CSV here
$filename = $_FILES['sel_file']['tmp_name'];
if (($handle = fopen($filename, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$result[]=$data;
}
fclose($handle);
}
var_dump($result);
?>
你的脚本在这里有一些问题:
在您的脚本中查看:
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
希望这有帮助!
它将返回:
array(4) { [0]=> array(3) { [0]=> string(8) "lastname" [1]=> string(9) "firstname" [2]=> string(3) "age" } [1]=> array(3) { [0]=> string(2) "bm" [1]=> string(4) "borj" [2]=> string(2) "20" } [2]=> array(3) { [0]=> string(2) "sm" [1]=> string(3) "sam" [2]=> string(2) "25" } [3]=> array(3) { [0]=> string(2) "ad" [1]=> string(3) "med" [2]=> string(2) "30" } }
P.S。请记住,使用fgetcsv:
CSV文件中的空白行将作为包含a的数组返回 单个空字段,不会被视为错误。
编辑: 要获取HTML表而不是数组,请使用:
$filename = $_FILES['sel_file']['tmp_name'];
if (($handle = fopen($filename, "r")) !== FALSE) {
echo "<table border='1'>";
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$result[]=$data;
echo "<tr><td>".implode("</td><td>",$data)."</td></tr>";
}
echo "</table>";
fclose($handle);
}
//var_dump($result);
?>
EDIT2:将数据放在底部的Bootstrap表中
将“thead”部分更改为:
<?php
echo "<thead><tr>";
foreach ($result[0] as $th) {
echo "<th style=\"width:10%\">$th</th>";
} unset($th);
echo "</tr></thead>";
?>
将“tbody”部分更改为:
<?php
echo "<tbody>";
for ($i=1; $i<count($result); $i++) {
echo "<tr><td>".implode("</td><td>",$result[$i])."</td></tr>";
}
echo "</tbody>";
?>
答案 2 :(得分:-1)
使用&#34; fread($ f,filesize(filename.kit)&#34;而不是fgetcsv。
<?Php
echo "<html><body><table border=1>";
$f = fopen("data2.csv", "r");
$fr = fread($f, filesize("data2.csv"));
fclose($f);
$lines = array();
$lines = explode("\n\r",$fr); // IMPORTANT the delimiter here just the "new line" \r\n, use what u need instead of...
for($i=0;$i<count($lines);$i++)
{
echo "<tr>";
$cells = array();
$cells = explode(";",$lines[$i]); // use the cell/row delimiter what u need!
for($k=0;$k<count($cells);$k++)
{
echo "<td>".$cells[$k]."</td>";
}
// for k end
echo "</tr>";
}
// for i end
echo "</table></body></html>";
?>