nutballs

So i think this is the simplest example of what is happening and reproduces the problem. The reason I store messages into session is because I tend to work on apps where the user jumps through a few pages depending on criteria, finally landing on the correct page. They pick up a bunch of messages and errors along the way, and I display it all on the last page, whichever that may be.

The following code snippet and class "should" produce:
1
2
3
4
5

but instead it produces:
1
1
2
1
1
2
3
1
1
2
1
1
2
3
4
1
1
2
1
1
2
3
1
1
2
1
1
2
3
4
5

Seriously, WTF!!!Applause? I am completely stumped. I am sure its because my head is buried in it and I am just not seeing it.

test.

php

  - this is the test code to reproduce the problem.

<?

php

 
require_once('messages.class.

php

 ');
$messages = new messages();

for ($i=1;$i<=5;++$i)
{
$messages->addmsg($i);
}

echo $messages->showmsg();
?>


My messages class. messages.class.

php

 

<?

php

 
class messages
{
public function showmsg()
{
$msg = $_SESSION['msg'];
$_SESSION['msg']='';
return $msg;
}

public function showerr()
{
$msg = $_SESSION['err'];
$_SESSION['err']='';
return $msg;
}
public function addmsg($m)
{
$_SESSION['msg'] .= $_SESSION['msg'].$m."<br> ";
}

public function adderr($e)
{
$_SESSION['err'] .= $_SESSION['err'].$e."<br> ";
}
}
?>

DangerMouse

lol just tested that out to try to reproduce the problem, and it caused a serious wtf?! moment for me aswell Applause

The 'adderr' and 'addmsg' methods are duplicating the data held in the session variable, causing it to recursively append itself as you run through the loop.

If you replace
$_SESSION['msg'] .= $_SESSION['msg'].$m."<br> ";

with
$_SESSION['msg'] .= $m."<br> ";


And the same for the 'errors' method, it works out ok.

DM

perkiset

Applause

NBs you're using the concatenation shorthand... you're used to this:

a = a + 1

but the shorthands do this:

a += 1

strings are the same

$aString = $aString . ' more text';

or

$aString .= ' more text';

Good eye DM

/p

nutballs

HAHAHAHAHAHAHAHAHAHA i am a
fishing TARD

I am getting myself all screwed up between

ASP

  and

PHP

 . lol.

Applause

nevermind, move along... nothing to see here...

thanks DM. seriously. hours of my life, wasted.

perkiset

Applause Applause

Wish I had a dime for every time I was a fishing tard...

jammaster82

quote

Wish I had a dime for every time I was a fishing tard...

You do dont you?    Applause    Applause

@ nbs::

Chill out, scro' , theres plenty of
'tards out there now living really
kick ass lives.

My first wife was 'tarded.  Shes
a pilot now..  so if you could
just go ahead and put your
tattoo in that shit....

:p

I do stuff like that all day long,
i can bend a coat hanger to get
into my car where the keys are
in literally 14 seconds with no tools.

Thats bent and in the car.

Well i

learn

 ed a thing or two out of your
post anyway...

nutballs

nice quote. do you have that movie completely memorized? Hmmm... i sense an oxymoron in there somewhere.

there is probably a second lesson in here as well.

DO NO CODE IN TWO SIMILAR LANGUAGES AT THE SAME TIME OR BABY JEBUS WILL CRY LOTS.

perkiset

This is precisely why I have multiple monitors.

On the left we have

PHP

 . In the middle we have miscellaneous tools. On the right we have JS. I have worked it out so that my body remembers that when I am looking to the right I DO NOT put a $ in front of variables, and when I am looking left I use -> instead of . for objects. It's taken quite a while to get that one locked down I'll tell you.

jammaster82

quote

do you have that movie completely memorized?


y, amongst other numbers i may have only seen
once or twice like to poke address 752 with
a 1 on an atari 800 and you can make the cursor
disap

pear

 ? autism much?  Still cant remember
the keys have to come out of the truck with me though.

Applause Applause Applause Applause

i always thought an oxymoron was a dumb guy
putting on zit medicine...


Perkiset's Place Home   Politics @ Perkiset's