I need support with increment


#1

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; ?>”);

}[/code]

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

<input value=">>" name="next_hair" type="button" onClick="sumhair()"/> <input value="<<" name="next_hair" type="button" onClick="neghair()"/>

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]$un = 1;

<? echo ++$un; ?> // This being used in the links written within JS script[/php]

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.


#2

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


#3

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

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…

:slight_smile:


#4

++ always.

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


#5

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 )


#6

Ok, thank you for confirming that $un=$un+1; will always adds just one. I wasn’t sure if it was meant to work that way or not … I also must say that even after reading this page you linked (google’d before i posted here and saw it) I still don’t get the difference between ++$ and $++ to be honest…

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…

I indeed am not. I wasn’t aware I had to. Can you give me some clue? What should I be doing in order to create an endless increament? Would this be done throught “while”, “function” … $_SESSION maybe? I am clueless lol


#7

Okay, a few more hints… The issue that I was trying to explain about the ++ is that an experience programmer will use this, but, hard for beginners to get.

First, if the ++ is BEFORE the variable, it add the 1 to the value BEFORE it does anything else.

So, $x= ++$y; Would take $y, increment it, and store it into $x… Just as you would expect.

If the ++ is AFTER it the increment is done after it is used…

So, $x= $y++; Would take $y, store it into $x, then increment it. So, this can confuse beginners because they just see an increment and do not see it is done late. Hope that makes sense.

Also, ++ is used for ANY variable. So, the use can get confusing if the variable is a string. Another area that always messes up beginners… So, that’s that on ++… LOL

Now, on to your code. First, you have HTML, PHP and Javascript. PHP is handled SERVER-SIDE. It never gets to the CLIENT-SIDE. (Browser) RIGHT-CLICK on any page in the world and select VIEW-SOURCE and you do not see any PHP code. This is because PHP is done SERVER-SIDE before it is sent to the browser to be rendered onto the screen.

So, the PHP code you are displaying is using a variable that is on the server. You press a button which goes off the Javascript by way of the ONCLICK button option. All normal to this point.

Next, now inside the Javascript which is CLIENT-SIDE (in browser), which sets some values in the field on your page called by name “avatar”. This item is not listed anywhere in what you posted. It then ends. So, nothing was saved or stored. The new value of $un does not exist, not stored… So, you either must store this in a database, save it in a SESSION variable or pass it with a variable. It appears that you were trying to store data from the avatar.php page with various options. Not sure what all that was about.

So, keep in mind that the PHP only exists on your server, not in live browser page. It is handled before the browser sees it and then outputs whatever and combines with HTML and Javascript and CSS code into a page that is then sent to the browser which “renders” it onto the screen.

Not sure if that helps or makes it worst , but, hope it helps…


#8

Oh! Forgive me… there is indeed a part I forgot to give you! I put it into my include file, so I forgot it was there. hehe.

[php]$skin1 = $_GET[‘skin’];
$skin2 = $_GET[‘hair’];
$skin3 = $_GET[‘face’];

$type2 = $_GET[‘armor’];
$type1 = $_GET[‘hat’]; //

$item1 = $_GET[‘item1’];
$item2 = $_GET[‘item2’]; [/php]

If you are curious and wish to see more from Avatar.php :

[php]<?php

include(“includes.php”);

$avatar = imagecreatefromPNG(“img/blank.png”);

$item____2 = imagecreatefrompng(‘img/item2/’.$item2.’.png’);
$skin_face = imagecreatefrompng(‘img/face/’.$skin3.’.png’);
$skin_avatar = imagecreatefrompng(‘img/skin/’.$skin1.’.png’);
$skin_hair = imagecreatefrompng(‘img/hair/’.$skin2.’.png’);
$hat_avatar = imagecreatefrompng(‘img/hat/’.$type1.’.png’);
$armor_avatar = imagecreatefrompng(‘img/armor/’.$type2.’.png’);
$item____1 = imagecreatefrompng(‘img/item1/’.$item1.’.png’);

imagecopy($avatar, $item____2, 0, 0, 0, 0, 48, 64);
imagecopy($avatar, $skin_avatar, 0, 0, 0, 0, 48, 64);
imagecopy($avatar, $skin_face, 0, 0, 0, 0, 48, 64);
imagecopy($avatar, $skin_hair, 0, 0, 0, 0, 48, 64);
imagecopy($avatar, $hat_avatar, 0, 0, 0, 0, 48, 64);
imagecopy($avatar, $armor_avatar, 0, 0, 0, 0, 48, 64);
imagecopy($avatar, $item____1, 0, 0, 0, 0, 48, 64);

header(‘Content-Type: image/png’);
imagepng($avatar);
imagedestroy($avatar);

?>[/php]

Your explanations about increaments were helpful, thank you. I understand better how it works now. Since I am going to store items into a database at a later time, perhaps should I consider using database way right now to make this happens. Not going to ask you to write it for me, but if you wish to provide sample of what it could looks like, or links to articles providing tutorial for this, you are very welcome.


