The pagination needed to limit my result to a .csv file in my PHP page

In my csv file ("food.csv") the data is stored in the order -ITEM, DESC, QTY, RATE, TYPE. When I use an array to find the element code, all the results are displayed on one page. What I need is a result that should be limited to 5 cases per page along with "NEXT / BACK". Can anyone help me out? other wise men offer any other option, please. The PHP file is shown below.

<?php
$handle = fopen("food.csv","r")or die("file dont exist");
$output = '  ';
while (!feof($handle )){
    $data = fgetcsv($handle, 4096, ",");
        if($data[0] ==100){
        $output .= sprintf( "<b>Fruit Name: %s.   </b><br>",  $data[1]);
        $output .= sprintf( "Quantity Avaliable:  %d  Kgs @ %d USD each.   <br>",  $data[2], $data[3]);
        $output .= sprintf( "Item code:  %d (Stock:  %s) <br><hr><br>", $data[4],$dara[0]);   
        }
}
echo $output;
fclose($handle);
?>

All results are approximated (12 cases) on one page, for example:

Fruit name: MANGO Available quantity: 10 kg for 500 US dollars each. Product Code: 100 (Stock: OLD)

Fruit Name: APPALE Amount Available: 12 Kgs @ 300 USD Each. Product Code: 100 (Stock: NEW)

: MANGO : 5 Kgs @650 USD . : 100 (Stock: NEW)

+4
3

, , . , . , PHP.

<?php
$handle = fopen("food.csv","r")or die("file dont exist");
$output = '  ';
$numPerPage = 5;
$page = $_GET['page'];
$count = 0;
$start = $page * $numPerPage;
$end = ($page + 1) * $numPerPage;
while (!feof($handle )){
    $data = fgetcsv($handle, 4096, ",");
        if($data[0] ==100 && $count < $end && $count >= $start){
        $output .= sprintf( "<b>Fruit Name: %s.   </b><br>",  $data[1]);
        $output .= sprintf( "Quantity Avaliable:  %d  Kgs @ %d USD each.   <br>",  $data[2], $data[3]);
        $output .= sprintf( "Item code:  %d (Stock:  %s) <br><hr><br>", $data[4],$data[0]);

        if($count == $numPerPage) {
            if($page != 0) {
                $output .= '<a href="?page="' . ($page - 1) . '">BACK</a> ';
            }
            if(!feof($handle)) {
                $output .= ' <a href="?page="' . ($page + 1) . '">NEXT</a>';
            }
        }

        $count++
        }
}
echo $output;
fclose($handle);
?>

. , .

+1

-

$fh = fopen('food.csv', 'r');
while ((feof($fh) === false) ){
  $i=1;
  $start = 5;
  $end = 10;
  while ((feof($fh) === false) && $i > $start && $i < $end){
    fgets($fh);
    $i++;
  }
  $line = fgets($fh);
  echo $line;
}
fclose($fh);

,

+1

<?php

    if ( ! empty($_FILES['file']['name']) ) {
        $f = fopen($_FILES['file']['tmp_name'],'r');

        $head = fgetcsv($f,0,',','"');
        $num_per_page = 50;

        $out = array(1 => '<table width="100%" cellspacing="0" cellpadding="1"><tr><th>'. join('</th><th>',$head) .'</th></tr>');
        $k = count($out);

        $i = 0;
        $p = 1;
        $z = -1;
        while( $arr = fgetcsv($f,0,',','"') ) {
            $arr = array_pad($arr,$k,'');
            if ( $i >= $num_per_page ) {
                $i = 0;
                $out[$p] = $out[$p] .'</table>';
                $out[$p] = $out[$p] .'<div style="text-align: right;">';
                if ( $p > 1 ) {
                    $out[$p] = $out[$p] .'<a href="?p='. ($p -1 ) .'">&lt; Previous</a> &nbsp;&nbsp;&nbsp;';
                }
                $out[$p] = $out[$p] .'<a href="?p='. ($p +1 ) .'">Next &gt;</a>';
                $out[$p] = $out[$p] .'</div>';

                $p++;
                $out[$p] = '<table width="100%" cellspacing="0" cellpadding="1"><tr><th>'. join('</th><th>',$head) .'</th></tr>';
            }
            $z *= -1;
            $out[$p] = $out[$p] .'<tr '. ( $z > 0 ? 'class="odd"' : '' ) .'><td>'. join('</td><td>',$arr) .'</td></tr>';
            $i++;
        }
        if ( $i > 0 ) {
            $out[$p] = $out[$p] .'</table>';
        }
        if ( $p > 1 ) {
            $out[$p] = $out[$p] .'<div style="text-align: right;">';
            $out[$p] = $out[$p] .'<a href="?p='. ($p -1 ) .'">&lt; Previous</a> &nbsp;&nbsp;&nbsp;';
            $out[$p] = $out[$p] .'</div>';
        }

        echo '<pre>'. htmlspecialchars('<?php' ."\n"
            .'$arr = '. var_export($out,true) .'; $p = 1; if ( ! empty($_GET[\'p\']) ) $p = intval($_GET[\'p\']); if ( ! array_key_exists($p,$arr) ) $p = 1; echo $arr[$p];'
            . "\n". '?>'
        );

    } else {
?>
    <form method="post" enctype="multipart/form-data">
        CSV File<input type="file" name="file" /> <input type="submit" value="Submit" />
    </form>
<?php
    }

?>
0
source

Source: https://habr.com/ru/post/1528891/


All Articles