prev|next with array and session
I want the following issue to be resolved:
page results.php shows the users list (sql query results). This page also stores all user_id to an array $_SESSION['user_ids']. And when I click to view details of a user, user_view.php shows the details of that user.
I want the user details to be changed in user_view.php page when I click on (prev|next) links.
What I have achieved:
- I am using user_view.php?prev=1 for
prev
link and user_view.php?next=1 fornext
link. - I am checking on top of the user_view.php page to see if $_GET['prev'] or $_GET['next'] 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... When I click on prev
the array is cleared and $id_view doesn't have any value.
What I want: Change the $id_vie开发者_StackOverflow中文版w value when I click on prev|next links and display the details in that page.
Can you guys help me??
To pagination in general:
Generally, when paginating, two things are done differently. First, instead of telling the server to go "next" or "back", the server is told to go to a certain index. This servers two purposes. First, it is simpler logic and easier to test -- is it going to the right index? Then it works.
. Second, it lets the user bookmark their current place in the pagination.
Next, most of the time, unless there is substantial reason not to (for example, a query would take half a minute every time it ran), the page is generally displayed based on use of LIMIT (or equivalent) in the DB. It may make for slightly more complicated queries, but it is simpler overall.
Your query might look like this:
SELECT * FROM $tbl_name WHERE user_id IN (
SELECT USER_ID FROM SOME_OTHER_TABLE LIMIT $index, 1
);
Your question specifically:
next
and prev
aren't stored in a serialized array and that is exactly how arrays are stored in $_SESSION between refreshes.
// serialization loses index!
$a = array(1,2,3);
echo next($a); // 2
echo current($a); // 2
// !! resets the index !!
echo current(unserialize(serialize($a))); // 1
Instead of next
and prev
, you may want to consider:
$index = $_SESSION['paginatino_index'];
if (isset($_GET['prev']))
{
if ($_GET['prev'] == "1")
{
$index = ( $index > 1 )? $index - 1: 0;
}
}
// this will never exist simultaneously with $_GET['prev'] unless the user
// does something very bad.
elseif (isset($_GET['next']))
{
if ($_GET['next'] == "1")
{
$count = count( $_SESSION['user_ids'] ) - 1;
$index = ( $index < $count - 1 )? $index + 1: $count;
}
}
// caution! will not work with associative arrays.
$id_view = $_SESSION['user_ids'][ $index ];
Long and boring story time
There was one time that I wanted to keep every page bookmarkable, but that lead to an extremely complicated URL, and I wanted to avoid that. So I serialized a data object and stored that in the database, and stored a parallel version in the individual's session. If a session existed, pagination worked as below. If a session didn't exist, the data object was deserialized and stored in session. The result? I was able to have fifteen extremely long parameters stored with only search_id=1
, making the get parameters search_id=1&page=2
. I thought that clever but I have never wanted for pride...
精彩评论