yandoo Posted September 9, 2007 Share Posted September 9, 2007 Hi, i was hoping for a little help with my pagnation problem. I have followed the basic tutoral in this site and dont seem to be able to get the correct results! I have one table thats records are displayed. The page displays fine except for a couple of things i thnk i need help with . The limit works as only the limited number of records appear. The NEXT and PREVIOUS are there....butNeither of them are hyperlinked. By looking at the code it suggests that the If statement isnt parsing because the Else part is displaying the NEXT and PREVIOUS unlinked as inteded if, if statment couldnt complete. Please see code below: if(($totalrows - ($limit * $page)) > 0){ $pagenext = $page++; echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>"); }else{ echo("NEXT".$limit); } This happens on both the NEXT and PREVIOUS parts....And the actual page numbers that are supposed to be hyperlinked are not either!!! What am i missing here, please help me!!! Below is full code: <?php $limit = 5; // Sets how many results shown per page $query_count = "SELECT count(*) FROM guest_book"; // Sets what we want to pull from the database $query = mysql_query($query_count); // Pulls what we want from the database $totalrows = mysql_num_rows($query); // This counts the number of users if(empty($page)){ $page = 1; } $limitvalue = $page * $limit - ($limit); // Ex: (2 * 25) - 25 = 25 <- data starts at 25 $query=mysql_query("select * from guest_book LIMIT $limitvalue, $limit"); //$result = mysql_query($query) or die("Error: " . mysql_error()); echo "<table border='0' class=\"bot\" width='400' cellspacing='0' cellpadding='0' align=center >"; // Selects all the data from table. // mysql_error() will print an error if one occurs. /* Tip: The MySQL LIMIT value syntax is as follows: LIMIT $row_to_start_at, $how_many_rows_to_return */ while($nt=mysql_fetch_array($query)){ $dt=date("m/d/y",$nt['date']); // formating the date $message=nl2br($nt['message']); // this will change the line breaks to html line breaks echo "<tr bgcolor= #F8F8F8><td width='20'> <font face=\"Verdana, Arial\" size=\"1\" color=\"#505050\"><b>Name: </b></font></td><td width='280'class=\"style1\"><font face=\"Verdana, arial\" size=\"2\">$nt[name]</font></td></tr>"; echo "<tr bgcolor= #F8F8F8><td width='30'class=\"style1\"><font face=\"Verdana, arial\" size=\"1.5\" color=\"#505050\"><b>Email: </b></font></td><td width='150' class=\"style1\"> <font face=\"Verdana, arial\" size=\"2\"> $nt[email]</font></td></tr>"; echo "<tr bgcolor= #F8F8F8><td width='20' class=\"style1\"><font face=\"Verdana, arial\" size=\"1.5\" color=\"#505050\"><b>Web: </b></font></td><td width='70'class=\"style1\"><font face=\"Verdana, arial\" size=\"2\"> $nt[url]</font></td></tr>"; echo "<tr bgcolor= #F8F8F8><td width='30'class=\"style1\"><font face=\"Verdana, arial\" size=\"1.5\" color=\"#505050\"><b>Date: </b></font></td><td width='150'class=\"style1\"><font face=\"Verdana, arial\" size=\"2\">$dt</font></td></tr>"; echo "<br>"; echo"<tr><td colspan=4</td></tr>"; echo "<tr><td colspan=4></td></tr>"; echo "<tr bgcolor= #eeeeee><td colspan=4 class=\"style1\"> <font face=\"Verdana, arial\" size=\"2\"> $message </font></td></tr>"; echo "<tr><td colspan=4></td></tr>"; echo "</tr> \n"; echo "<table border='0' class=\"bot\" width='400' cellspacing='0' cellpadding='0' align=center >"; } echo "</table>"; echo "<center><font face='Verdana' size='2'><a href='entry.html'>Add entry</a></center><br><br><br>"; ?> </span> </p> </td> </tr> </table> <p> </p> <p class="style1"> </p></td> <td width="19%" align="center" valign="top"> </td> <td> </td> </tr> </table> <?php if($page != 1){ $pageprev = $page--; // Fancy way of subtracting 1 from $page // while($row = mysql_fetch_array($result)){ echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> "); }else{ echo("PREV".$limit." "); // If we're on page 1, PREV is not a link } $numofpages = $totalrows / $limit; for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); } } if(($totalrows % $limit) != 0){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); } } if(($totalrows - ($limit * $page)) > 0){ $pagenext = $page++; echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>"); echo("<a href=\"index.html\">the link</a> "); }else{ echo("NEXT".$limit); } ?> </td> </tr> <tr> <td height="107" align="left" valign="top"> </td> </tr> </table> <p> </p></td> </tr> </table> </body> Any help much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/68635-pagnation-help/ Share on other sites More sharing options...
AdRock Posted September 10, 2007 Share Posted September 10, 2007 This is a pagination script I found ages ago but can't remember where <?php $webpage = basename(news); function pagination_one($total_pages,$page){ global $webpage; echo '<div class="page_numbers"> <ul>'; if($total_pages!=1){ for ($i=1;$i<$total_pages+1;$i++){ if($i==$page){ echo '<li><a class="current">'.$i.'</a></li> '; } else{ echo '<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a></li> '; } } echo '</ul> </div>'; } } function pagination_two($total_pages,$page){ global $webpage; echo '<div class="page_numbers"> <ul>'; if($page>"1"){ echo '<li class="current"><a href="'.$webpage.'?page=1">First</a></li> <li class="current"><a href="'.$webpage.'?page='.($page-1).'">Prev</a></li> '; } if($total_pages!=1){ for ($i=1;$i<$total_pages+1;$i++){ if($i==$page){ echo '<li><a class="current">'.$i.'</a></li> '; } else{ echo '<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a></li> </li>'; } } } if(($page >="1")&&($page!=$total_pages)){ echo '<li class="current"><a href="'.$webpage.'?page='.($page+1).'">Next</a></li> <li class="current"><a href="'.$webpage.'?page='.$total_pages.'">Last</a></li> '; } echo '</ul> </div>'; } function pagination_three($total_pages,$page){ global $webpage; echo '<div class="page_numbers"> <ul>'; if($page>"1"){ echo '<li class="current"><a href="'.$webpage.'?page=1">First</a></li> <li class="current"><a href="'.$webpage.'?page='.($page-1).'">Prev</a></li> '; } if($total_pages!=1){ $maximum_links = 10; if($page>=$maximum_links){ $maximum_links=$page; } for ($i=1;$i<$maximum_links+1;$i++){ if($i==$page){ echo '<li><a class="current">'.$i.'</a></li> '; } else{ echo '<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a></li> '; } } } if(($page >="1")&&($page!=$total_pages)){ echo '<li class="current"><a href="'.$webpage.'?page='.($page+1).'">Next</a></li> <li class="current"><a href="'.$webpage.'?page='.$total_pages.'">Last</a></li> '; } echo '</ul> </div>'; } function pagination_four($total_pages,$page){ global $webpage; $max_links = 11; $max = 6; $shift = 5; $h=1; if($total_pages>$max_links){ if(($page>=$max_links-$shift)&&($page<=$total_pages-$shift)){ $max_links = $page+$shift; $h=$max_links-$max; } if($page>=$total_pages-$shift+1){ $max_links = $total_pages+1; $h=$max_links-$max; } } else{ $h=1; $max_links = $total_pages+1; } echo '<div class="page_numbers"> <ul>'; if($page>"1"){ echo '<li class="current"><a href="'.$webpage.'?page=1">First</a></li> <li class="current"><a href="'.$webpage.'?page='.($page-1).'">Prev</a></li> '; } if($total_pages!=1){ for ($i=$h;$i<$max_links;$i++){ if($i==$page){ echo '<li><a class="current">'.$i.'</a></li> '; } else{ echo '<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a></li> '; } } } if(($page >="1")&&($page!=$total_pages)){ echo '<li class="current"><a href="'.$webpage.'?page='.($page+1).'">Next</a></li> <li class="current"><a href="'.$webpage.'?page='.$total_pages.'">Last</a></li> '; } echo '</ul> </div>'; } function pagination_five($total_pages,$page){ global $webpage; $max_links = 10; $h=1; if($page>$max_links){ $h=(($h+$page)-$max_links); } if($page>=1){ $max_links = $max_links+($page-1); } if($max_links>$total_pages){ $max_links=$total_pages+1; } echo '<div class="page_numbers"> <ul>'; if($page>"1"){ echo '<li class="current"><a href="/'.$webpage.'/1">First</a></li> <li class="current"><a href="/'.$webpage.'/'.($page-1).'">Prev</a></li> '; } if($total_pages!=1){ for ($i=$h;$i<$max_links;$i++){ if($i==$page){ echo '<li><a class="current">'.$i.'</a></li>'; } else{ echo '<li><a href="/'.$webpage.'/'.$i.'">'.$i.'</a> </li>'; } } } if(($page >="1")&&($page!=$total_pages)){ echo '<li class="current"><a href="/'.$webpage.'/'.($page+1).'">Next</a></li> <li class="current"><a href="/'.$webpage.'/'.$total_pages.'">Last</a></li> '; } echo '</ul> </div> '; } function pagination_six($total_pages,$page){ global $webpage; $max_links=10; $max_links_marker = $max_links+1; $h=1; $link_block=(($page-1)/$max_links); if(($page>=$max_links_marker)&&(is_int($link_block))){ $max_links_marker=$page+$max_links; $h=$max_links_marker-$max_links; $prev=$h-1; } elseif(($page>=$max_links_marker)&&(!is_int($link_block))){ $round_up=ceil($link_block); $new_top_link = $round_up*$max_links; $max_links_marker=$new_top_link+1; $h=$max_links_marker-$max_links; $prev=$h-1; } if($max_links_marker>$total_pages){ $max_links_marker=$total_pages+1; } echo '<div class="page_numbers"> <ul>'; if($page>"1"){ echo '<li class="current"><a href="'.$webpage.'?page=1">First</a></li> <li class="current"><a href="'.$webpage.'?page='.($page-1).'">Prev</a></li>'; } $prev_start = $h-$max_links; $prev_end = $h-1; if($prev_start <=1){ $prev_start=1; } $prev_block = "Pages $prev_start to $prev_end"; if($page>$max_links){ echo '<li class="current"><a href="'.$webpage.'?page='.$prev.'">'.$prev_block.'</a></li>'; } if($total_pages!=1){ for ($i=$h;$i<$max_links_marker;$i++){ if($i==$page){ echo '<li><a class="current">'.$i.'</a></li>'; } else{ echo '<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a></li>'; } } } else{ echo '<li><a class="current">'.$page.'</a>'; } $next_start = $max_links_marker; $next_end = $max_links_marker+$max_links; if($next_end >=$total_pages){ $next_end=$total_pages; } $next_block = "Pages $next_start to $next_end"; if($total_pages>$max_links_marker-1){ echo '<li class="current"><a href="'.$webpage.'?page='.$max_links_marker.'">'.$next_block.'</a></li>'; } if(($page >="1")&&($page!=$total_pages)){ echo '<li class="current"><a href="'.$webpage.'?page='.($page+1).'">Next</a></li> <li class="current"><a href="'.$webpage.'?page='.$total_pages.'">Last</a></li>'; } echo '</ul> </div>'; } ?> The css .page_numbers { width: 600px; padding: 5px 0px; float:left; clear:left; margin:0 auto; } .page_numbers ul { margin: 0 auto; list-style-type: none; padding: 0px; text-align: center; } .page_numbers li { display: inline; float: left; margin:1px; background: #a7a7a7; width:25px; } .page_numbers li.current{ width:50px; } .page_numbers li a { background: #fff; border: 1px solid #a7a7a7; padding: 1px; text-decoration: none; color: #000; font:bold 8px verdana,sans-serif; display:block; } .page_numbers a.current, .page_numbers li a:hover { background: #a7a7a7; color: #fff; } and an example on how to use it (used on my site) <h2>News / Events</h2><hr /> <div> <?php include "includes/connection.php"; $webpage = basename(news); //could be news.php but i use mod rewrite function pagination_five($total_pages,$page){ global $webpage; $max_links = 10; //max links per page $h=1; if($page>$max_links){ $h=(($h+$page)-$max_links); } if($page>=1){ $max_links = $max_links+($page-1); } if($max_links>$total_pages){ $max_links=$total_pages+1; } if($total_pages>1){ echo '<div class="page_numbers"><ul>'; if($page>"1"){ echo '<li class="current"><a href="/'.$webpage.'/1">First</a></li> <li class="current"><a href="/'.$webpage.'/'.($page-1).'">Prev</a></li> '; } if($total_pages!=1){ for ($i=$h;$i<$max_links;$i++){ if($i==$page){ echo '<li><a class="current">'.$i.'</a></li>'; } else{ echo '<li><a href="/'.$webpage.'/'.$i.'">'.$i.'</a> </li>'; } } } if(($page >="1")&&($page!=$total_pages)){ echo '<li class="current"><a href="/'.$webpage.'/'.($page+1).'">Next</a></li> <li class="current"><a href="/'.$webpage.'/'.$total_pages.'">Last</a></li> '; } echo '</ul></div>'; } } $result = mysql_query("Select count(*) from news WHERE archived='n'") or die (mysql_error()); $numrows = mysql_fetch_row($result); if(isset($_GET['pagenum'])?$page = $_GET['pagenum']:$page = 1); $entries_per_page = 1; $total_pages = ceil($numrows[0]/$entries_per_page); $offset = (($page * $entries_per_page) - $entries_per_page); //after we have $total_pages and $page, we can include the //pagination style wherever we want on the page. //so far there is no output from the script, so we could have //pagination before or after the pages results //before the results $result = mysql_query("SELECT id,title,content, DATE_FORMAT(time, '%W %D %M %Y') as date, photo, alternate FROM news WHERE archived='n' ORDER BY id DESC LIMIT $offset,$entries_per_page"); if(!$result) die(mysql_error()); $err = mysql_num_rows($result); if($err == 0) die("No matches met your criteria."); while($row=mysql_fetch_array($result)){ $title = htmlentities(strtoupper($row['title'])); $content = htmlentities($row['content']); if (!$row['photo']){ echo "<div id='right'><h3>".$title."</h3><p class='italic'>Date added: ".$row['date']."</p><p>".nl2br($content)."</p><br /></div>\n"; } else { echo "<div id='right'><h3>".$title."</h3><p class='italic'>Date added: ".$row['date']."</p><p><img src='/images/large/".$row['photo']."' alt='".$row['alternate']."' class='right' />".nl2br($content)."</p><br /></div>\n"; } } //or after the results pagination_five($total_pages,$page); ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/68635-pagnation-help/#findComment-345105 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.