Click to See Complete Forum and Search --> : Banging my head on the desk...


leaded
10-01-2005, 06:05 PM
Why is it that the following code...

<?php
$states=array("AK","AL","AR","AZ","CA","CO","CT","DE","...etc");
foreach($states as $key => $value)
{
if ($_SESSION[c_state]=$value) echo " <option value=\"$value\" selected>$value</option>\n";
else echo " <option value=\"$value\">$value</option>\n";
}
?>


Produces the following output?

<option value="AK" selected>AK</option>
<option value="AL" selected>AL</option>
<option value="AR" selected>AR</option>
<option value="AZ" selected>AZ</option>

<option value="CA" selected>CA</option>
<option value="CO" selected>CO</option>
<option value="CT" selected>CT</option>
<option value="DE" selected>DE</option>
<option value="DC" selected>DC</option>
<option value="FL" selected>FL</option>

...etc...


... when $_SESSION[c_state] doesn't equal ANYTHING? I'm so frustrated... Thanks for helping, in advance.

rch10007
10-01-2005, 08:27 PM
First, you need to use: session_start() before attempting to use any $_SESSION variable.

Second, your 'if' statement isn't enclosed in brackets { } so therefore it is basically being skipped over.

leaded
10-01-2005, 09:21 PM
That's just a code snippet. I have the session_start at the top. Also, according to the php.net documentation, I don't need the curly brackets if there's one statement after the if. That format works on other parts of my code too...

NogDog
10-01-2005, 10:03 PM
Need comparison '==' instead of assignment '=' :

if ($_SESSION[c_state]==$value)

And to be really correct (more info (http://www.php.net/manual/en/language.types.array.php#language.types.array.foo-bar)):

if ($_SESSION['c_state']==$value)

rch10007
10-01-2005, 10:06 PM
missed the assignment operator

NogDog
10-01-2005, 10:12 PM
missed the assignment operator
After you've made that mistake a few hundred times, you learn to look for it when your if blocks execute more than they should. :)

leaded
10-02-2005, 10:33 PM
Thank you!! I should've known that. I don't do PHP that often (or any programming for that matter), but I totally understand it. Because I wasn't doing a comparison, but rather an assignment, I was giving my session variable a value every time. Duh!

agent_x91
10-03-2005, 10:48 AM
Hehe mistaking == and = is all too common. Oddly enough I've never made that mistake:confused: But I dread the time that I do, because I'll never think to look for it:D