How to echo alternatively 2 values on every refresh? - php

I'm writing PHP code and I have two variables $var1 and $var2. I want a method that helps me to display alternatively on every refresh one of the two values.
For example:
On the first load of page it must display: echo $var1
On the first refresh of page it must display: echo $var2
On the second refresh of page it must display: echo $var1
On the third refresh of page it must display: echo $var2
etc...

For example you can use sessions.
<?php
session_start();
$a = 1;
$b = 2;
if(!isset($_SESSION['variable'])){
$_SESSION['variable'] = true;
}
if($_SESSION['variable']){
echo $a;
}else{
echo $b;
}
$_SESSION['variable'] = !$_SESSION['variable'];
?>

Counting reloads and saving counter to session (https://github.com/Waldz/Examples/blob/master/session/session_counter.php)
<?php
session_start();
if (!isset($_SESSION['timesPageReloaded'])) {
$_SESSION['timesPageReloaded'] = 1;
}
if ($_SESSION['timesPageReloaded']%2 == 1) {
echo 'VAR1 (odd reload)';
} else {
echo 'VAR2 (even reload)';
}
$_SESSION['timesPageReloaded']++;

Related

Pass diferent $_SESSION variable through <a> tag PHP

Hello how can I pass a different $_SESSION variable depending on which link I clicked? For example something like this.
<a href="page2.php value="<?php echo $_SESSION['variable'] = "Foo" ?>Foo</a>
<a href="page2.php value="<?php echo $_SESSION['variable'] = "Bar" ?>Bar</a>
And in page2.php receive the correct value.
How can I do this without using $_POST or $_GET? Thanks in advance!
Only this way (but you need send variable to server, because SESSION you have only on server side):
Foo
Bar
page2.php
<?php
if ($_GET['page'] == 'foo') {
$_SESSION['variable'] = "Foo";
} elseif ($_GET['page'] == 'bar') {
$_SESSION['variable'] = "Bar";
}

PHP - Add and stack a value with a session

Let's say I have a $variable, which contains the word "hey" and a link with a GET parameter.
HTML
Link
PHP
$var = "hey";
if ($_GET['add'] == 'true') {
$var .= "2";
echo $var;
}
When I click on the link, it will add "2" to the variable value "hey", so the
output is: hey2.
Is there a way of keep adding (stacking) the numbers on click with pure PHP?
If i click again, I'd like to have hey22, hey222 (...)
EDIT: It looks like it isn't going to work with variables, so the best answer goes to #Bunker Boy because he solved it with sessions.
#Syno try this:
// mywebsite.html
Link
//mywebsite.php
<?php
session_start();
if(isset($_SESSION["var"])){
if ($_GET['add'] == 'true') {
$_SESSION["var"] .= "2";
echo $_SESSION["var"];
}
}
else{
$_SESSION["var"] = "hey";
if ($_GET['add'] == 'true') {
$_SESSION["var"] .= "2";
echo $_SESSION["var"];
}
}

storing incremented value in session variable and printing it

Had googled this but maybe I'm not a good googler. My ultimate goal is to have a view more button that will read the next 6 files from a directory but before I do that I need to figure out this problem:
at the top of my index.php i have this:
<?php
if(session_id() == '') {
session_start();
$_SESSION["count"] = 0;
}
?>
lower down I have this:
<?php
function album() {
$_SESSION["count"]=$_SESSION["count"]+10;
echo $_SESSION["count"];
}
?>
when the user clicks a button its supposed to print the session variable + 10 on each click.. so 10 20 30 40 ..ect. But it keeps printing 10, it's not updating.
Always start session at the top of the page like
<?php
session_start();
if(session_id() == '') {
$_SESSION["count"] = 0;
}
function album() {
$_SESSION["count"] += 10;
echo $_SESSION["count"];
}
?>
You need to first do session_start() and then ask for $_SESSION.
<?php
session_start();
if(empty($_SESSION['count'])){
$_SESSION["count"] = 0;
}
function album() {
$_SESSION["count"] = $_SESSION["count"] + 10;
echo 'Count: '.$_SESSION["count"];
}
album();
?>
This I just tested it and it works fine.

increment/decrement $_SESSION variable when button is click. Hint header()

I Initialized a $_SESSION variable = 1 and I want to increment/decrements its value when a link is click. And the links will reload the page then echo the value of the $_SESSION. I was given a hint redired using header() but I still can't figure it how.
<?php
if (isset($_SESSION['count'])) {
$count = $_SESSION['count'];
} else {
$count = '';
}
?>
Increment
Decrement
<?php if (isset($_SESSION['count'])): ?>
<?php echo $count ?>
<?php endif ?>
header("Location: index.php") is a php redirect. In this case if the code above is in the index.php file already, there's no need to redirect the user.
<?php
session_start();
if(!isset($_SESSION['count']) { // first time opening the page
$_SESSION['count'] = 0; // initializing the counter
} else { // counter already have a value
if(isset($_GET['inc'])) { // increasing
echo ++$_SESSION['count']; // no need for extra variable (preincrement to echo immediately)
}
if(isset($_GET['dec'])) { // decreasing
echo --$_SESSION['count']; // no need for extra variable (predecrement to echo immediately)
}
}
?>
Increment
Decrement
<?php
session_start();
if(!isset($_SESSION['count'])
$_SESSION['count'] = 0;
$counter = $_SESSION['count'];
$counter = (int)$counter;
if (isset($_GET['inc'])==TRUE) {
$counter++;
$_SESSION['count'] = $counter;
header("Location: index.php");
}
if (isset($_GET['dec'])==TRUE) {
$counter--;
$_SESSION['count'] = $counter;
header("Location: index.php");
}
?>
Also You may need to cast $counter to int..

PHP Static variable reloads after Page Load

This is a simplified version of what I want to accomplish:
In my script I want a static variable x to be incremented every time the submit button is pressed.
<?php
function IncX(){
static $x = 0;
$x++;
echo $x;
}
?>
<body>
<form>
<input type="submit" name="submit" class="next btn btn-primary" value="Submit" />
</form>
</body>
But it initializes to x=0 on every page reload after submit.
You're loading the variable afresh every time the page loads, so it's always going to be the same.
The solution is to store it in a session and then increment it there. Include a conditional to create the variable if it doesn't already exist.
<?php
session_start();
if (!isset($_SESSION['x'])) {
$x = $_SESSION['x'];
} else {
$x = 0;
}
$x++;
echo $x;
$_SESSION['x'] = $x;
?>
<?php
session_start();
$x = 0;
if (isset($_SESSION['x'])) {
$x = $_SESSION['x'];
$x++;
} else {
$_SESSION['x'] = $x;
}
// /$x++;
echo $x;
$_SESSION['x'] = $x;
?>
Apache doesn't keep track of variables in php scrips between clicks, you would have to store it somewhere, be it the $_SESSION or a database.
Moreover, the static keyword doesn't do what you seem to think it does. It would work for successive calls of the function in a single run of the script, but not between clicks.
in any event, you can use the ternary operator to achieve this, would you happen to put it in the session. I've also added a check to make sure the variable is actually a viable count number:
session_start();
$_SESSION['x'] = isset($_SESSION['x']) && is_int($_SESSION['x'])
? $_SESSION['x'] + 1
: 1;
echo $_SESSION['x'];

Categories