943,616 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 491
  • PHP RSS
Jun 17th, 2009
0

A question about variable

Expand Post »
Hello,
as you all already know from my previous question I'm new in PHP(by the way,thanks again for the previous help),
I am trying to build a simple "walking" program,so I'll have "forwards",
"backeards","right" and "left" buttons and when I click on one of them the picture on the page will change.
I started with a simple variable expirement,I have one button on the page and when I click it I expect the variable to grow by 1(in the future that var will show a place inside an array),but from some reason the code doesn't work,here is the code:
PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <body>
  3. <?php
  4. $array[3];
  5. $i=0;?>
  6. <BUTTON type="pushbutton" onClick="<?php $i++?>" ><img alt="Forwards"/ > </BUTTON>
  7. <?php
  8. echo"<br/>".$i ; ?>
  9. </body>
  10. </html>
The described array will be used later.
The problem with the code is that the printed variable value is always 1,
it doesn't equals 0 like it should before I push the button and it doesn't turn to 2 after I push the button.
Can please anyone tell me how to fix it.
And also,I have some C/C++ programing expirience,so maybe it gives me trouble when I try to program on php,any php profi advice about my project would be great.

Thank you,
A.
Similar Threads
Reputation Points: 15
Solved Threads: 1
Light Poster
Arctic wolf is offline Offline
47 posts
since Jul 2007
Jun 17th, 2009
0

Re: A question about variable

try something like this:
I don't think I have this down yet but this will give you some direction
$i = 0;
$j = 0;
$k = 0;
$l = 0;
Though it is not necessary to initialize the variables, but I don't think it hurts anything.

switch ($submit) {
case left
$i = ++$i;
echo $i;
break;
case (right)
$j = ++$j:
echo $j;
break;
case (forward)
$k = ++$k;
echo $k;
break;
case (backward)
$l = ++$l
echo $l;
break;
}

Then when you make buttons your submits have the left, right , forward, and backwards assigned to them.
I hope this puts you in the right direction.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathman54 is offline Offline
4 posts
since Nov 2008
Jun 17th, 2009
0

Re: A question about variable

PHP does not work like C/C++. When a page refreshes its previous state is gone, it has no knowledge of what happened before. You must save variables if you want to carry them across pageloads with either a database, sessions, cookies or files.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jun 17th, 2009
0

Re: A question about variable

You need JavaScript (unless you're comfortable with the page reloading on each change).
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Jun 17th, 2009
0

Re: A question about variable

Mathman,I'll check it but I dont think it will help if the language doesn't remember variables values...
ShawnCplus,
I tried to save the variable into a file,but it didn't help,
I still have the same problem,here is the code:
PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <body>
  3. <?php
  4. function add ()
  5. {
  6. $fp=fopen("position.dat","r");
  7. $i=fgetc($fp);
  8. fclose($fp);
  9. $i++;
  10. $fp=fopen("position.dat","w");
  11. fwrite($fp,$i);
  12. fclose($fp);
  13. }
  14. $array[3];
  15. $i=0;
  16. $fp=fopen("position.dat","w");
  17. fwrite($fp,$i);
  18. fclose($fp);
  19.  
  20.  
  21. ?>
  22. <BUTTON type="submit" onClick="<?php add()?>" "><img alt="Forwards"/ > </BUTTON>
  23. <?php
  24. $fp=fopen("position.dat","r");
  25. echo "<br/>".fgetc($fp) ;
  26. fclose($fp);
  27. ?>
  28. </body>
  29. </html>
  30.  

BuddyLee17,
at the moment I'm trying to handle php,not JavaScript.
Last edited by Arctic wolf; Jun 17th, 2009 at 9:54 pm.
Reputation Points: 15
Solved Threads: 1
Light Poster
Arctic wolf is offline Offline
47 posts
since Jul 2007
Jun 18th, 2009
0

Re: A question about variable

Hi everyone,
I changed the code to the following:
PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <body>
  3. <?php
  4. function add ()
  5. {
  6. $fp=fopen("position.dat","r");
  7. $i=fgetc($fp);
  8. fclose($fp);
  9. $i++;
  10. $fp=fopen("position.dat","w");
  11. fwrite($fp,$i);
  12. fclose($fp);
  13. }
  14. $array[3];
  15. ?>
  16. <INPUT TYPE="BUTTON" onClick="<?php add();?>" ><img alt="Forwards"/ > </INPUT>
  17. <?php
  18. $fp=fopen("position.dat","r");
  19. echo "<br/>".fgetc($fp) ;
  20. fclose($fp);
  21. ?>
  22. </body>
  23. </html>

And now something weird happens,
the printed variables value increases every time the page reloads
WITHOUT me clicking on the button!
And when I do click on the button the code ignores it(the variables value doesn't change).
Why does it happen?

A.
Reputation Points: 15
Solved Threads: 1
Light Poster
Arctic wolf is offline Offline
47 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: error_404 page problem
Next Thread in PHP Forum Timeline: Mail() Function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC