Connecting Tech Pros Worldwide Help | Site Map

Problems with counter across pages

  #1  
Old July 17th, 2005, 01:31 AM
Marc
Guest
 
Posts: 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>";
[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, 01:31 AM
Tom Thackrey
Guest
 
Posts: n/a

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)
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 07:46 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 03:55 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 5 November 14th, 2005 12:36 PM
How to do a <table> with <div> tag Toby Austin answers 61 July 20th, 2005 07:27 PM