Altec Posted June 16, 2009 Share Posted June 16, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/162315-solved-paginating-an-array/ Share on other sites More sharing options...
Maq Posted June 16, 2009 Share Posted June 16, 2009 I'm wondering if someone has a link to a tutorial or can explain pagination. Thanks! There's one on phpfreaks but it's database driven, which is worth learning and better in the long run anyway. PHP Basic Pagination Quote Link to comment https://forums.phpfreaks.com/topic/162315-solved-paginating-an-array/#findComment-856708 Share on other sites More sharing options...
haku Posted June 16, 2009 Share Posted June 16, 2009 You can adapt the tutorial to use array_slice() to output the section of the array that you want. Quote Link to comment https://forums.phpfreaks.com/topic/162315-solved-paginating-an-array/#findComment-856765 Share on other sites More sharing options...
Altec Posted June 16, 2009 Author Share Posted June 16, 2009 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/162315-solved-paginating-an-array/#findComment-856812 Share on other sites More sharing options...
haku Posted June 16, 2009 Share Posted June 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/162315-solved-paginating-an-array/#findComment-856831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.