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??

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
diafol

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
diafol

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;
?>