Form to edit mysql records. how to do select field

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

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form to edit mysql records. how to do select field

Post by Celauran »

Yeah, that semicolon after ['state'] shouldn't be there
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Form to edit mysql records. how to do select field

Post by donny »

thank you that works.. now can you please show me how to set the value as like CA and the option show California.. CA is what goes into the DB so i want them to see the full name in the options.. can you show me the example of that array
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form to edit mysql records. how to do select field

Post by Celauran »

You'll need to change your array around a little.

Code: Select all

$states = array(
	'CA' => 'California',
	'NY' => 'New York',
	'VT' => 'Vermont',
);
And then your foreach will become

Code: Select all

<?php foreach ($states as $abbr => $state): ?>
<option value="<?= $abbr; ?>" <?= ($abbr == $user['state']) ? 'selected="selected"' : ''; ?>>
    <?= $state; ?>
</option>
<?php endforeach; ?>
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Form to edit mysql records. how to do select field

Post by donny »

thank you that works..

one more thing.. when they're done editing their page i have another page that displays their state from the db . it shows just the abbr because thats whats stored in the database. can you show me how to do the same thing and show the full state name .

thanks so much
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form to edit mysql records. how to do select field

Post by Celauran »

Use the same states array. Assuming your user's values are stored in $user,

Code: Select all

echo $states[$user['state']];
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Form to edit mysql records. how to do select field

Post by donny »

thank you so much you saved me a lot of time and headache!!!!
Post Reply