ArizonaJohn Posted May 28, 2009 Share Posted May 28, 2009 Hello, I have gotten some code for pagination, and I have gotten it to work for the first page. However, when I click on the links to the next pages, the results are blank. So, apparently the problem is that I am not passing the correct variable to these next pages. The problem is that my output that I'm trying to paginate is a little fancy, and it's hard for me to tell what variable to pass from page to page, and whether or not this variable is an array. My output code is below. This is all kicked off after a user submits a value for the variable "$find" via an HTML form. What should I use as a variable to pass from page to page? Thanks in advance, John $result=mysql_query("SHOW TABLES FROM sand2 LIKE '%$find%'") or die(mysql_error()); if(mysql_num_rows($result)>0){ while($table=mysql_fetch_row($result)){ print "<p class=\"topic\">$table[0]</p>\n"; $r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` ORDER BY effective_vote DESC LIMIT $offset, $rowsperpage"); print "<table class=\"navbar\">\n"; while($row=mysql_fetch_array($r)){ print "<tr>"; print "<td>".'<a href="http://'.$row['site'].'" class="links2">'.$row['site'].'</a>'."</td>"; print "<td class='votes'>".'<span class="votes_count" id="votes_count'.$row['id'].'">'.number_format($effective_vote).'</span>'."</td>"; print "<td class='ballot'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.Vote.'</a>'.'</span>'."</td>"; } print "</tr>\n"; } print "</table>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/159990-pagination-what-is-the-variable-that-i-should-pass-from-page-to-page/ Share on other sites More sharing options...
Alt_F4 Posted May 28, 2009 Share Posted May 28, 2009 effectively you would only need to pass the $rowsperpage variable. each time you move forward a page you would need to add the value of $rowsperpage to $offset before the following query was executed $r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` ORDER BY effective_vote DESC LIMIT $offset, $rowsperpage"); have a look here if you need clarification http://php.about.com/od/mysqlcommands/g/Limit_sql.htm Quote Link to comment https://forums.phpfreaks.com/topic/159990-pagination-what-is-the-variable-that-i-should-pass-from-page-to-page/#findComment-843909 Share on other sites More sharing options...
JonnoTheDev Posted May 28, 2009 Share Posted May 28, 2009 No Your first query should count the total number of rows in the table. Then from a page number value you can work out the offset. So the only parameter needed is the page number i.e. articles.php?p=1 , articles.php?p=2 , etc i.e. If you want to display 15 records per page $rowsPerPage = 15; $numberOfPages = ceil($numberOfRows / $rowsPerPage); $currentPageNumber = is_numeric($_GET['p']) ? $_GET['p'] : 1; $offset = $rowsPerPage * ($currentPageNumber - 1)); // add to the sql query $sqlQuery .= " LIMIT ".$offset.", ".$rowsPerPage; Quote Link to comment https://forums.phpfreaks.com/topic/159990-pagination-what-is-the-variable-that-i-should-pass-from-page-to-page/#findComment-843980 Share on other sites More sharing options...
redarrow Posted May 28, 2009 Share Posted May 28, 2009 neil.johnson please extend the example with links loving it. cheers. example. <=prev next=> to the current example, will print it as a example and learn it sounds easy and grate your way. Quote Link to comment https://forums.phpfreaks.com/topic/159990-pagination-what-is-the-variable-that-i-should-pass-from-page-to-page/#findComment-843984 Share on other sites More sharing options...
JonnoTheDev Posted May 28, 2009 Share Posted May 28, 2009 You could do with a function i.e << 1 2 3 4 5 6 7 8 9 10 ... >> <?php // display the pagination links function displayLinks($numberOfPages, $maxPageLinks, $currentPageNumber, $filename) { $displayLinksString = false; if($currentPageNumber > 1) { $displayLinksString .= '<a href="'.$filename. '?p='.($currentPageNumber - 1)).'">'."<<".'</a> '; } $curWindowNum = intval($currentPageNumber / $maxPageLinks); if($currentPageNumber % $maxPageLinks) { $curWindowNum++; } $maxWindowNum = intval($numberOfPages / $maxPageLinks); if($numberOfPages % $maxPageLinks) { $maxWindowNum++; } if($curWindowNum > 1) { $displayLinksString .= '<a href="'.$filename.'?p='.(($curWindowNum - 1) * $maxPageLinks)).'">...</a>'; } for($jumpToPage = 1 + (($curWindowNum - 1) * $maxPageLinks); ($jumpToPage <= ($curWindowNum * $maxPageLinks)) && ($jumpToPage <= $numberOfPages); $jumpToPage++) { if($jumpToPage == $currentPageNumber) { $displayLinksString .= ' <b>'.$jumpToPage.'</b> '; } else { $displayLinksString .= ' <a href="'.$filename.'?p='.$jumpToPage.'">'.$jumpToPage.'</a> '; } } if($curWindowNum < $maxWindowNum) { $displayLinksString .= '<a href="'.$filename.'?p='.(($curWindowNum) * $maxPageLinks + 1)).'">...</a> '; } if(($currentPageNumber < $numberOfPages) && ($numberOfPages != 1)) { $displayLinksString .= ' <a href="'.$filename. '?p='.($currentPageNumber + 1)).'">'. ">>".'</a> '; } return $displayLinksString; } $rowsPerPage = 15; $maxPageLinks = 10; $numberOfPages = ceil($numberOfRows / $rowsPerPage); $currentPageNumber = is_numeric($_GET['p']) ? $_GET['p'] : 1; // display pagination links print displayLinks($numberOfPages, $maxPageLinks, $currentPageNumber, "articles.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/159990-pagination-what-is-the-variable-that-i-should-pass-from-page-to-page/#findComment-843992 Share on other sites More sharing options...
redarrow Posted May 28, 2009 Share Posted May 28, 2009 whale looks good but it complected. if you get time can you do a step via step example for us all for pag. please make it easy to understand (babyish) as i am a very slow learner and thank you. Quote Link to comment https://forums.phpfreaks.com/topic/159990-pagination-what-is-the-variable-that-i-should-pass-from-page-to-page/#findComment-843998 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.