PHP Programming > General PHP Help

I need support with increment

<< < (2/3) > >>

shadowloud:

--- Quote from: ErnieAlex on March 22, 2012, 01:54:05 AM ---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: 

--- End quote ---

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..


--- Quote ---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...
--- End quote ---

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

ErnieAlex:
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...

shadowloud:

--- Quote from: ErnieAlex on March 22, 2012, 05:23:07 PM ---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...

--- End quote ---

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 Code: ---$skin1 = $_GET['skin']; 
$skin2 = $_GET['hair']; 
$skin3 = $_GET['face']; 

$type2 = $_GET['armor'];
$type1 = $_GET['hat']; //

$item1 = $_GET['item1']; 
$item2 = $_GET['item2']; 
--- End code ---


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


--- PHP Code: ---<?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);

?>
--- End code ---


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.

shadowloud:
Here is what I tried so far. I made an "items" table. Which will contains ID and item types.


--- PHP Code: ---
$itemid_query = $query->prepare('SELECT ID FROM items WHERE 1');
$itemid = $itemid_query->fetch();
$itemid_query->closeCursor();

--- End code ---



--- PHP 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 ++$itemid['ID'] ?>"); 

}
--- End code ---


Still not working.

ErnieAlex:
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...

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version