Pagination required to limit my .csv file result on my PHP Page
csv, php
Solution
Basically you want to have a variable that stands for the page, and then calculate beginning and ending values. Then only output the date if it is between those ranges. Please excuse the code, I am a little rusty with 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);
?>
Can't test this now. Let me know if you run into problems.
Problem
In my csv file ("food.csv"), the data is stored in the order of -ITEM,DESC,QTY,RATE,TYPE. When I use the array to find the item code, the entire results are coming in one page. What I need is the result should be limited to 5 case per page along with " NEXT /BACK". Can any body help me please? other wise suggest any other option please. The PHP file is given 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); ?> ``` The entire results are coming ( 12 cases) in a single page like: Fruit Name: MANGO Quantity Available: 10 Kgs @ 500 USD each. Item code: 100 (Stock: OLD) Fruit Name: APPALE Quantity Available: 12 Kgs @ 300 USD each. Item code: 100 (Stock: NEW) Fruit Name: MANGO Quantity Available: 5 Kgs @ 650 USD each. Item code: 100 (Stock: NEW)