Mundo Posted February 17, 2009 Share Posted February 17, 2009 Basically, I've been working from this tutorial: http://www.spoono.com/php/tutorials/tutorial.php?id=25. There are a few obvious errors but it seems easy enough to implement. However, I can just not seem to get it to work. My code: index.php <? include "tpl/header.tpl"; include "tpl/navigation.tpl"; mysql_connect("localhost","root",""); mysql_select_db("ncfc"); if(!$rowstart) $rowstart=0; $query = sprintf("SELECT * FROM news ORDER BY `news_ID` DESC LIMIT $rowstart, 5"); $result = mysql_query($query); $result2 = mysql_query("SELECT * FROM news"); $numrows = mysql_num_rows($result2); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } while ($row = mysql_fetch_assoc($result)) { include "tpl/news.tpl"; } /* IGNORE THIS FOR NOW if ($rowstart > $numrows) { echo "<a href=\"<? $php_self; ?>?rowstart=<? echo $rowstart-5; ?>\">Previous Page</a>"; } */ if($rowstart+5 < $numrows) { echo $rowstart; $rowstart =+ 5; echo $rowstart; include "tpl/next.tpl"; } include "tpl/footer.tpl"; mysql_free_result($result); mysql_close(); ?> next.tpl <a href="index.php?rowstart=<? echo "$rowstart"; ?>">Next --></A> Can you see what I've done wrong? Quote Link to comment https://forums.phpfreaks.com/topic/145606-next-previous-buttons/ Share on other sites More sharing options...
drisate Posted February 17, 2009 Share Posted February 17, 2009 let me share you my super page by page script ;-) <?php $rowsPerPage=10; if ($_GET ==""){$pageNum = 1;}else{$pageNum = $_GET['page'];} $offset = ($pageNum - 1) * $rowsPerPage; $result = mysql_query("SELECT COUNT(id) AS numrows FROM vip $limit_status") or die (mysql_error()); $row = @mysql_fetch_array($result, MYSQL_ASSOC); $numrows = $row['numrows']; $re_page = $pageNum*$rowsPerPage; $ree_page = $re_page-$rowsPerPage+1; // Table Start print (""); $result = mysql_query("SELECT * FROM vip $where") or die (mysql_error()); while($vip = @mysql_fetch_array($result)) { // Table Middle } // Table end print (""); $dbh2=mysql_connect("localhost", "ffrteam_vip", "g22ppl22") or die ('1 Erreur: '.mysql_error()); mysql_select_db("ffrteam_joom1"); $maxPage = ceil($numrows/$rowsPerPage); $self = "index.php?option=com_interractif"; $nav = ''; for($page = 1; $page <= $maxPage; $page++) { if ($page == $pageNum) { $nav .= " $page "; } else { $nav .= " <a href=\"$self&page=$page\">$page</a> "; } } if ($pageNum > 1) { $page = $pageNum - 1; $prev = " <a href=\"$self&page=$page\">[back]</a> "; $first = " <a href=\"$self&page=1\">[First page]</a> "; } else { $prev = ' '; $first = ' '; } if ($pageNum < $maxPage) { $page = $pageNum + 1; $next = " <a href=\"$self&page=$page\">[Next]</a> "; $last = " <a href=\"$self&page=$maxPage\">[Last page]</a> "; } else { $next = ' '; $last = ' '; } echo "<p align='center'>($ree_page/$re_page from $numrows) Page:<b>$first $prev $nav $next $last</b></p>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/145606-next-previous-buttons/#findComment-764429 Share on other sites More sharing options...
Mundo Posted February 17, 2009 Author Share Posted February 17, 2009 Thanks for the speedy reply but I'd rather fix mine than use yours! I've worked on it a bit more and I've pretty much got it sorted... <? include "tpl/header.tpl"; include "tpl/navigation.tpl"; mysql_connect("localhost","root",""); mysql_select_db("ncfc"); if(!$rowstart) $rowstart=0; $rowstart = $_GET["rowstart"]; $result = mysql_query(sprintf("SELECT * FROM news ORDER BY `news_ID` DESC LIMIT $rowstart, 5")); $numrows = mysql_num_rows(mysql_query("SELECT * FROM news")); echo $numrows; while ($row = mysql_fetch_assoc($result)) { include "tpl/news.tpl"; } if($rowstart+5 < $numrows) { include "tpl/more.tpl"; } include "tpl/footer.tpl"; mysql_free_result($result); mysql_close(); ?> The only problem is when the page is viewed from "index.php" then I get error messages, I have to view the page from "index.php?rowset=0"... It's because of $rowstart = $_GET["rowstart"]; Is there a way I can do the following: IF $_GET["rowstart"] exists THEN $rowstart = $_GET["rowstart"] ELSE $rowstart = 0; I only know how to test if something doesn't exist... (if(!$rowstart)) Quote Link to comment https://forums.phpfreaks.com/topic/145606-next-previous-buttons/#findComment-764452 Share on other sites More sharing options...
Mundo Posted February 17, 2009 Author Share Posted February 17, 2009 Ah I got it, sorry for double post, couldn't find the EDIT button! Thanks for help! Quote Link to comment https://forums.phpfreaks.com/topic/145606-next-previous-buttons/#findComment-764461 Share on other sites More sharing options...
drisate Posted February 17, 2009 Share Posted February 17, 2009 Yeah like this <? include "tpl/header.tpl"; include "tpl/navigation.tpl"; mysql_connect("localhost","root",""); mysql_select_db("ncfc"); if ($_GET["rowstart"]){$rowstart=$_GET["rowstart"];}else{$rowstart="0";} $result = mysql_query(sprintf("SELECT * FROM news ORDER BY `news_ID` DESC LIMIT $rowstart, 5")); $numrows = mysql_num_rows(mysql_query("SELECT * FROM news")); echo $numrows; while ($row = mysql_fetch_assoc($result)) { include "tpl/news.tpl"; } if($rowstart+5 < $numrows) { include "tpl/more.tpl"; } include "tpl/footer.tpl"; mysql_free_result($result); mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/145606-next-previous-buttons/#findComment-764462 Share on other sites More sharing options...
drisate Posted February 17, 2009 Share Posted February 17, 2009 lol guess we posted at the same time hehe Chears bro ;-) Quote Link to comment https://forums.phpfreaks.com/topic/145606-next-previous-buttons/#findComment-764464 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.