Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with if statement 3

Status
Not open for further replies.

jgurgen

Programmer
Mar 9, 2006
192
0
0
US
for some reason i just dont understand what is wrong with my code

WORKS
Code:
<select name="Position" id="Position">
								<?PHP 

echo '<option value="F">F</option>
<option value="D" selected="selected">D</option>
<option value="G">G</option> '
							 	?>
								</select>

DOESNT WORK
Code:
<select name="Position" id="Position">
								<?PHP 
								if($Position=="F")
								{
								echo '<option value="F">F</option>
								<option value="D" selected="selected">D</option>
                                <option value="G">G</option> '
								}					
							 	?>
								</select>
 
For a completely different reason, I can't understand it either. Would you like to tell us what is not working? As in, what errors you get, what happens, what you expect to happen and so on. I would like to know where your $Position is defined.
 
Wouldn't the first inclination be to use a debug approach to "echo $Position" before you enter the loop? Perhaps you've mis-spelled the variable name or it doesn't have the value you expect?

D.E.R. Management - IT Project Management Consulting
 
good practice would dictate that you terminate each php command with a semi-colon too.
i think that a semicolon is implied by closing php tags (first example) but in the second example you have curly braces interrupting the implicit semi-colon.
 
These lines:

<select name="Position" id="Position">
<?PHP
if($Position=="F")
brings to mind a few things:

[ul][li]PHP variable names are case-sensitive. $Position is a different variable from $position.[/li][li]By default, PHP does not put values from submitted forms into variables by the same name. Your value may be in $_POST['Position'] or $_GET['Position'], but not in $Position.[/li][li]You are referencing a variable name immediately after outputting a SELECT tag by the same name. The form must be sent to the browser, interacted with by the user, and submitted back to the server before any variables from that form can be availalbe in PHP[/li][li]If you reference a non-instantiated variable in the condition of an if-statement, PHP will throw a warning. You may not be seeing that warning due to php.ini settings. Check the values for display_errors and error_reporting to see if they would allow a warning or error to be seen from these lines[/li][/ul]







Want the best answers? Ask the best questions! TANSTAAFL!
 
the page just comes up white with the if statement in the code.

i had the semicolon in there and it still didnt work.

ill check $Postions return value but im 99% sure that it is returning correctly.
 
So the PHP code isn't being executed?

Does your file name have a .php file extension? Under the default configuration of PHP with most web browsers, that's required.


Want the best answers? Ask the best questions! TANSTAAFL!
 
I copied the code you posted, and pasted it and ran it. Without the already pointed semi-colon I get an error:

PHP said:
Parse error: syntax error, unexpected '}', expecting ',' or ';' in...

Now With the semi-colon there and if $Position is equal to "F" Then i get a dropdown. with three options.

If $Position is not equal to "F", then i get and empty select box.

That is the correct behavior to expect, so in light of this, I suspect that whatever else is happening before the code you posted is causing this piece of code to not actually execute. So what is going on before this? Is there a While loop or something before it? another condition that is not being met? what exactly is going on?

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
of course its .php

yeah there is a lot of code going on before it. Ill have to thumb through it to check it.

what program did you use to get that. i dont have php installed locally so i cant use php expert editor
 
Even if you are uploading to a webserver and testing it, you should get the errors unless your php.ini has the option turned off (which is probably the case for security).

If you change the option to true, you'll probably see the errors he's talking about.

If you can't modify php.ini yourself, you can try using error_reporting()
 
so if i put error_reporting(E_ALL) in my php files i should see my errors?
 
unless display_errors is set to off. check the output of phpinfo() to determine the facts.
 
And if your developing, i suggest you install a local testing server, that somewhat closely mirrors the setup in your deployment server.

I for example have Apache Web server, PHP, and MYSQL running on my laptop to test everything before I upload it.





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
display errors is set to off

i tried this code on the same page

<?PHP
$x="1";
if(x=="1")
{ echo 'if'; }
else
{ echo 'else'; }
?>

it returns else

something is f'd up
 
never mind had x=="1" not $x=="1"
it works now
 
change
Code:
  $x="1";
 if([red]x[/red]=="1")

to

  $x="1";
 if([red]$[/red][blue]x[/blue]=="1")

You forgot the dollar sign before the variable name. Try it again.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
weird its now working no problem...

dont know why but i didnt chagne anything. THanks for all the help
 
Feel free to correct me if I'm wrong, but I believe you can use ini_set() to change the options for both display_errors and error_reporting which *should* override the php.ini during the execution of the script.

I have not testing this myself though, because my server allows errors, and my localhost is set up how I like as well.
 
only if there are no parse errors in the script (which would kill the script before it gets run).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top