Jump to content

Recommended Posts

I currently have an array of quotes that I rotate throughout my site. I want to have on epage to list them all. I currently use this:

    echo '<table class="data_table">';
    foreach($quotes as $quote) {
        static $i = 0;
        $row = ($i % 2 == 1) ? ' class="even"' : '';
        echo '<tr'.$row.'><td>'.$quote.'</td></tr>'."\n";
        $i++;
    }
    echo '</table>';

Which works fine except that I have a lot of quotes. I want to paginate the array with, say, 15 quotes per page.

 

I've searched Google but I can only find classes and snippets. When I do find a tutorial, often it doesn't explain things very well or it is geared towards MySQL database pagination, which confuses me to no end.

 

I'm wondering if someone has a link to a tutorial or can explain pagination. Thanks!

Link to comment
https://forums.phpfreaks.com/topic/162315-solved-paginating-an-array/
Share on other sites

I figured it out:

<?php

$numquotes = count($quotes);
$quotesperpage = 15;
$totalpages = ceil($numquotes / $quotesperpage);

if(isset($_GET['pagenum']) && is_numeric($_GET['pagenum'])) {
	$page = (int) $_GET['pagenum'];
} else {
	$page = 1;
}

if($page > $totalpages) {
	$page = $totalpages;
}
if($page < 1) {
	$page = 1;
}

$start = ($page - 1) * $quotesperpage;
$stop = $start + $quotesperpage;
$currentquote = $start;

echo '<ul id="pagination" style="margin-bottom: 1.4em;">'."\n";

if($page > 1) {
	$previous = $page - 1;
	echo '<li class="pagination-prev"><a href="'.$url.'/quotes/page/'.$previous.'">Previous</a></li>'."\n";
}

if($page != $totalpages) {
	$next = $page + 1;
	echo '<li class="pagination-next"><a href="'.$url.'/quotes/page/'.$next.'">Next</a></li>'."\n";
}

echo '</ul>'."\n",'<table class="data_table">'."\n";

foreach($quotes as $quote) {
	static $i = 0;

		if($currentquote == $stop || empty($quotes[$currentquote])) {
			break;
		}
		$rowbg = ($i % 2 == 1) ? ' class="even"' : '';
		echo '<tr'.$rowbg.'><td>'.$quotes[$currentquote].'</td></tr>'."\n";

	$currentquote++;
	$i++;
}

echo '</table>'."\n".'<ul id="pagination">'."\n";

if($page > 1) {
	$previous = $page - 1;
	echo '<li class="pagination-prev"><a href="'.$url.'/quotes/page/'.$previous.'">Previous</a></li>'."\n";
}

if($page != $totalpages) {
	$next = $page + 1;
	echo '<li class="pagination-next"><a href="'.$url.'/quotes/page/'.$next.'">Next</a></li>'."\n";
}

echo '</ul>'."\n";
?>

Good job putting that together quickly.

 

You probably don't need to cast $page as an int here:

 

if(isset($_GET['pagenum']) && is_numeric($_GET['pagenum'])) {
  $page = (int) $_GET['pagenum'];

 

As you are already checking if it's numeric above. Not a big point really, just something I thought I would mention.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.