PHP Programming > General PHP Help

I need support with increment

(1/3) > >>

shadowloud:
Greetings,

I just started coding PHP 2 days ago. I decided that I would make some PHP Based PHP.

To do so I first used imagecopy to make myself an "avatar system" that looks like this :

hxxp://website.com/avatar.php?skin=1&armor=1&hat=1&face=1&item1=1&item2=1&hair=1

There will belong the names of the png images that will be used to display the various items. I name give them numbers names because at a later time, once I am more skilled, I will make an item table into mysql and make PHP fetch into this table.

Meanwhile, I am building my registration page on which I want users to be able to set their characters.

http://img1.uploadscreenshot.com/images/orig/3/8012253478-orig.jpg

In order to make this thing work, I decided to pick a pre-made JS script on the internet.. it seemed easier, and I didn't want to bother too much on this part of my project lol. So, it looks like this :


--- Code: ---function sumhair() {

document.getElementsByName("avatar")[0].setAttribute("src", "avatar.php?skin=1&armor=1&hat=1&face=1&item1=1&item2=1&hair=<? echo ++$un; ?>");
}

function neghair() {
document.getElementsByName("avatar")[0].setAttribute("src", "avatar.php?skin=1&armor=1&hat=1&face=1&item1=1&item2=1&hair=<? echo --$un; ?>");

}
--- End code ---

I would have JS codes of eachs defining functions for increament and decreament for the arrow buttons.


--- Code: ---<input value=">>" name="next_hair" type="button" onClick="sumhair()"/>
<input value="<<" name="next_hair" type="button" onClick="neghair()"/>
--- End code ---

Setted up through an included js file. Okayyyyy........ So! Here is my issue. I do not understand how increamentation works. I do want to make incrementing buttons for my avatar design, so it switch items. I tried the following as of now :


--- PHP Code: ---$un = 1;

<? echo ++$un; ?> // This being used in the links written within JS script
--- End code ---


But I get stuck at 2. It seems $un + 1 = 2. Then if I press my button again... ++$un is still understood as " 1 + 1 = 2 " and not as " 2 + 1= 3 ", which is what I want as result. Can somebody help me understand how to deal with this issue? Perhaps should I make some function? Hmmm.. I am somewhat lost there.

ErnieAlex:
I am sure that others will disagree, but, I always use the form $un=$un+1;   There is NO issue with this version.  Never changes, always adds 1...

By the way, the ++$un and $un++ is totally different.  Also, if you are doing this in a formula or echo, one returns the value then adds and the other adds then returns.  Confusing, I say...   $un=$un+1; never changes.  It always adds just one...

Here is a link discussing this:   http://www.php.net/manual/en/language.operators.increment.php

Laffin:
However that wont solve the issue.
I prefer ++ myself :)
but again I have years of experience. and throw it in formulas like it was sugar being added to my coffee :) Shake... Shake... Considering Thought.... Shake :)

The issue, my money being on: You arent saving/retrieving the value of $un
so $un will always be zero
and no matter what increment method you use, the end result is 1

show us the code of the saving/retrieving of $un and we go from there...

:)

bushkid85:
++ always.

$sun=$sun+1 is ugly, I thought better of you ErnieAlex. Ha  :P

ErnieAlex:
See, I said people will disagree...   I smiled at your sugar note, Laffin...  lMAO...

Anyway, I teach more than I realized...  My comments were for someone just starting PHP two (2) days ago!
LOL,  Confuse him with ++,  Not my style...  Start him with the old basic way and throw in ++ or other tricky code which is very hard for a beginner to follow later on when they can handle it.  Sorry, I should have explained it better...   $un=$un+1 is much easier for a brand new 2-day old PHP baby to follow...

Hmmm, speaking of sugar, another coffee sounds good...  (Expect some more posts...  LOL )

Navigation

[0] Message Index

[#] Next page

Go to full version