Syntax help.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Syntax help.

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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>";

?>
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

Thanks. I tried it, but I'm getting a default of 'Alabama' when my $row['state'] is 'LA'.

Any thoughts?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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 ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hehehe, silly brit, Los Angeles isn't a state.. :P
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

But but but.....he said
$row['state'] is 'LA'.
:o
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Louisiana.. Los Angeles is in California. :D
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post 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)
Post Reply