如何在网页上显示CSV信息?

How to display CSV information on web page?

我这里有这段代码,允许在数据库中上传和覆盖它。 但我不知道如何让它在我的网页上显示 CSV 详细信息。 有人可以帮忙吗?

 <?php
 session_start();
if (isset($_POST["Upload"])) {
include "dbFunctions.php";
mysql_select_db($DB) or die(mysql_error());
$filename = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
    $file = fopen($filename, "r");

    $count = 0;
    do {
        $excelData = fgetcsv($file, 10000, ",");

        if ($excelData === FALSE && $count == 0) {
            break;
        } elseif ($excelData !== FALSE) {
            if (count($excelData) < 8) {
                break;
            }
            $count++;
            $sql = "INSERT into profile (id, password, name, dob, contact, email, gender, module, school) values ('" . $excelData[0] . "',SHA1('" . $excelData[1] . "'),'" . $excelData[2] . "','" . $excelData[3] . "','" . $excelData[4] . "','" . $excelData[5] . "','" . $excelData[6] . "','" . $excelData[7] . "','" . $excelData[8] . "') ON DUPLICATE KEY UPDATE password=SHA1('" . $excelData[1] . "'), name='$excelData[2]', dob='$excelData[3]', contact='$excelData[4]', email='$excelData[5]', gender='$excelData[6]', module='$excelData[7]', school='$excelData[8]'";


            mysql_query($sql);
        }
    } while ($excelData !== FALSE);
    if ($count > 0) {
        $msg = 'CSV File has been successfully imported!';
    } else {
        $msg = 'Invalid File. Please check that the file is uploaded in .CSV format!';
    }
//        echo $msg;
}
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="stylesheet/style.css" rel="stylesheet"/>
</head>
<body>

    <h2>Load Details</h2>
    <br>
    <?php
    echo $msg
    ?>
    <br>

    <table align='center' border='3' cellpadding='5' cellspacing='0'>
        <tr>
            <th>ID </th>
            <th>Password</th>
            .....

        </tr>
    </table>

    <?php
    echo "<tr>";
    echo "<td>" "</td>";
    echo "</tr>";
    ?>

</body>

要在 table 中放什么??

试试这个编码..

<?php
 echo "<html><body><table style='border: 1px solid black;'>\n\n";
 $f = fopen("so-csv.csv", "r");
 while (($line = fgetcsv($f)) !== false) {
    echo "<tr>";
    foreach ($line as $cell) {
            echo "<td style='border: 1px solid black;'>" . htmlspecialchars($cell) . "</td>";
    }
    echo "</tr>\n";
  }
  fclose($f);
    echo "\n</table></body></html>";

找到下面的代码,用于在成功解析后显示为 table 结构的已处理 CSV 文件,

<?php
session_start();
if (isset($_POST["Upload"])) {
    include "dbFunctions.php";
    mysql_select_db($DB) or die(mysql_error());
    $filename = $_FILES["file"]["tmp_name"];
    if ($_FILES["file"]["size"] > 0) {
        $file = fopen($filename, "r");

        $count = 0;
        do {
            $excelData = fgetcsv($file, 10000, ",");

            if ($excelData === FALSE && $count == 0) {
                break;
            } elseif ($excelData !== FALSE) {
                if (count($excelData) < 8) {
                    break;
                }
                $count++;
                $sql = "INSERT into profile (id, password, name, dob, contact, email, gender, module, school) values ('" . $excelData[0] . "',SHA1('" . $excelData[1] . "'),'" . $excelData[2] . "','" . $excelData[3] . "','" . $excelData[4] . "','" . $excelData[5] . "','" . $excelData[6] . "','" . $excelData[7] . "','" . $excelData[8] . "') ON DUPLICATE KEY UPDATE password=SHA1('" . $excelData[1] . "'), name='$excelData[2]', dob='$excelData[3]', contact='$excelData[4]', email='$excelData[5]', gender='$excelData[6]', module='$excelData[7]', school='$excelData[8]'";


                mysql_query($sql);
            }
        } while ($excelData !== FALSE);
        if ($count > 0) {
            $msg = 'CSV File has been successfully imported!';
        } else {
            $msg = 'Invalid File. Please check that the file is uploaded in .CSV format!';
        }
        //        echo $msg;
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="stylesheet/style.css" rel="stylesheet"/>
</head>
<body>

    <h2>Load Details</h2>
    <br>
    <?php
    echo $msg
    ?>
    <br>

    <?php 

    if(strpos($msg,'success') !== false){
?>
      <table align='center' border='3' cellpadding='5' cellspacing='0'>
        <tr>
            <th>ID </th>
            <th>Password</th>
            .....

        </tr>
<?php 

    $dataList = fopen($filename, "r");

    $loopCount = 0;
        while($dataRow = fgetcsv($dataList, 10000, ",")){


                if (count($dataRow) < 8) {
                    continue;
                }
                $loopCount++;
                ?>
            <tr>
                <td><?php echo $dataRow[0]; ?> </td>
                <td><?php echo $dataRow[1]; ?> </td>
                .....

            </tr>
                <?php 
            }
        }
    ?>

    </table>



</body>
</html>