thunder708 Posted February 17, 2010 Share Posted February 17, 2010 right i have this code that when run from a link is supposed to search for the next and previous records in the data base function next_message($id) { global $userrow, $user, $controlrow; $sql = doquery("select id from {{table}} where id<'$id' AND UserTo='$userrow[id]' order by id limit 1", "mail"); if($inf = mysql_fetch_array($sql)){ // A next record is found $next = $inf["id"]; } header("Location: mail.php?do=read:".$next); } function previous_message($id) { global $userrow, $user, $controlrow; $sql = doquery("select id from {{table}} where UserTo='$userrow[id]' AND id>'$id' order by id limit 1", "mail"); if($inf = mysql_fetch_array($sql)){ // A next record is found $previous = $inf["id"]; } header("Location: mail.php?do=read:".$previous); } when clicking previous it happily goes to the previous record but when clicking next the script jumps to the last record for the user. any help would be greatful thanks Link to comment https://forums.phpfreaks.com/topic/192443-cant-understand-this/ Share on other sites More sharing options...
sader Posted February 17, 2010 Share Posted February 17, 2010 I would do it like so via url pass current post id in both cases next/prev and ?do=read_next or do=read_prev the next prev link would look like this <a href='index.php?do=read_prev&id=current_post_id_here'>Preview</a> <a href='index.php?do=read_next&id=current_post_id_here'>Next</a> if($_GET['do'] == 'read_next') { $current = intval($_GET['id']); $sql = "SELECT * FROM posts WHERE id>$current ORDER BY id ASC LIMIT 1"; //... } else if($_GET['do'] == 'read_prev') { $current = intval($_GET['id']); $sql = "SELECT * FROM posts WHERE id<$current ORDER BY id DESC LIMIT 1"; //... } Link to comment https://forums.phpfreaks.com/topic/192443-cant-understand-this/#findComment-1014033 Share on other sites More sharing options...
thunder708 Posted February 17, 2010 Author Share Posted February 17, 2010 Thank you for this help, i cant believe how stupid i was i missed out the ASC and DESC much thanks! Link to comment https://forums.phpfreaks.com/topic/192443-cant-understand-this/#findComment-1014035 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.