Question

 <?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

Was it helpful?

Solution

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;
?>

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top