Php 如何在表中显示所有CSV数据?

Php 如何在表中显示所有CSV数据?,php,csv,Php,Csv,我上传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]=> N

我上传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文件:

使用“fread($f,filesize(filename.kit)”代替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>";
?> 

我的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);
    }

如果您只想将$data“echo”打印出来,您应该查看手册中的示例:

希望这有帮助

它将返回:

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" } }
请记住,对于fgetcsv:

CSV文件中的空行将作为包含 单个空字段,将不会被视为错误

编辑: 要获取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);
?>
$filename=$\u文件['sel\u文件]['tmp\u名称];
if($handle=fopen($filename,“r”)!==FALSE){
回声“;
while(($data=fgetcsv($handle,1000,“,”)!==FALSE){
$result[]=$data;
echo“”。内爆(“,$data”);
}
回声“;
fclose($handle);
}
//var_dump($结果);
?>
EDIT2:将数据放入底部的引导表中

将“thead”部分更改为:

<?php
echo "<thead><tr>";
foreach ($result[0] as $th) {
    echo "<th style=\"width:10%\">$th</th>";
} unset($th);
echo "</tr></thead>";
?>
<?php
echo "<tbody>";
for ($i=1; $i<count($result); $i++) {
    echo "<tr><td>".implode("</td><td>",$result[$i])."</td></tr>";
}
echo "</tbody>";
?>

将“t车身”部分更改为:

<?php
echo "<thead><tr>";
foreach ($result[0] as $th) {
    echo "<th style=\"width:10%\">$th</th>";
} unset($th);
echo "</tr></thead>";
?>
<?php
echo "<tbody>";
for ($i=1; $i<count($result); $i++) {
    echo "<tr><td>".implode("</td><td>",$result[$i])."</td></tr>";
}
echo "</tbody>";
?>


有两件事会跳出:1)WHILE循环被die()终止,2)$row始终为0。@BigScar即使我删除它,它也不会改变任何东西。你从不更新循环中的$row,所以你不断地设置
$sel_file[0]
那么解决方案是什么呢?首先我上传文件,我没有给它一个特定的CSV文件来读取它,当我使用fread时,它会给出错误。你知道fgetcsv基本上已经在做
fread()
,然后是
explode()
。这是一个csv。为什么不使用专门为读取这些文件而设计的工具?我不想给它提供我想要的列的名称,这些列在php上恢复了表中的所有数据。当我使用var_dump时,它会返回我的所有数据,但它不会显示这些数据。这是我的问题。在代码中,为什么给$data[1]=null?此外,您的输出显示array2->filename数组:您是否从任何其他页面发布文件名?$sel\u file是数组,然后使用print\r($sel\u file);若要显示它@我不在其他页面上使用它。现在,当我在处理之前或之后使用var_dump时,给我没有var_dump的数据,它将不返回任何内容。请编辑您的问题,并向我们显示您想要的最终结果。你想要一个HTML表格吗?你想要一个数组吗?(我的示例为您提供了一个数组)是否需要简单的数据打印?检查手册中的示例。是的,我使用了你为我编写的内容,它只适用于var_dump。我想要一个包含我的csv文件的所有数据的HTML表。好的,检查答案的最后部分。