[英]How to display CSV information on web page?
I have this code here, where it is allowed to be uploaded and overwrite on database. 我在这里有此代码,可以在其中上传和覆盖数据库。 But I do not know how do I actually allow it to show the CSV detail on my webpage. 但是我不知道如何实际允许它在我的网页上显示CSV详细信息。 Anybody can help? 有人可以帮忙吗?
<?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>
what to put in the table?? 摆在桌上什么?
try this coding.. 试试这个编码..
<?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>";
Find the below code for displaying as table structure for the processed CSV file over a successful parsing, 找到以下代码,以在成功解析后显示为已处理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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.