Yo,
I'm having some irksome difficulties with a little PHP work. In short the problem seems to run:
[Pretend we have mySQL connection code here]
if (!(isset($myVariable))){
//Do nothing
}
else {
$myVariable == 1;
}
echo "Variable: " . $myVariable;
$mySecondVar = $myVariable + 1;
echo "<a href='{$_SERVER['PHP_SELF']}?myVariable=$mySecondVar'>Click</a>"
[mySQL close connection code here]
My plan is simply to feed the variable back to this page and increment the myVariable value- i.e. every time you press 'CLICK' the variable goes up by one. It's just not playing ball (I don't think I made any typos copying it out here).
Far as I can see, if the myVariable is already set (i.e. its 5) we do nothing, and display 5. If it's not set, we set it to 1. mySecondVar is automatically set to whatever myVariable is plus 1. When we click the hyperlink, we feed a new myVariable value back into this which is equivalent to the current value of mySecondVar.
It's just doing nothing- myVariable is consistently just 1.
Any ideas what I'm doing wrong? I'm guessing there's some intricacy to the isset() function which I'm messing up.
PHP Query
Moderators:Best First, spiderfrommars, IronHide
- Legion
- Over Pompous Autobot Commander
- Posts:2739
- Joined:Mon Jan 15, 2001 12:00 am
- Location:The road to nowhere
Err...
so if, myVariable is already definied, you don't want anything to happen, right? if it's not defnied then set it to one, is that right?
if so, i think you've got your logic mixed up,
you should have :
so if, myVariable is already definied, you don't want anything to happen, right? if it's not defnied then set it to one, is that right?
if so, i think you've got your logic mixed up,
is saying, if myVariable ISN'T set, do nothing, otherwise make it 1.if (!(isset($myVariable))){
//Do nothing
}
else {
$myVariable == 1;
}
you should have :
i think.if (isset($myVariable)){
//Do nothing
}
else {
$myVariable = 1;
}
- Ozz
- Help! I have a man for a head!
- Posts:885
- Joined:Mon Sep 22, 2003 11:00 pm
- Location:Poland
- Contact:
I dressed the code from the first post in the HMTL tags like that:
and it gives my this after I click in the link:
Variable: Click
*click*
Variable: 1Click
*click*
Variable: 2Click
*click*
Variable: 3Click
*click*
Variable: 4Click
and so on...
So I guess that's what you wanted and the code actually is correct.
However it works only when the register_globals in the php.ini file is set on. I'm pretty sure that the problem is caused because php on the server you are using has registered_globals turned off and you should access the variable $myVariable as $_GET["myVariable"]. Otherwise, isset() will always say that it is not set.
Code: Select all
<HTML><BODY>
<?
if (!(isset($myVariable))){
//Do nothing
}
else {
$myVariable == 1;
}
echo "Variable: " . $myVariable;
$mySecondVar = $myVariable + 1;
echo "<a href='{$_SERVER['PHP_SELF']}?myVariable=$mySecondVar'>Click</a>"
?>
</BODY></HTML>
Variable: Click
*click*
Variable: 1Click
*click*
Variable: 2Click
*click*
Variable: 3Click
*click*
Variable: 4Click
and so on...
So I guess that's what you wanted and the code actually is correct.
However it works only when the register_globals in the php.ini file is set on. I'm pretty sure that the problem is caused because php on the server you are using has registered_globals turned off and you should access the variable $myVariable as $_GET["myVariable"]. Otherwise, isset() will always say that it is not set.
Code: Select all
if (!(isset($_GET["myVariable"]))){
//Do nothing
}
else {
$myVariable == 1;
}
echo "Variable: " . $_GET["myVariable"];
$mySecondVar = $_GET["myVariable"] + 1;
echo "<a href='{$_SERVER['PHP_SELF']}?myVariable=$mySecondVar'>Click</a>"