Jump to content

Cant understand this


thunder708

Recommended Posts

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

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";
//...
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.