Pagination file csv

Hi all, i’m a newbie in Php
I have to add a pagination to this page that goes to read a csv file with too many lines…For example to change every 100 records
Thank u in advance

This is the code:

			<?php

			$csv = array_map('str_getcsv', file('data/galleries2.csv')); //print_r($csv);

			if ($csv):

/*
[0] => <!-- Website
[1] => <!-- Date
[2] => <!-- Title
[3] => <!-- Url
[4] => <!-- Urlimage
*/

				?>

				<div class="content-loop grid clearfix">

					<?php for ($x = 1; $x < count($csv)-1; $x++): ?>
					

						<div class="grid-item">
							<article class="hentry">

								<div class="entry-image clearfix">
									<a target="_blank" href="<?php echo $csv[$x][3]; ?>"><img src="<?php echo $csv[$x][4]; ?>" alt=""/></a>
								</div>

								<header class="entry-header">
									<h2 class="entry-title"><a target="_blank" href="<?php echo $csv[$x][3]; ?>"><?php echo $csv[$x][2]; ?></a></h2>
								</header>

						
								<div class="entry-footer clearfix">
									<div class="entry-meta">
										<span class="entry-category">  <h6><?php echo $csv[$x][0]; ?></h6></a></span>
									
									</div>
								</div>

							</article>
						</div>

					<?php endfor; ?>

				</div>

			<?php endif; ?>

Use array_slice(). The offset and length parameters are the same that you would use in a traditional database pagination script.

Pagination need a few steps

  1. you will need a GET variabele like ?page=2
  2. you will need to know the total amount of rules in the CSV file (for example 650)
  3. you need to know how many rules may be displayed on one page (for example 100)
  4. Then you need to calculate which rule to start to display on page 2: (2 -1) * 100
  5. Don’t add more then 100 rules on the page
  6. Draw navigation links. To do so you will need to calculate the total amount of pages which is ceil(650/100). Each link will have another page=? variabele. So you will get something like
<ul class="pagination">
    <li><a href="mypage.php?page=1">1</a></li>
    <li><a href="mypage.php?page=2">2</a></li>
    <li><a href="mypage.php?page=3">3</a></li>
    <li><a href="mypage.php?page=4">4</a></li>
    <li><a href="mypage.php?page=5">5</a></li>
    <li><a href="mypage.php?page=6">6</a></li>
    <li><a href="mypage.php?page=7">7</a></li>
</ul>

When the page GET variabele is missing you would like to set page on 1:

$page = 1; // default value

if(isset($_GET['page']) && intval($_GET['page']) > 0) {
    $page = intval($_GET['page'];
}

And make some calculations:

$rowsPerPage = 100;
$totalRows = ....  // get the total of lines in the CSV
$numberOfPages = ceil(totalRows / $rowsPerPage);

// if the page number is higher then there are pages then go to the last page
if($page > $numberOfPages) {
   $page = $numberOfPages;
}

$startPosition = ($page - 1) * $rowsPerPage; 
1 Like

for readability and modularity i would recommend to encapsulate that into a function

Sponsor our Newsletter | Privacy Policy | Terms of Service