| | |
A question about variable
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2007
Posts: 47
Reputation:
Solved Threads: 1
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:
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.
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)
<html> <body> <?php $array[3]; $i=0;?> <BUTTON type="pushbutton" onClick="<?php $i++?>" ><img alt="Forwards"/ > </BUTTON> <?php echo"<br/>".$i ; ?> </body> </html>
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.
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.
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.
When it says free and it why is it so time consuming?
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.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
•
•
Join Date: Jul 2007
Posts: 47
Reputation:
Solved Threads: 1
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:
BuddyLee17,
at the moment I'm trying to handle php,not JavaScript.
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)
<html> <body> <?php function add () { $fp=fopen("position.dat","r"); $i=fgetc($fp); fclose($fp); $i++; $fp=fopen("position.dat","w"); fwrite($fp,$i); fclose($fp); } $array[3]; $i=0; $fp=fopen("position.dat","w"); fwrite($fp,$i); fclose($fp); ?> <BUTTON type="submit" onClick="<?php add()?>" "><img alt="Forwards"/ > </BUTTON> <?php $fp=fopen("position.dat","r"); echo "<br/>".fgetc($fp) ; fclose($fp); ?> </body> </html>
BuddyLee17,
at the moment I'm trying to handle php,not JavaScript.
Last edited by Arctic wolf; Jun 17th, 2009 at 9:54 pm.
•
•
Join Date: Jul 2007
Posts: 47
Reputation:
Solved Threads: 1
Hi everyone,
I changed the code to the following:
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.
I changed the code to the following:
PHP Syntax (Toggle Plain Text)
<html> <body> <?php function add () { $fp=fopen("position.dat","r"); $i=fgetc($fp); fclose($fp); $i++; $fp=fopen("position.dat","w"); fwrite($fp,$i); fclose($fp); } $array[3]; ?> <INPUT TYPE="BUTTON" onClick="<?php add();?>" ><img alt="Forwards"/ > </INPUT> <?php $fp=fopen("position.dat","r"); echo "<br/>".fgetc($fp) ; fclose($fp); ?> </body> </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.
![]() |
Similar Threads
- (Newbie question) Variable problem (Assembly)
- How to use the 2nd variable I captured?? (PHP)
- General C question (variables) (C)
- typing xxx to exit (C++)
- First Attempt at Arrays (Java)
- Help with sprintf in C (C)
- MFC Tab controls (C)
Other Threads in the PHP Forum
- Previous Thread: error_404 page problem
- Next Thread: Mail() Function
| Thread Tools | Search this Thread |
.htaccess alerts apache api archive array autocomplete beginner binary broken cakephp checkbox class cms code convert cron curl database dataentry date display duplicates dynamic echo email emptydisplayvalue error execute explodefunction file files firstoptioninphpdroplist folder form forms function functions google hack href htaccess html htmlspecialchars image include insert ip javasciptvalidation javascript joomla keywords limit link login mail matching menu methods mlm multiple mysql network object oop paypal pdf php problem query radio random recursion recursive redirect remote script search securephp server sessions shot sms source space sql subscription syntax system table tutorial tutorials update upload url validator variable video web youtube






