By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,743 Members | 1,780 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,743 IT Pros & Developers. It's quick & easy.

Problems with counter across pages

P: n/a
Hello,

This is a simple thing but somehow I can't work it :((((

I have a self-submitting form displaying data from a database. To keep
track which data to show I want to create a variable that keeps track
how many times the page is visited.

The idea is:
first call to page:var is not set => set var=0,
subsequent calls:
depending on the way the page was called* add or subract 1 from var
(provided var is respectively < nrofrecords in database and < 0).

*via a submit button called 'next page' or 'previous page'

The code:
// test 1
echo"\$Submit = ".$Submit." - \$questionCounter =
".$questionCounter."<br>";

if($questionCounter==NULL)
$questionCounter=0;
elseif($Submit=="previous page")
$questionCounter=$questionCounter>0?$questionCount er-1:$questionCounter;
elseif($Submit=="next page')
$questionCounter=($questionCounter<getNumberOfQues tions($category_id)-1)?$questionCounter+1:$questionCounter;

//test 2
echo"\$questionCounter: ".$questionCounter."<br>";
getNumberOfQuestions($category_id) correctly returns amount of questions in db;values for $Submit is set;
first call to $questionCounter gives ""
second call to $questionCounter gives 0
session.auto_start=1 in php.ini


$questionCounter never adds up. Always when I call the page via the
'next page' button the initial value of $questionCounter ="", after
the code it's 0

What's wrong - how can I make this work?

Thanks,

Marc
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

On 13-Oct-2003, me**********@yahoo.com (Marc) wrote:
I have a self-submitting form displaying data from a database. To keep
track which data to show I want to create a variable that keeps track
how many times the page is visited.

The idea is:
first call to page:var is not set => set var=0,
subsequent calls:
depending on the way the page was called* add or subract 1 from var
(provided var is respectively < nrofrecords in database and < 0).

*via a submit button called 'next page' or 'previous page'

The code:
// test 1
echo"\$Submit = ".$Submit." - \$questionCounter =
".$questionCounter."<br>";

if($questionCounter==NULL)
$questionCounter=0;
elseif($Submit=="previous page")
$questionCounter=$questionCounter>0?$questionCount er-1:$questionCounter;
elseif($Submit=="next page')
$questionCounter=($questionCounter<getNumberOfQues tions($category_id)-1)?$questionCounter+1:$questionCounter;

//test 2
echo"\$questionCounter: ".$questionCounter."<br>";
getNumberOfQuestions($category_id) correctly returns amount of

questions in db;
values for $Submit is set;
first call to $questionCounter gives ""
second call to $questionCounter gives 0
session.auto_start=1 in php.ini


$questionCounter never adds up. Always when I call the page via the
'next page' button the initial value of $questionCounter ="", after
the code it's 0

What's wrong - how can I make this work?


The value of the counter is NOT automatically preserved between pages.
You either need to stash the value of the counter in a hidden input field
(<input type="hidden" ...) on the page or use sessions and a $_SESSION
variable.

http://www.php.net/manual/en/ref.session.php

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.