0

This is more a question on php theory, and wondering how this scenario would be done..

So I have a gallery which has pagination, all done through reloading the page through $_GET.

I want the ability for a user to change the amount of images are displayed on a page (basically my LIMIT). I have this working, however when they go to the next page, the php reloads, and the pagecount gets reset back to default.

Is there a way to store this variable through $_POST to another page when they choose the page count, and then every time the page re-loads, it will grab that variable, so it is not re-set?

Excuse my noobiness. Hopefully this makes sense

1
  • absolutely possible. just do it, as they say. you could just tack it on to your get request, also Commented Mar 5, 2011 at 1:30

3 Answers 3

2

I believe you are looking for session variables

<?php
session_start(); 
$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>

http://www.tizag.com/phpT/phpsessions.php

Sign up to request clarification or add additional context in comments.

3 Comments

Storing this kind of thing in a session variable will prevent you from keeping multiple browser windows open, with different paging.
So how can I update this session variable? Through a get? like if I wanted my <option> dropdown value to update the session variable, how would this be done? Thanks!
@Chris On before page: <form method=GET"> <select name="views"/> </form> On after page: $_SESSION['views'] = $_GET['views'];
1

What you want are PHP sessions, which

...consists of a way to preserve certain data across subsequent accesses

See the PHP docs for more information.

Comments

0

Whenever you make a request to the server, pass along all the variables that you need. So if you're changing the limit by submitting a form, pass along the page number as a hidden form field:

<select name="limit">...</select>
<input type="hidden" name="pageNum" value="<?= htmlspecialchars($pageNum) ?>" />

Or if you're changing the limit with a link, pass along the page number as another URL argument:

<a href="?limit=10&pageNum=<?= htmlspecialchars($pageNum) ?>">Limit 10</a>

Then you can read it on the server using $_POST["pageNum"] or $_GET["pageNum"].

I don't recommend storing things like this in the session. If you do, you'll prevent people from having multiple windows open to different pages. It's best to pass everything in the request (i.e. the form or link).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.