PHP - refreshing page and showing different rows in a table

不打扰是莪最后的温柔 提交于 2019-12-12 00:48:52

问题


 <?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 = "&nbsp;";
        }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>';
}

 }

 ?>

I made a table in php which gets the data from a .csv file. My question is, how do i let php show the first 7 rows, then it should refresh the page and show the following 7 rows, there are 30 in total. Once it has shown all the rows, it should still refresh the page and start all over again.

How do I do this? I know how to refresh a php page, but showing 7 rows per refresh is quite hard. Any help?

Greetings


回答1:


The following (tested) uses PHP sessions and JS setTimeout.
Interesting, but I wonder if I will ever use this effect. (I was unable to save the $handle as a session variable.)

<?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 = "&nbsp;"; }
    else { $value = $data[$c]; }
    echo '<td>'.$value.'</td>'; }
  echo "</tr>\n"; }
echo '</table></body>';
if (feof($handle)) { $rownum = 1; }
fclose($handle);
$_SESSION['sessrow1st'] = $rownum;
?>



回答2:


You can use Javascript's setTimeout and a get variable to accomplish this.

//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 = "&nbsp;";
            }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;

}

First step is figure out which page you're on, then display what you want, then use javascript redirect, as php redirect will fail after headers are sent. Also the setTimeout function is what controls the wait



来源:https://stackoverflow.com/questions/22583622/php-refreshing-page-and-showing-different-rows-in-a-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!