LukeBateson Posted June 1, 2011 Share Posted June 1, 2011 I can't get my head around it, no matter how many tutorials I read.. So would anyone be able to put this code into a pagination of 3 per page? Many Thanks <?php if(!$_GET['id']) { $query = "SELECT * FROM news where published = '1' ORDER by id DESC"; $result = mysql_query($query) or die(mysql_error()); $news = mysql_fetch_assoc($result); ?> <?php do { ?> <table width="590" border="0" cellspacing="0" cellpadding="0"> <tr> <td colspan="2"><h2><? echo $news['title']; ?></h2></td> </tr> <tr> <td width="398"><? echo $news['story']; ?></td> <td width="192"><img src="<? echo $news['image']; ?>" alt="<? echo $news['shortstory']; ?>" width="192" height="108" /></td> </tr> <tr> <td colspan="2"><i><br /> Posted by <? echo $news['author']; ?> on <? echo $news['date']; ?></i></td> </tr> </table><br /> <?php } while ($news = mysql_fetch_assoc($result)); } ?> Link to comment https://forums.phpfreaks.com/topic/238148-pagination-help/ Share on other sites More sharing options...
Tenaciousmug Posted June 1, 2011 Share Posted June 1, 2011 This should work: <?php //found out how many rows are in the table $sql = "SELECT COUNT(*) FROM news"; $result = mysqli_query($cxn, $sql); $row = mysqli_fetch_row($result); $num = mysqli_num_rows($result); $numrows = $row[0]; //number of rows to show per page $rowsperpage = 10; //finding total pages $totalpages = ceil($numrows / $rowsperpage); //getting the current page or setting a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { //cast variable as integer $currentpage = (int) $_GET['currentpage']; } else { //default page number $currentpage = 1; } //if current page is greater than total pages if ($currentpage > $totalpages) { //set current page to last page $curentpage = $totalpages; } //if current page is less than the first page if ($currentpage < 1) { //set current page to first page $currentpage = 1; } //offset of the list $offset = ($currentpage - 1) * $rowsperpage; echo "<table width=\"590\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n"; //retrieving news information from table $query = "SELECT * FROM news WHERE published = '1' ORDER BY id DESC LIMIT $offset, $rowsperpage"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysqli_fetch_assoc($result)) { echo "<tr>\n"; echo "\t<td colspan=\"2\"><h2>".$row['title']."</h2></td>\n"; echo "</tr>\n"; echo "<tr>\n"; echo "\t<td width=\"398\">".$row['story']."</td>\n"; echo "\t<td width=\"192\"><img src=\"".$row['image']."\" alt=\"".$row['shortstory']."\" width=\"192\" height=\"108\" /></td>\n"; echo "</tr>\n"; echo "<tr>\n"; echo "\t<td colspan=\"2\"><i><br />\n"; echo "\tPosted by ".$row['author']." on ".$row['date']."</i></td>\n"; echo "</tr>\n"; } echo "\t</table>\n"; //BUILDING PAGINATION LINKS //range of number of links to show $range = 3; echo "\t<p align=\"center\">"; //if not on page one, don't show back links if ($currentpage > 1) { //show link to go back to first page //echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?currentpage=1\"><<</a>"; //get previous page number $prevpage = $currentpage - 1; //show link to go back a previous page echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?currentpage=".$prevpage."\"><</a>"; } //loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { //if its a valid page number if (($x > 0) && ($x <= $totalpages)) { //if we're on the current page if ($x == $currentpage) { //highlight it, but dont make it a link echo "<b>".$x."</b>"; } else { //if not current page //make it a link echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?currentpage=".$x."\">".$x."</a>"; } } } //if not on last page, show forward and last page links if ($currentpage != $totalpages AND $num == 0) { //get next page $nextpage = $currentpage + 1; //echo forward link for next page echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?currentpage=".$nextpage."\">></a>"; //echo forward link for last page //echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?currentpage=".$totalpages."\">>></a>"; } echo "</p>"; ?> Link to comment https://forums.phpfreaks.com/topic/238148-pagination-help/#findComment-1223771 Share on other sites More sharing options...
LukeBateson Posted June 1, 2011 Author Share Posted June 1, 2011 Thanks! That works, except, the ">" arrow doesn't show anytime.. Link to comment https://forums.phpfreaks.com/topic/238148-pagination-help/#findComment-1223782 Share on other sites More sharing options...
LukeBateson Posted June 1, 2011 Author Share Posted June 1, 2011 Oh, Doesn't matter: got it! Thanks! Link to comment https://forums.phpfreaks.com/topic/238148-pagination-help/#findComment-1223785 Share on other sites More sharing options...
Tenaciousmug Posted June 1, 2011 Share Posted June 1, 2011 You're welcome! (: You can change how many results you wan to display per page. Just change the $rowsperpage variable. Link to comment https://forums.phpfreaks.com/topic/238148-pagination-help/#findComment-1223788 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.