Reputation: 15
I am looking for help in how to paginate my foreach output. I've looked at other questions and answers and cannot find a solution that works for me or that I can figure out on my own. Right now my code, which is below, outputs everything into table rows. My problem, of course, is that it dumps all data on a single page -- hence the reason I want pagination. I want to paginate for every 11 items on the page. The page is a magazine archive, and there are 11 issues pear year -- so every page is equal to 1 year of our magazine. The first page should host issues 1-11 and page two should host issues 12 through 22, etc. We have 10 years worth of magazine issues. Any help would be greatly appreciated. Thank you!
<table>
<tr>
<?php $col = 0; ?>
<?php foreach (get_terms('term') as $cat) : ?>
<?php if ($col > 0 && $col % 3 == 0): ?>
</tr>
<tr>
<?php endif; ?>
<?php $col++; ?>
<td>
<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><strong><?php echo $cat->name; ?></strong></a><br>
<em><a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><?php echo $cat->description; ?></a></em><br>
<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>
</td>
<?php endforeach; ?>
</tr>
</table>
Upvotes: 0
Views: 2096
Reputation: 15
Here is the code I used to solve my problem:
<?php
$url = $_SERVER["REQUEST_URI"];
$segments = explode('/', $url);
$page = is_numeric($segments[count($segments)-2]) ? $segments[count($segments)-2] : 1;
$next = $page + 1;
$prev = $page - 1;
$issues_per_page = 11;
$lastpage = ceil(wp_count_terms( 'mag') / $issues_per_page) ;
?>
<?php wp_count_terms( 'mag' ); ?>
<table>
<tr>
<?php $col = 0; ?>
<?php foreach (get_terms('mag', array('offset' => ($page - 1) * $issues_per_page, 'number' => $issues_per_page)) as $cat) : ?>
<?php if ($col > 0 && $col % 3 == 0): ?>
</tr>
<tr>
<?php endif; ?>
<?php $col++; ?>
<td>
<a href="<?php echo get_term_link($cat->slug, 'mag'); ?>"><strong><?php echo $cat->name; ?></strong></a><br>
<em><a href="<?php echo get_term_link($cat->slug, 'mag'); ?>"><?php echo $cat->description; ?></a></em><br>
<a href="<?php echo get_term_link($cat->slug, 'mag'); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>
</td>
<?php endforeach; ?>
</tr>
</table>
<?php if ($prev > 0) : ?>
<a href="/mag-archive?page=<?php echo $prev; ?>">Previous</a>
<?php endif ?>
<?php if ($page < $lastpage) : ?>
<a href="/mag-archive?page=<?php echo $next; ?>">Next</a>
<?php endif ?>
Upvotes: 0
Reputation: 140
this may not be what you were looking for or expecting but hope you or someone else finds useful. the following script does everything small configuration required ps_pagination.php You need to include the ps_pagination.php into the pages where you want to use it and then use the following code to populate the information on the paginated pages. Hope its helpful.
db_connection = mysql_connect('localhost', 'user', 'password')or die("cannot connect");
mysql_select_db('database',$conn);
// mysql query
$sql_query="SELECT * FROM example ORDER BY id DESC";
// Create the ps_pagination object here
$pager = new ps_pagination($db_connection,$sql_query,10,5);
//The paginate() function returns a mysql result set
$rs = $pager->paginate();
while($rows = mysql_fetch_assoc($rs)) {
// table to display results here // modify here
echo $rows["title"].'</p>';
echo '<p> '.$rows["post"].'</p>';
echo '<p><span class="style1">By</span>: '.$rows["name"].' ';
echo '<p>Date/Time</span>:'.$rows["datetime"].'</p>';
echo "<BR>";
}
// close mysql connection here
mysql_close();
//Display the full navigation in one go
echo $pager->renderFullNav();
Upvotes: 0
Reputation: 5700
I think this is what you want:
get_terms('term', array('offset' => $page * 11, 'number' => 11));
Where $page
starts at 0 for the first page. If you want page to start at 1, use ($page-1) * 11
.
How you determine what page to show is up to you.
See the get_terms
documentation for more information: http://codex.wordpress.org/Function_Reference/get_terms
Upvotes: 0
Reputation: 197
What framework/ application is this? While I don't understand 100% where the data is coming from, you could try modifying your code to add a break when you hit 11 entries (or 10 when you are starting from 0).
<?php $col = 0; ?>
<?php foreach (get_terms('term') as $cat) : ?>
<?php if ($col > 0 && $col % 3 == 0): ?>
</tr>
<tr>
...
<?php if(10 == $col) { break;}
endforeach; ?>
That will limit this to the first 11 results of your foreach loop. From there, you would have to modify how you select the data so that the second page starts at record 12.
Of course, it may just be easier to use limit syntax on your sql query, eg:
limit 12, 22
Upvotes: 2