PHP Query

If the Ivory Tower is the brain of the board, and the Transformers discussion is its heart, then General Discussions is the waste disposal pipe. Or kidney. Or something suitably pulpy and soft, like 4 week old bananas.

Moderators:Best First, spiderfrommars, IronHide

Post Reply
User avatar
Kaylee
Big Honking Planet Eater
Posts:4071
Joined:Thu Oct 26, 2000 12:00 am
::More venomous than I appear
Location:Ashford, Kent, UK.
Contact:
PHP Query

Post by Kaylee » Sat Jul 29, 2006 9:53 am

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.

User avatar
Legion
Over Pompous Autobot Commander
Posts:2739
Joined:Mon Jan 15, 2001 12:00 am
Location:The road to nowhere

Post by Legion » Sat Jul 29, 2006 11:10 am

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,
if (!(isset($myVariable))){
//Do nothing
}
else {
$myVariable == 1;
}
is saying, if myVariable ISN'T set, do nothing, otherwise make it 1.

you should have :
if (isset($myVariable)){
//Do nothing
}
else {
$myVariable = 1;
}
i think.

User avatar
Kaylee
Big Honking Planet Eater
Posts:4071
Joined:Thu Oct 26, 2000 12:00 am
::More venomous than I appear
Location:Ashford, Kent, UK.
Contact:

Post by Kaylee » Sat Jul 29, 2006 11:16 am

That's true, thank you :) sadly that was a typo on my part though, my actual php file has it the right way round and still doesn't work :( {sob}

User avatar
Ozz
Help! I have a man for a head!
Posts:885
Joined:Mon Sep 22, 2003 11:00 pm
Location:Poland
Contact:

Post by Ozz » Sat Jul 29, 2006 12:26 pm

I dressed the code from the first post in the HMTL tags like that:

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

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>"
Image

User avatar
Kaylee
Big Honking Planet Eater
Posts:4071
Joined:Thu Oct 26, 2000 12:00 am
::More venomous than I appear
Location:Ashford, Kent, UK.
Contact:

Post by Kaylee » Sat Jul 29, 2006 2:00 pm

Aha, I did wonder if it was a server configuration error.

Indeed yes, $_GET() works fine. Okay, so now I know the scope and nature of the issue :) Thank you very much ozz :)

Post Reply