I have multiple pages. On each page I have a list of items. So on Page 1, I have Items 1-10. On page 2, I have items 11-20. It is NOT ten items per page but the point is that page 3 should start off with the number immediately following the last number on page 2.
The pages have a simple numeric variable so the counting of items works fine per page but I want it to be set in one place.
I am brand new to php so my question is mostly about how and where to store a variable that would keep track of this item count over multiple files?
Desired behavior:
Let's say Page 1 has 5 items and my counter variable on page 1 sets them to be correctly numbered to:
1.apple
2.grape
3.pear
4.banana
5.orange
But let's say I need to remove banana. When I do so, the current counter on page 1 works fine and sets to:
1.apple
2.grape
3.pear
4.orange
But on page 2 the counter variable is set to 6 and has:
6.mango
7.peach
8.watermelon
I want to set the persistent variable so that when I remove a fruit, it will update not only the current page the removed fruit is on but every page after that. So it would adjust page 2 to:
5.mango
6.peach
7.watermelon
Would sessions make that change permanent or would it disappear with the user's cookie?
<?php
//On the first line of your PHP code
session_start();
//then store your variable in the SESSION array.
$_SESSION['yourCountersNameHere'] = 56;
//You should now have acces to its value from page to page like this
echo $_SESSION['yourCountersNameHere'];
//Should display 56.
?>
you could also store it in a DB. and then check in the DB on every page.
//to connecte
$m_DB = new PDO("mysql:host=".$Host.";dbname=".$DBName."; charset=utf8", $Login, $Password);
//check value in DB
$query = "SELECT * FROM table";
$stmt = $m_DB->prepare($query);
//$param here is empty but normaly it would be a array for all your params like this:
//$param = array('rowname'=>$value, 'rowName2'=>$value2, .........and so on for all the $params you use.);
$getCounter = $stmt->execute($param);
while($Counter = $getCounter->fetch())
{
//Display the value in your MySQLDB.
echo $Counter['table.rowName'];
}
If you use another DB type than MYSQL the code should be fairly the same exepte for a couple of changes you'll have to make.
You can look at http://www.php.net/manual/en/session.examples.basic.php for more information using sessions.
Any variable that is placed in a session will be persistent for the user as long as their cookie doesn't expire.
On each page of your site that will access/change your page variable, add this code at the very beginning:
<?php
//On the very first line of each file using the variable
session_start();
Then for accessing/setting see the following:
<?php
session_start();
// store session data
$_SESSION['views']=1;
?>
<html>
<body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
</body>
</html>
This code will spit out one from the session variable 'views'
To check if something has been set:
<?php
session_start();
if(isset($_SESSION['views']))
//do something
Clearing out the session variables:
<?php
session_start();
if(isset($_SESSION['views']))
unset($_SESSION['views']);
?>
Deleting the session:
<?php
session_destroy();
?>
Best site in the universe : http://www.w3schools.com/php/php_sessions.asp