Page 1 of 1

Syntax help.

Posted: Mon Jul 05, 2004 9:13 pm
by dardsemail
This is pretty basic, but I've never done it before and am not familiar with the syntax. I'm trying to create a list menu that has the state/provincial abbreviations, but I'd like to have the default value appear based on a value from a recordset. I don't think I have the syntax right. I've done this with input boxes, but seem to be stuck on the list menu. I've left out most of the state values to facilitate the ease of reading the code...

Here's the code:

Code: Select all

<?php
		echo "\n<tr>";
		echo "\n\t<td><h3>";
		echo "State";
		echo "</td><td>";			
		echo "<select name=",state," value='";
		echo $row['state'];
        echo "'><option value="AL">Alabama</option>
            <option value="AK">Alaska</option>
            <option value="AS">American Samoa</option>
            <option value="AZ">Arizona</option>
          </select>";
		echo "</h3></td>";
		echo "</tr>";

?>
Thanks!

Posted: Mon Jul 05, 2004 9:56 pm
by John Cartwright

Code: Select all

<?php

echo "<tr>
            <td>
               <h3> State </h3>
            </td>
            <td>          
               <select name='state' value='".$row['state']."'>
               <option value='AL'>Alabama</option> 
               <option value='AK'>Alaska</option> 
               <option value='AS'>American Samoa</option> 
               <option value='AZ'>Arizona</option> 
               </select>
            </td>
         </tr>";

?>

Posted: Tue Jul 06, 2004 1:58 pm
by dardsemail
Thanks. I tried it, but I'm getting a default of 'Alabama' when my $row['state'] is 'LA'.

Any thoughts?

Posted: Tue Jul 06, 2004 2:23 pm
by markl999
You need to be doing this as you loop around the states to create the options really, otherwise it gets ugly.
Eg:

Code: Select all

<?php
$states = array(
  'AB' => 'Alabama',
  'AK' => 'Alaska',
  'LA' => 'Los Angeles',
  'AS' => 'American Samoa',
  'AZ' => 'Arizona'
);
$row['state'] = 'LA';
?>
<select name="state">
<?php
foreach($states as $key=>$val){
  $selected = ($row['state'] == $key) ? ' selected' : '';
  echo '<option value="'.$key.'"'.$selected.'>'.$val.'</option>';
}
?>
</select>
I made up the state codes as i don't know them ;)

Posted: Tue Jul 06, 2004 2:34 pm
by feyd
hehehe, silly brit, Los Angeles isn't a state.. :P

Posted: Tue Jul 06, 2004 2:37 pm
by markl999
But but but.....he said
$row['state'] is 'LA'.
:o

Posted: Tue Jul 06, 2004 2:41 pm
by feyd
Louisiana.. Los Angeles is in California. :D

Posted: Tue Jul 06, 2004 2:42 pm
by dardsemail
Thanks. I'll give it a go.

Don't worry about the LA thing. It actually stands for Louisiana which also really isn't a state unless you consider a third world country in the southern US a state. ;-)

(Coming from a Canadian ex-pat currently living here in LA)