FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Re: Counter reset or not?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Counter reset or not? [message #182485] Thu, 08 August 2013 04:06 Go to next message
Richard Damon is currently offline  Richard Damon
Messages: 58
Registered: August 2011
Karma: 0
Member
On 8/7/13 7:38 PM, Twayne wrote:
> Hi all,
>
> I'm confused; this code isn't working and I don't know why; perhaps you
> can tell me or at least point me in the right direction:
>
> <code>
> if($_SESSION["cntr"] > "0") {
> echo "Count is now : " . $_SESSION["cntr"]." ";
> }
> ELSE {
> $_SESSION['cntr']="0";
> $cntr=$_SESSION['cntr'];
> echo "Counter : " . $cntr."<br />";
> }
> </code>
>
> OK: This code is on the first of 3 PHP form pages. The ELSE works fine.
> If the count is zero, the initial count of "0" is echoed properly.
>
> But if the code is 1 or greater, set on one of the two succeeding pages,
> it still runs the ELSE code and insists on returning the value of "0".
>
> I'm not using session_destroy, nor an I using die(), exit() or anything
> else that should bother the SESSION var.
> 2. If the counter advances to 1 or more, however, when I send the user
> back to the first page (where the above code resides), it still prints
> the ELSE portion of the code, NOT what the SESSION var has been set to
> (1 or greater).
>
> I'm confused!! What am I doing wrong?
>
> TIA,
>
> Twayne`
> O

Have you called session_start() (or set session.auto_start) ?
Re: Counter reset or not? [message #182490 is a reply to message #182485] Thu, 08 August 2013 16:15 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 2013-08-07 11:30 PM, Richard Yates wrote:
> On Wed, 07 Aug 2013 19:38:39 -0400, Twayne <nobody(at)spamcop(dot)net> wrote:
>
>> Hi all,
>>
>> I'm confused; this code isn't working and I don't know why; perhaps you
>> can tell me or at least point me in the right direction:
>>
>> <code>
>> if($_SESSION["cntr"] > "0") {
>> echo "Count is now : " . $_SESSION["cntr"]." ";
>> }
>> ELSE {
>> $_SESSION['cntr']="0";
>> $cntr=$_SESSION['cntr'];
>> echo "Counter : " . $cntr."<br />";
>> }
>> </code>
>>
>> OK: This code is on the first of 3 PHP form pages. The ELSE works fine.
>> If the count is zero, the initial count of "0" is echoed properly.
>>
>> But if the code is 1 or greater, set on one of the two succeeding pages,
>> it still runs the ELSE code and insists on returning the value of "0".
>>
>> I'm not using session_destroy, nor an I using die(), exit() or
>> anything else that should bother the SESSION var.
>> 2. If the counter advances to 1 or more, however, when I send the user
>> back to the first page (where the above code resides), it still prints
>> the ELSE portion of the code, NOT what the SESSION var has been set to
>> (1 or greater).
>>
>> I'm confused!! What am I doing wrong?
>>
>> TIA,
>>
>> Twayne`
>> O
>
> Your code works here if I set $_SESSION['cntr'] just above the if
> statement?

Specifically what do you mean by 'set" it above the if? I've tried
various methods of "setting" $_SESSION['cntr'] before the IF, but ...
nothing changes or I get a Notice of an undefined variable if I try to
, say, echo $_SESSION["cntr"] . "<br />"; , it also reports 0! So
somehow, the session variable never makes it back to the first page.

Further to the above, I do not have a value pre-set for the related
input field.

Here's the code from the second page where an error is being purposely
generated if it helps any:
<code>
echo "Counter was : " . $_SESSION["cntr"] . "<br />";
if(strlen($zip)< "5") {
$cntr=$_SESSION["cntr"] + "1";
echo "Counter is " .$cntr."<br />" ;
}
</code>

Both echo statements work, with the expected results when an error
occurs. It's 0 for the first echo, and 1 for the last echo.

Apparently somehow I am destroying the contents of the session var but
darned if I can find it!

An further thoughts or comments would be appreciated; feel free to be
brutal; I'm sure it's something very basic I'm doing. I am in the
process of creating files with nothing but the counter session to get a
closer look maybe.

My sincere apologies for not providing entire code for the pages; it's
simply not allowed.
>
> Have you tried echoing $_SESSION['cntr'] before the if statement to
> confirm that it is what you think it is?
>

No, but ... if I do, it shows $SESSION["cntr"] to be "0". And that
means it's somehow trashing the vars somewhere I'm not aware of.

Thanks much for your response; it at least put things into a better
prospective for me. I think :)