#9

Here is what I tried so far. I made an “items” table. Which will contains ID and item types.

[php]
$itemid_query = $query->prepare(‘SELECT ID FROM items WHERE 1’);
$itemid = $itemid_query->fetch();
$itemid_query->closeCursor();
[/php]

[php]function sumhair() {

document.getElementsByName(“avatar”)[0].setAttribute(“src”, “avatar.php?skin=1&armor=1&hat=1&face=1&item1=1&item2=1&hair=<? echo ++$itemid['ID'] ?>”);

}[/php]

Still not working.


#10

Well, if you are planning on using a database, it is always easier to code pages once the database is planned out and filled with some sample data. Then, the pages pull everything except pictures from the database not being created in the pages. Much easier to play with…

So, anyways, back to your page. You “create” the current versions of what is displayed from inside the passed arguments. “avatar.php?skin=1&armor=1&etc…” So these are pulled from the page in the PHP code with the $_POST[‘skin’], $_POST[‘armor’], etc… Then, you can set these are the defaults. But, if you are going to post back to the same page and display the new ones, you must POST the page back to the form page with the new settings. The page would then read the items again, getting any changes made.
Not sure if your javascript is sending the values.

I have a question about your creation of the avatars. How many skin, armor, etc are you allowing? If there 7 items with 5 options for each, that would be only 35 images. Not many. You could just store them each as avatar111111.jpg or whatever to avatar555555.jpg. Then, just use the actual picture instead of all the image drawings. Not sure about your use of this of course, only seeing a little of your code. Looks interesting. I like avatar’s… LOL

Okay, so, your Javascript looks like it stays the same as you never send the variables…
So, this: "avatar.php?skin=1&armor=1&hat=1, etc
Is not : “avatar.php?skin=” & skin & “,armor=” & armor & “,hat=” hat;

Looks like the first version is sending the same number to the PHP page,
the second version is using the actual variables with the rest of the text the same…
I do not have all your code, so just guessing on how to write that last line. Just a wild guess.
You have to create the URL SRC so that it is the URL plus the arguments names= variables of the new values that were pulled with the $_POST commands and changed by next buttons…

Gee, that sounds mixed up, but, hope that makes sense to you… Good luck and let us know…


#11

Ah, I do know about that and I did it. It is working already, since I can change my hairs to “2” at least. But I sticked those with 1 still since I am only trying to make hairs work first with the following line :

[php]<? echo ++$itemid['ID'] ?>[/php]

Which is not working as I would like. Used to be :

[php]<? echo ++$un; ?>[/php]

I am attempting to make the link echo the item id within my item table.

[php]try
{
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$query = new PDO(‘mysql:host=localhost;dbname=super’, ‘mario’, ‘bros’, $pdo_options);

$itemid_query = $query->prepare('SELECT ID FROM items LIMIT 1,2');
$itemid = $itemid_query->fetch();
$itemid_query->closeCursor();

}
[/php]


#12

I got it… I know a way to fix this. I went back over all your posts (and mine, too) I am sorry, I was blind!

The screen dump of your sample shows me what I need to know…

Sorry again for taking so long to fix it… LOL…

Okay, what you want to do is load the page. when they select one of the items, it is a button.
Make the button the HREF to the new page with the changes already inside the button…
No need for any Javascript… Just the PHP posing page…

So, let’s just talk about the HAIR button…

Basically it is like this:

change them to something like this: (Not actually, but, for talking right now…)

So, to explain that. Wrapped around the button would be a direct link to send you to the avatar.php page if you press the button. The “&hair=” argument will hold the previous and next values for HAIR. I put 5 for the previous figuring you have 5 hair samples, and 2 for the next version. These are just for the sample for you to look at…

Next, inside of the avatar.php file, you would read all the values. Then, where you normally print/echo the button code above, instead you set up each of the arguments in some small code that alters each and create a new button HREF with the new values…

Did that make sense? I could make up some dummy code, but give it a try first. If you need help, post the complete two pages to me in a private-message and I will fix it for you… I think this would work well for you… Let me know if you agree or if you want me to look further into your current way…


#13

My apologizes, it somewhat makes me sad to end up asking you to make it lol, but I must say that I can’t end up getting it to work. Thing is, if I remove my JS, then buttons will href directly on avatar.php rather than only switch image to another… I don’t know how to do that with PHP. Infact, I thought it was impossible. ::slight_smile:

I sent you my while code so you can investiguate it better and find out.

Meanwhile I tried to create a function… which didn’t work either.

[php]function itemid($query) {
$itemid_query = ‘SELECT ID FROM items ORDER BY ID’;
foreach ($query->query($itemid_query) as $id) {
echo $id[‘ID’];
}
}

$itemid = itemid(1);[/php]


#14

Thanks for the code, I will look at it later. I PM’d you explaining. Give me a few more hours… Sorry.