Hello Friends,

I want the following issue to be resolved:

1. page results.php shows the users list (sql query results). This page also stores all user_id to an array $_SESSION. And when I click to view details of a user, user_view.php shows the details of that user.

2. I want the user details to be changed in user_view.php page when I click on (prev|next) links.

What I have achieved:

1. I am using user_view.php?prev=1 for `prev` link and user_view.php?next=1 for `next` link.
2. I am checking on top of the user_view.php page to see if $_GET or $_GET is set.

in results.php page following variables were set

$_SESSION['user_ids']=$user_ids; // $user_ids is an array with ids
$_SESSION['first_value']=reset($user_ids);
$_SESSION['last_value']=end($user_ids);

in user_view.php page following checks were being performed

$id_view=$_SESSION['user_id']; // This is to view the first user details and is set after clicking "view" for a user in results.php page

$user_ids=$_SESSION['user_ids']; // array
if (isset($_GET['prev']))
{
	if ($_GET['prev'] == "1")
	{
		if (current($_SESSION['user_ids'])==$_SESSION['first_value'])
		{
			$id_view = $_SESSION['user_value'];
		}
		else
		{
			$id_view = prev($_SESSION['user_ids']);
		}
	}
}
if (isset($_GET['next']))
{
	if ($_GET['next'] == "1")
	{
		if (current($_SESSION['user_ids'])==$_SESSION['last_value'])
		{
			$id_view = $_SESSION['user_value'];
		}
		else
		{
			$id_view = next($_SESSION['user_ids']);
		}
	}
}

// GET THE DETAILS OF THE USER WITH ID $id_view
$result=mysql_query("SELECT * FROM $tbl_name WHERE user_id='$id_view'",$db);

Above code is not working at all...

What I want:
Change the $id_view value when I click on prev|next links and display the details in that page.

Can you guys help me??

Dani AI

Generated

A short diagnosis and a practical fix.

The root cause is the use of PHP’s internal array pointer functions (current/next/prev) across separate HTTP requests. Those pointer functions work only inside a single script execution, so relying on them after redirects/page reloads behaves unreliably. ’s suggestion to dump variables for debugging was spot on, and ’s session-key idea is workable — but a more robust and bookmarkable approach is to compute the current position every request (either from an index saved in session or from an ID passed in the URL).

Recommended pattern (summary):

  • Start each script with session_start(). Ensure the results page stores a stable, ordered list of IDs in the session (or re-run the same query on each view to rebuild that list).
  • On user_view.php accept either an id (preferred for bookmarking) or an explicit pos/index. Determine the current position with array_search($id, $ids, true) (or use the provided index).
  • Compute prev/next positions with simple arithmetic, clamp to bounds, then pick the ID from the array and fetch the record with a prepared statement (PDO or mysqli). Avoid deprecated mysql_* calls and always validate/cast IDs to integers.
  • If the session-stored ID list can change (rows added/removed), detect a missing ID (array_search returns false) and refresh the list or fall back to the first element.

Example logic (conceptual):

<?php
session_start();
// $ids = array_values($_SESSION['user_ids'] ?? []);
// $currentId = isset($_GET['id']) ? (int)$_GET['id'] : ($ids[0] ?? null);
// $pos = ($currentId !== null) ? array_search($currentId, $ids, true) : 0;
// adjust $pos for prev/next and fetch $ids[$pos] via prepared statement
?>

Troubleshooting notes: var_dump($_SESSION['user_ids']) and $_GET during development; use array_values() to ensure sequential indexes; handle empty or stale sessions gracefully; prefer passing id in the URL for predictable, bookmarkable links. This resolves the pointer persistence problem while keeping navigation simple and secure.

Recommended Answers

All 6 Replies

cant go into too much detail on your code, but... debug.

if (isset($_GET['prev']))
  echo $_GET['prev'];
  if ($_GET['prev'] == "1") {
      echo "true";
  } else {
echo "false
}

bad post....

discover your values in your variables and see if what you expect is there.

Member Avatar for Member #120589

prev, current etc don't store across page refreshes. you can store the values in session vars, but you can't move the pointer without a bit of trickery. key() would probably be your best bet. Still, v. tricky.

prev, current etc don't store across page refreshes. you can store the values in session vars, but you can't move the pointer without a bit of trickery. key() would probably be your best bet. Still, v. tricky.

I am already playing with key now :)

Can you share some knowledge on how keys will be handy in this situation? It would be really helpful.

Member Avatar for Member #120589

key() will store the pointer or 'key position'. I don't know of any easy way to set the pointer on page refresh. I had a play with key loops using next as an 'incrementer' followed by a single prev. It worked, but it's really messy and you could end up with an infinite loop if there's a change to the underlying array.

Here's a session-based bit of code - it's raw and uses the actual session vars themselves (not good), but it'll give you the idea:

<?php
//variables
$allowed = array("first","prev","last","next");
$f = '<a href="?s=first">First</a>';
$p = '<a href="?s=prev">Previous</a>';
$n = '<a href="?s=next">Next</a>';
$l = '<a href="?s=last">Last</a>';

//check or initialise session vars
if(!isset($_SESSION['ids'])){
	$_SESSION['ids'] = range(1,20); //place your default array here if req'd
}
if(!isset($_SESSION['key'])){
	$_SESSION['key'] = 0; //place current key at start
}

//check action - only actions where s=first, s=next etc. allowed
//protects against silliness in querystring by user
//although may be useless for bookmarking
if(isset($_GET['s']) && in_array($_GET['s'],$allowed)){
	switch($_GET['s']){
		case "first":
			$_SESSION['key'] = 0;
			break;
		case "prev":
			if($_SESSION['key']>0)$_SESSION['key']--;
			break;
		case "next":
			if($_SESSION['key']<count($_SESSION['ids']) - 1)$_SESSION['key']++;
			break;
		case "last":
			$_SESSION['key'] = count($_SESSION['ids']) - 1;
			break;		
	}	
}
//output the value
echo $_SESSION['ids'][$_SESSION['key']] . "<br />";
//output the navigation
if($_SESSION['key'] == 0){
	$out = "$n | $l";
}elseif($_SESSION['key'] == count($_SESSION['ids']) - 1){
	$out = "$f | $p";	
}else{
	$out = "$f | $p | $n | $l";
}
echo $out;
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.