ghostrider.k9 Posted May 12, 2008 Share Posted May 12, 2008 Hi i am an amatuer in php programming i need your help dearly. I have a problem. I am trying to create prev and next links for a set of results i obtain thru the mysql database. SQL Statement is "SELECT aid FROM news where aid='1190' order by date ASC,aid ASC ;" the data below in the db So the results will be aid , date 1181, 4/5 1191, 3/5 1182, 4/5 1180, 4/5 1183, 8/5 1181, 4/5 1184, 8/5 1182, 4/5 1185, 8/5 1190, 4/5 1186, 6/5 1186, 6/5 1187, 9/5 1183, 8/5 1188, 9/5 1184, 8/5 1189, 10/5 1185, 8/5 1190, 4/5 1187, 9/5 1191, 3/5 1188, 9/5 1192, 11/5 1189, 10/5 So when i print out i want just display the current id which is 1190 and link "prev" with id 1182 and "next" with 1186. Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/105235-php-and-mysql-prev-and-next-link-for-current-record/ Share on other sites More sharing options...
beboo002 Posted May 12, 2008 Share Posted May 12, 2008 you should work with paging if u use "order by date ASC,aid ASC" here is paging code <? //how many record show per page $rowsPerPage = 1; // by default we show first page $pageNum = 1; // if $_GET['page'] defined, use it as page number if(isset($_GET['page'])) { $pageNum = $_GET['page']; } // counting the offset $offset = ($pageNum - 1) * $rowsPerPage; $query = " SELECT aid FROM news order by date ASC,aid ASC LIMIT $offset, $rowsPerPage"; $result = mysql_query($query) or die('Error, query failed'); // print the random numbers while($row = mysql_fetch_array($result)) { echo $row['aid'] . '<br>'; } // how many rows we have in database $query = "SELECT COUNT(aid) AS numrows FROM news "; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result, MYSQL_ASSOC); $numrows = $row['numrows']; // how many pages we have when using paging? $maxPage = ceil($numrows/$rowsPerPage); // print the link to access each page $self="index.php"; $nav = ''; for($page = 1; $page <= $maxPage; $page++) { if ($page == $pageNum) { $nav .= " $page "; // no need to create a link to current page } else { $nav .= " <a href=\"$self&page=$page\">$page</a> "; } } if ($pageNum > 1) { $page = $pageNum - 1; $prev = " <a href=\"$self&page=$page\">[Prev]</a> "; $first = " <a href=\"$self&page=1\">[First Page]</a> "; } else { $prev = ' '; // we're on page one, don't print previous link $first = ' '; // nor the first page link } 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 = ' '; // we're on the last page, don't print next link $last = ' '; // nor the last page link } // print the navigation link echo $first . $prev . $nav . $next . $last; ?> Link to comment https://forums.phpfreaks.com/topic/105235-php-and-mysql-prev-and-next-link-for-current-record/#findComment-538860 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.