<?php
//if pageNum isset figure out where to start(7 rows per page * page num +1
$pageNum = isset($_GET['pageNum']) ? (int)$_GET['pageNum'] : 0;
$startRow = $pageNum == 0 ? 0 : ($pageNum * 7 + 1);
$endRow = $startRow + 7;
$count = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
if($count >= $startRow)
echo ' aantal vervangingen: 30';
$row = 1;
if (($handle = fopen("vervangingen.csv", "r")) !== FALSE) {
echo '<table border="1">';
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
if($count == $endRow)
{
//wait 3 seconds then use javascript to redirect.
sleep(3);
echo '<script>window.loaction.href="theurl?pageNum='.($pageNum +1).'"</script>';
}
}
?>
我在php中创建了一个表,它从.csv文件中获取数据。我的问题是,我如何让php显示前7行,然后它应该刷新页面并显示以下7行,总共有30行。一旦显示了所有行,它仍应刷新页面并重新开始。
我该怎么做?我知道如何刷新php页面,但每次刷新显示7行是相当困难的。有什么帮助吗?
问候
答案 0 :(得分:0)
您可以使用Javascript的setTimeout和一个get变量来完成此任务。
//if pageNum isset figure out where to start(7 rows per page * page num +1
$pageNum = isset($_GET['pageNum']) ? (int)$_GET['pageNum'] : 0;
$startRow = $pageNum == 0 ? 0 : ($pageNum * 7 + 1);
$endRow = $startRow + 7;
$count = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
if($count >= $startRow)
{
$num = count($data);
if ($row == 1) {
echo '<tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
if($count == $endRow)
{
//make it dynamic...
$theUrl = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."?pageNum=".($pageNum+1);
//wait 3 seconds then use javascript to redirect.
echo '<script>setTimeout(function(){
window.location.href="'.$theUrl.'"}, 3000)
</script>';
}
++$count;
}
第一步是找出你所在的页面,然后显示你想要的内容,然后使用javascript重定向,因为在发送标题后php重定向将失败。 setTimeout函数也控制着等待
答案 1 :(得分:0)
以下(已测试)使用PHP会话和JS setTimeout 有趣,但我想知道我是否会使用这种效果。 (我无法将$ handle保存为会话变量。)
<?php // z1.php is this file, z1.csv is the data file
session_start();
echo <<<EOD
<body onload="setTimeout('f1();',3000);">
<script type="text/javascript">
function f1() { window.location.replace("z1.php"); }
</script>
EOD;
if (isset($_SESSION['sessrow1st'])) { $row1st = $_SESSION['sessrow1st']; }
else { $row1st = 1; }
$handle = fopen("z1.csv", "r");
if ($handle === false) { exit("open error"); };
echo "<table border='1'>\n";
$rownum = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$rownum += 1;
if ($rownum < $row1st) continue;
if ($rownum > $row1st+6) break;
$numcols = count($data);
echo '<tr>';
for ($c=0; $c < $numcols; $c++) {
if(empty($data[$c])) { $value = " "; }
else { $value = $data[$c]; }
echo '<td>'.$value.'</td>'; }
echo "</tr>\n"; }
echo '</table></body>';
if (feof($handle)) { $rownum = 1; }
fclose($handle);
$_SESSION['sessrow1st'] = $rownum;
?>