Twayne`
Re: Counter reset or not? [message #182491 is a reply to message #182485] Thu, 08 August 2013 16:19 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 2013-08-08 12:06 AM, Richard Damon wrote:
> On 8/7/13 7:38 PM, Twayne wrote:
>> Hi all,
>>
>> I'm confused; this code isn't working and I don't know why; perhaps you
>> can tell me or at least point me in the right direction:
>>
>> <code>
>> if($_SESSION["cntr"] > "0") {
>> echo "Count is now : " . $_SESSION["cntr"]." ";
>> }
>> ELSE {
>> $_SESSION['cntr']="0";
>> $cntr=$_SESSION['cntr'];
>> echo "Counter : " . $cntr."<br />";
>> }
>> </code>
>>
>> OK: This code is on the first of 3 PHP form pages. The ELSE works fine.
>> If the count is zero, the initial count of "0" is echoed properly.
>>
>> But if the code is 1 or greater, set on one of the two succeeding pages,
>> it still runs the ELSE code and insists on returning the value of "0".
>>
>> I'm not using session_destroy, nor an I using die(), exit() or anything
>> else that should bother the SESSION var.
>> 2. If the counter advances to 1 or more, however, when I send the user
>> back to the first page (where the above code resides), it still prints
>> the ELSE portion of the code, NOT what the SESSION var has been set to
>> (1 or greater).
>>
>> I'm confused!! What am I doing wrong?
>>
>> TIA,
>>
>> Twayne`
>> O
>
> Have you called session_start() (or set session.auto_start) ?
>

Yes; session_start(). set session.auto_start I wasn't aware of though;
something else to check out.

Thanks much,

Twayne
Re: Counter reset or not? [message #182493 is a reply to message #182485] Thu, 08 August 2013 19:14 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 2013-08-08 12:06 AM, Richard Damon wrote:

Richard,

Please see my Update for the current situation. It has changed and so
has the code.

Twayne`

>
Re: Counter reset or not? [message #182494 is a reply to message #182485] Thu, 08 August 2013 19:13 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 2013-08-07 11:30 PM, Richard Yates wrote:
> On Wed, 07 Aug 2013 19:38:39 -0400, Twayne <nobody(at)spamcop(dot)net> wrote:
>
Please see my Update for the current situation. It has changed and so
has the code.

Twayne`
Re: Counter reset or not? [message #182524 is a reply to message #182490] Sat, 10 August 2013 12:49 Go to previous message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
On Thu, 08 Aug 2013 12:15:30 -0400, Twayne <nobody(at)spamcop(dot)net> wrote:

>>> <code>
>>> if($_SESSION["cntr"] > "0") {
>>> echo "Count is now : " . $_SESSION["cntr"]." ";
>>> }
>>> ELSE {
>>> $_SESSION['cntr']="0";
>>> $cntr=$_SESSION['cntr'];
>>> echo "Counter : " . $cntr."<br />";
>>> }
>>> </code>

> Specifically what do you mean by 'set" it above the if?

It's part of a basic debugging strategy. If a script is not working,
start breaking it into smaller pieces to see how variables change as
it flows. You are using two pages and session variables. You don't
know if the session variable is being set wrong, or is not accessible
to page 2, or page 2 is faulty. So...

on page 2 insert the line:

$_SESSION['cntr']=1;

before:

if($_SESSION["cntr"] > "0") {

If the results are still screwy then the problem is on page 2. If they
are correct, then the problem is page 1 or in accessing the session
variable on page 2.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: fetching value from related multiple rows & columns in PHP SQL
Next Topic: Please evaluate my cache-control & pragma etc. code
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 22 17:10:23 GMT 2024

Total time taken to generate the page: 0.02717 seconds