Hello i got variable $offset. When user is viewing page - $offset should be equal to 0;
When he is clicking on link
Next
It should update the $offset by adding to it next's value.
So i wrote like this
$offset = $offset + $_GET['next'];
When i'm clicking on link first time it works, but the future clicks doesnt, because he dont remember $offset value. How should i write to do it right?
I would deplore using a session to store this. It is a waste of server resources.
$nextLot=3;
$offset=0;
if(!empty($_GET['offset']))
{
$offset=$_GET['offset'];
}
$offset+=$_GET['next'];
Next
This method saves some resources and simply checks the URL for all the info that is needed.
You can pass the $offset variable via $_SESSION.
So your code will look something like this:
$_SESSION['offset'] = $_SESSION['offset'] + $_GET['next'];
You can do this by using SESSION
<?php if(isset($_SESSION['next']) : ?>
Next
<?php else : ?>
Next
<?php endif; ?>
and for offset
$_SESSION['offset'] = isset($_SESSION['offset']) + $_GET['next'];
Related
I am trying to make anchor links for different sections in the page.
#section1
#section2
But, the url isn't being updated each time I click the href link
if(!isset($_SESSION['counter'])) {
$_SESSION['counter'] = 1;
}
<?php echo ''.'Next Section'.'';?>
When I echo the counter I can see it being updated upon refresh but I want to be able update the anchor section without refreshing.
Thank you
You need to actually increment the value in the session and not just what is currently stored in the session.
// If there is a value in the session, then increment it.
if(isset($_SESSION['counter'])) {
$_SESSION['counter'] = $_SESSION['counter'] + 1;
} else {
$_SESSION['counter'] = 1;
}
// Now, use the value which you have set.
<?php echo ''.'Next Section'.'';?>
I'm trying to get a session variable to alternate between 0 and 1 on each page load.
So first time page loads
$_SESSION['turn'] = 0;
Second time
$_SESSION['turn'] = 1;
Third time
`$_SESSION['turn'] = 0;`
and so on.
Then I can call that variable later in the page.
I can't work out how to do this. I've tried a simple IF function but can't get it to work.
First the session must be started on any page wishing to make use of the session array. session_start()
Next you have to remember that initially the session variable you are using will not exist the first time you attempt to use it
So
<?php
session_start();
if ( !isset($_SESSION['turn']) ) {
// does not exist yet, so create with 0
// you may want to initialize it to 1, thats up to you
$_SESSION['turn'] = 0;
} else {
$_SESSION['turn'] = $_SESSION['turn'] == 0 ? 1 : 0;
}
Try this where the page is loaded.
$_SESSION['turn']=1-$_SESSION['turn'];
code:
<?php
session_start();
echo $_SESSION['turn'];
$_SESSION['turn']=1-$_SESSION['turn'];
?>
Edit : RiggsFolly !isset() is correct. mine misses it and it will give errors in log. and the first value is not 0
I have a form sender which is posting a value of 6 to another form receiver. What I'm trying to achieve is store the posted value from sender into a variable in the receiverthen increment the variable it every time the sender posts. Then print the updated variable
This is what I have tried to do
$val= $_POST['val'];
$limit = 6 + $val;
echo $limit;
Im getting the result as 12. But what I want is
After first post result = 12
After second post result = 18
On and on...
NB:$_POST['val'] = 6;
session_start();
$limit = 6;
if(!isset($_SESSION['lastLimit'])) {
$_SESSION['lastLimit'] = 0;
}
if(!empty($_POST)) {
$_SESSION['lastLimit'] = $_SESSION['lastLimit'] + $limit;
$postedValue = $_POST['val'] + $_SESSION['lastLimit'];
echo $postedValue;
}
Because the web is stateless i.e. scripts do not remember anything that happened the last time a page/form was executed the receiver script does not remember anything from the last time it was run.
But dont panic, there is a way. Its called a SESSION and you can store data in the session which will then be available the next time this user connects to your site. In PHP you use it like this. The session is linked to this specific connection to a specific user.
receiver.php
<?php
// must be run at top of script, before any output is sent to the new form
session_start();
// did the form get posted and is the variable present
// or replace POST with GET if you are using an anchor to run the script
if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['val']) {
if ( isset($_SESSION['limit'] ){
// increment the limit
$_SESSION['limit'] += (int)$_POST['val'];
} else {
// initialize the limit
$_SESSION['limit'] = (int)$_POST['val'];
}
echo 'Current value of limit is = ' $_SESSION['limit'];
} else {
// something is not right
// direct this user to some basic page like the homepage or a login
header('Location: index.php');
}
You need an intermediate layer to store the value.
Available options:
1) Global static value
2) session
3) file
4) database
I would recommend global value or session, as they data you want to store isn't that huge and would meet the requirements easily.
I would not write the syntax to store it in session as a number of people have already mentioned it. I just wanted to clarify the problem scenario and possible solutions.
You can store $limti into global varibale .
global $val;
$val += $_POST['val'];
$limit = 6 + $val;
echo $limit;
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'm working on a little project, basically I have some text on my PHP/HTML page that is being echo'ed from a variable ($brief_string).
There is also a back, and continue button which basically subtracts or adds to another variable ($brief_page - which is pulled from my DB). The brief_string changes depending on the brief_page by using if statements. First problem I encounter is that when I hit continue (submit button) it resubmits/refreshes the page, causing my brief_page to reset back to 0.
So I'm thinking maybe I could use JS to hold the info and page variables and control the dynamic text, but then, how would I update my DB with the current page value via JS? Isn't it really easy to manually change/hack these values? I would preferably like my DB to be updated with the page number each time the use presses the back/continue button.
I would just like some advice really as I am a student trying to develop an interactive book like site (that uses a DB to save your current page).
Code:
<?
$brief_info = "brief info goes here";
$brief_page = 0; //< will soon be pulled off DB
if (isset($_GET['brief1Go'])) {
$brief_page = $brief_page + 1;
}
else if (isset($_GET['brief1Back'])) {
$brief_page = $brief_page - 1;
}
$breifController = "
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
<input type=\"submit\" name=\"brief1Back\" id=\"brief1Back\" value=\"BACK\" />
</form>
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
<input type=\"submit\" name=\"brief1Go\" id=\"brief1Go\" value=\"CONTINUE\" />
</form>";
if($brief_page == 0){
$brief1_info = "<b>Welcome Commander,</b> you are currently viewing the Mission Control Brief page, here you can view all your missions that you need to complete in order to advance through your prestiges. You will need to follow your briefs carefully in order to succeed. <br /><br />
";
}
else if($brief_page == 1){
$brief_info = "Okay, let me show you around the place ...";
}
else if($brief_page == 2){
$brief_info = "brief is on 2";
}
?>
Why not just use get vars entirely?
yes, start at 0 unless $_GET['page'] is set...
$brief_page = 0;
if(isset($_GET['page']))
$brief_page = $_GET['page'];
then only use links to your next and previous pages instead of some weird post thing.
Previous Next
where obviously the page numbers in the previous and next are just echoed from php
$prev = $brief_page - 1;
$next = $brief_page + 1;
The user specific things to store can easily be handled with sesisons, cookies or even other get vars if you want to introduce a horrible security hole. Your choice really.
I would definitely not do this via $_POST though. totally annoying. Go with all full on ajax if you want to do that. At least you won't pester the user with "are you sure you want to resubmit the form data" messages if they choose to refresh the page.