Connecting Tech Pros Worldwide Help | Site Map

Problems with counter across pages

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 12:31 AM
Marc
Guest
 
Posts: n/a
Default Problems with counter across pages

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>";
[color=blue]
>getNumberOfQuestions($category_id) correctly returns amount of[/color]
questions in db;[color=blue]
>values for $Submit is set;
>first call to $questionCounter gives ""
>second call to $questionCounter gives 0
>session.auto_start=1 in php.ini[/color]

$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

  #2  
Old July 17th, 2005, 12:31 AM
Tom Thackrey
Guest
 
Posts: n/a
Default Re: Problems with counter across pages


On 13-Oct-2003, meelzooi2000@yahoo.com (Marc) wrote:
[color=blue]
> 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>";
>[color=green]
> >getNumberOfQuestions($category_id) correctly returns amount of[/color]
> questions in db;[color=green]
> >values for $Submit is set;
> >first call to $questionCounter gives ""
> >second call to $questionCounter gives 0
> >session.auto_start=1 in php.ini[/color]
>
> $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?[/color]

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 jamesbutler@willglen.net (it's reserved for spammers)
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.