I have a problem when it comes to displaying the correct number in my heading. When a user submits a page and navigates to this current page, I want the heading below to appear:
CREATING QUESTIONS AND ANSWERS: SESSION (AAA) 1 OF 3
As you can see it above it starts with the number 1 as the user is creating their first session. The problem I am getting is that it never displays number 1, it just keeps displaying this below:
CREATING QUESTIONS AND ANSWERS: SESSION (AAA) 3 OF 3
It keeps displaying the number 3 which is incorrect as if the user enters the current page for the first time then obviousl they don't start with session 3, they start with session 1, then 2 then 3.
So my question is how do I get "SESSION 1" to be displayed when the useer enters the page for the first time?
Below is the current code I have:
if(isset($_POST['sessionNum'])){
//Declare my counter for the first time
$_SESSION['initial_count'] = $_POST['sessionNum'];
$_SESSION['sessionNum'] = $_POST['sessionNum'];
}
if (!isset($_SESSION['sessionCount'])) {
$_SESSION['sessionCount'] = 1;
}
else if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
++$_SESSION['sessionCount'];
}
$sessionMinus = $_SESSION['sessionCount'];
...
<h1>CREATING QUESTIONS AND ANSWERS: SESSION (<?php echo $_SESSION['id'] ?>) <?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'] ?></h1>
When you are setting for the first time:
//Declare my counter for the first time
$_SESSION['initial_count'] = $_POST['sessionNum'];
Set it to 1! Because after that isset will return true, and so, it will not be set to 1...
//Declare my counter for the first time
$_SESSION['initial_count'] = 1;
Related
Below I have a line of code where it states which Page a user is currently on out of the number of Total Pages.
<h1>CREATING QUESTIONS AND ANSWERS: SESSION (AAA) <?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'] ?></h1>
So for example the line could read this:
CREATING QUESTIONS AND ANSWERS: SESSION (AAA) 3 OF 3
Now the problem I have is that if the user is on the first page, it doesn't display the number 1. So instead of stating 1 OF 3 or 1 OF 5, it is stating 2 OF 3 or 2 OF 5.
So my question is that how can I get the first page to equal 1 OF ... when user accesses the first session (first page)?
Below is the current code:
if(isset($_POST['sessionNum'])){
//Declare my counter for the first time
$_SESSION['initial_count'] = $_POST['sessionNum'];
$_SESSION['sessionNum'] = $_POST['sessionNum'];
$_SESSION['sessionCount'] = 0;
}
else if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
$_SESSION['sessionCount']++;
}
$sessionMinus = $_SESSION['sessionCount'];
$_SESSION['sessionNum'] is the total number of Sessions. So if it is 3 OF 5, then $_SESSION['sessionNum'] is 5.
if you want to build something like "Wizards" then in sessions you would store some data to keep track of user activity and general process, you say "if the user is in the first session ..." that's confusing, better say "if the user is in first step"
say you want to store in sessions, "user is in step 1 of 3 total steps", you would need
$_SESSION["user_track"] = array(
("current") => 1,
("next")=>2,
("total") => 3,
("previous") => 0
);
and a IF to check on every step
if($_SESSION["user_track"]["next"] != $GLOBAL_STEP){
/*
$GLOBAL_STEP , a integer you defined in the current step say its 2
and users next step matches 2 then he/she is on the correct step
*/
}else if($_SESSION["user_track"]["next"] == $_SESSION["user_track"]["total"]){
/*At the ending step*/
}else{
// couldn't proceed to next step, something went wrong
}
hope it helps
I would suggest :
to use the word 'page' or 'steps' instead of 'session', because 'session' is confusing with the $_SESSION array.
to use a simple GET parameter for current page/step (e.g. yourpage.php?step=X), where by default the step value would be 1.
<?php
session_start();
if(isset($_POST['sessionNum'])){
//Declare my counter for the first time
$_SESSION['initial_count'] = $_POST['sessionNum'];
$_SESSION['sessionNum'] = $_POST['sessionNum'];
}
if(!isset($_SESSION['sessionCount'])){
$_SESSION['sessionCount'] = 1;
}
else
{
$_SESSION['sessionCount']++;
}
$sessionMinus = $_SESSION['sessionCount'];
?>
How do I get it so that if $_SESSION['sessionCount'] is less than $_SESSION['sessionNum'], then add 1 to $_SESSION['sessionCount'] and if it equals $_SESSION['sessionNum'], then stop adding 1 to $_SESSION['sessionCount']?
Also if I go back on a previous page and I go back onto this page, I want $sessionMinus to go back to '1', and finally if the user refreshes the page, then whatever number $sessionMinus is, keep it on that number when page refreshes.
How do I get it so that if $_SESSION['sessionCount'] is less than
$_SESSION['sessionNum'], then add 1 to $_SESSION['sessionCount'] and
if it equals $_SESSION['sessionNum'], then stop adding 1 to
$_SESSION['sessionCount']?
if (!isset($_SESSION['sessionCount'])) {
$_SESSION['sessionCount'] = 1;
}
else if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
++$_SESSION['sessionCount'];
}
Also if I go back on a previous page and I go back onto this page, I
want $sessionMinus to go back to '1'
To do that you have to set $_SESSION['sessionMinus'] (or some other variable) in the previous page. Once this page is reached, the only way to know what happened earlier is specifically through $_SESSION variables. You cannot detect it on the spot.
and finally if the user
refreshes the page, then whatever number $sessionMinus is, keep it on
that number when page refreshes.
This is not possible. You cannot tell if the page was refreshed or loaded from scratch¹. What you could do is use the PRG pattern and count the "P" page as "the user just got here" and the "G" page as "the user has refreshed the page". You can set a variable (e.g. $_SESSION['redirecting'] = true) from the "P" page and modify it on the "G" page ($_SESSION['redirecting'] = false); just before you do that, check if it was true to begin with. If it was, then the user is here due to your redirect (which will only happen once). If it was already false, they have refreshed the page.
¹You can try to do it, again through $_SESSION, but really you are just guessing. There is no way to know for certain.
if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
// sessionNum is bigger then sessionCount
$_SESSION['sessionCount']++;
}
I've hit a dead end. I've been working on this for 3 weeks with no result.
I have 10 php pages (1.php, 2.php, ..., 10.php) and a starting page (start.php).
All I want to do is randomize the 10 pages with no repeat, so when I click "next" in the start.php it should go to one of the 10 pages (let's say for example it goes to 4.php). When I click "next" in 4.php it should redirect to another within the 10 pages except for 4.php.
It should continue this until all the numbers (1.php - 10.php) have been displayed. At this point it should randomize again. When I click "next" in the last number .php displayed, it should randomize the number and go back to the first on the random list.
Here's what I have so far:
start.php:
<?php $vidcount = 1; ?>
<? include ("source.php"); ?>
next page
source.php:
<?php
include ("start.php");
$numbers = range(1, $total_songs);
if(($vidcount == $total_songs)||($vidcount == 1)){
shuffle($numbers);
$vidcount = 1;
}
$nextvid[1] = $numbers[0];
$nextvid[2] = $numbers[1];
$nextvid[3] = $numbers[2];
$nextvid[4] = $numbers[3];
$nextvid[5] = $numbers[4];
$nextvid[6] = $numbers[5];
$nextvid[7] = $numbers[6];
$nextvid[8] = $numbers[7];
$nextvid[9] = $numbers[8];
$nextvid[10] = $numbers[9];
?>
1.php, 2.php, ... 10.php:
<?php
include("source.php");?>
<?php echo $vidcount; ?>
next page
<?php $vidcount++;?>
1.php - 10.php have the same code. I also have a source.php which is supposed to keep track of what number has been displayed and re-shuffle when all the numbers have been displayed.
Please help. I'll greatly appreciate any help I can get.
You don't have to use the above code, I don't mind starting from scratch if you have a different idea as long as the code I get works.
Well firstly why do you have ten files when you could just have one file and ?id=X in the URL? But never mind that.
Your best bet is to use a session variable. Something like this:
<?php
session_start();
if( !isset($_SESSION['sequence']) || !$_SESSION['sequence']) {
$_SESSION['sequence'] = shuffle(range(1,10));
}
$next = array_shift($_SESSION['sequence']);
// now use $next to create your "Next page" link.
?>
I have some variable where everytime the form is submiited or page is refreshed, it adds 1 to $sessionMinus. But if the user goes onto the previous page and then goes back onto this page, I want $sessionMinus to go back to being '1'.
At the moment if lets say the number is 3 and then the user goes back to previous page and then back to this page, it still displays 3, but I want it to go back to 1. I heard I have to set either $sessionMinus or $_SESSION['sessionCount'] to '1' on the previous page but how do I do this?
below is the code for the current page (not previous page) on how the $sessionMinus is declared and incremented:
if(!isset($_SESSION['sessionCount'])){
$_SESSION['sessionCount'] = 1;
}
else
{
$_SESSION['sessionCount']++;
}
$sessionMinus = $_SESSION['sessionCount'];
?>
On the previous page, do:
if( isset( $_SESSION['sessionCount'])){
$_SESSION['sessionCount'] = 1;
// Or maybe 0 if you want $sessionMinus = 1 on next page
}
I have a Session which I am using to hold items in a form that are accumulated up by the user until the user wants to proceed to checkout. Its a bit like a Shopping cart where items can be added from the form.
Logical breakdown of code:
Page loads, session starts
If $_SESSION['set'] is not set then set it to TRUE.
Display rest of page and form.
User hits "Add another item" button.
Page data gets posted to itself
Page checks that $_SESSION['set'] = True and $_POST['add_item'] is set.
Page creates a session variables in an array, and adds posted values to those sessions.
Page increments $_SESSION['tariff_count'] if more needs to be added
The problem is that my code is not behaving as it should. When I click "Add new tariff" button the first time it does not get caught by my if function. This should be immediately caught. However when I go and press the button again, it finally works and adds an item to my session.
Here is the code:
//start a session to remember tariff items
session_start();
//testing the session array
print_r($_SESSION);
//destroy session if this character is found in URL string
$des = $_GET['d'];
if($des == 1)
{
session_destroy();
}
//checks to see if session data has been set
//if a session variable count is set then
if ($_SESSION['set'] == TRUE)
{
//perform a check to ensure the page has been called by the form button and not been accidently refreshed
if(isset($_POST['add_tariff']))
{
//if user clicks Add another tariff button then increase tariff count by one
//temp variable set to the current count of items added
$count = $_SESSION['tariff_count'];
$_SESSION['tariff_name'][$count] = $_POST['tariff_name'];
$_SESSION['tariff_net'][$count] = $_POST['tariff_net'];
$_SESSION['tariff_inclusive'][$count] = $_POST['tariff_inclusive'];
$_SESSION['tariff_length'][$count] = $_POST['tariff_length'];
$_SESSION['tariff_data'][$count] = $_POST['tariff_data'];
//increment tariff count if more data needs to be added to the sessions later.
$_SESSION['tariff_count']++;
}
}
//if no session data set then start new session data
else
{
echo "session set";
$_SESSION['set'] = TRUE;
$_SESSION['tariff_count'] = 0;
}
The code seems to be fudging my arrays of Sesssion data. All my added items in the session are displayed in a table.
However if my table shows six items, if i do a print_r of the session it only shows there are 4 items in the array? I have tested it to make sure I am not reprinting the same instances in the array.
Here is a print_r of the array that shows six rows but there are only four rows in this array?
[tariff_count] => 5 [tariff_name] => Array (
[0] => STREAM1TARIFF [1] => STREAM1TARIFF [2] => CSS [3] => CSS [4] => CSS
)
I have take a screenshot as well to show this strange problem
http://i.imgur.com/jRenU.png
Note I have echoed out "True Value =6" but in the print_r of the session it is only 5, so my code is missing out one instance (n-1).
Here is my code that prints all the instances in the session arrays, I have a feeling part of the problem in mismatch is caused by the "<=" comparison?
if(isset($_SESSION['tariff_count']))
{
for ($i = 0; $i <= $count; $i++)
{
echo "<tr>";
echo "<td>".$_SESSION['tariff_name'][$i]."</td>";
echo "<td>".$_SESSION['tariff_net'][$i]."</td>";
echo "<td>".$_SESSION['tariff_inclusive'][$i]."</td>";
echo "<td>".$_SESSION['tariff_length'][$i]."</td>";
echo "<td>".$_SESSION['tariff_data'][$i]."</td>";
echo "</tr>";
}
}
Paste bin of php page - http://pastebin.com/petkrEck
Any ideas, why my If statement is not catching the event when the user presses "Add another tariff" button the first time it is pressed, but then detects it afterwards?
Thanks for your time
Merry Christmas!
The problem is your code flow. In simplified pseudo-code, you're doing this:
if (session is not initialized) {
set = true
count = 0;
} else {
add posted data to session
}
On the first 'add item' call, the session is not set up, so you set up the session. AND THEN IGNORE THE POSTED DATA.
The code flow should be:
if (session is not initialized) {
set = true;
count = 0;
}
if (posting data) {
add data to session
}