PHP permanent variable, but changeable?
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开发者_如何学C will grab that variable, so it is not re-set?
Excuse my noobiness. Hopefully this makes sense
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
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.
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).
精彩评论