Page 1 of 2

Form to edit mysql records. how to do select field

Posted: Fri Aug 29, 2014 1:05 pm
by donny
hello,

i have a page where people can edit their profile information. the page pulls data from a database and puts it right into text input forms so they can change what they need to and then click update. the process page simply updates the database row information. all that works fine but i need to know how make it so they can edit a field that was a select field. so when they go to the edit page there will be a select field for their state and the state they already have in the database will automatically be selected.. so if they go to edit their info their name and address lines appear in text inputs and their state will be a drop down select field option with their current state already selected.

anybody can point me in the right direction so i can make this work?

thank you.

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

Posted: Fri Aug 29, 2014 1:13 pm
by Celauran
You need to grab the elements for the select from wherever they're defined, iterate over them to generate the select, and compare against the user's profile.

Pseudo code, obviously, but this is the idea.

Code: Select all

<select name="foo">
	<?php foreach ($foo_values as $value): ?>
	<option value="<?= $value->id; ?>" <?= ($value->id == $user->foo) ? 'selected="selected"' : ''; ?>>
		<?= $value->name; ?>
	</option>
	<?php endforeach; ?>
</select>

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

Posted: Fri Aug 29, 2014 1:17 pm
by donny
so i would put all the states into an array as $foo_values?

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

Posted: Fri Aug 29, 2014 1:18 pm
by Celauran
You've already got the states listed somewhere, no? Just iterate over that array.

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

Posted: Fri Aug 29, 2014 1:20 pm
by donny
i just had them on the html page on the form code. sorry i am new this is gunna take me some time to figure out lol

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

Posted: Fri Aug 29, 2014 1:22 pm
by Celauran
That's something you're going to want to reuse, so I'd move that array either to a config file that's available everywhere, into the database, or into a method that returns an array. Anything that's going to make them easily and globally accessible.

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

Posted: Fri Aug 29, 2014 1:24 pm
by donny
there is only going to be 1 or 2 pages with the states field so i can do it on the page i need it on.. can you please show me an example with like 3 states in it.. then tell me what i need to assign to variables to make them work.. like $thisvariable is the state in the database . i need to make sure thats set.

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

Posted: Fri Aug 29, 2014 1:28 pm
by donny
also what will the final option tag look like with that script you made? the whole selected="selected" got me confused.

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

Posted: Fri Aug 29, 2014 1:29 pm
by Celauran

Code: Select all

<?php $states = ['New Hampshire', 'New York', 'Vermont']; ?>

<form>
	...
	Stuff
	...
	<select name="state">
		<?php foreach ($states as $state): ?>
		<option value="<?= $state; ?>" <?= ($state == $user['state']) ? 'selected="selected"' : ''; ?>>
				<?= $state; ?>
		</option>
		<?php endforeach; ?>
	</select>
	...
</form>

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

Posted: Fri Aug 29, 2014 1:30 pm
by Celauran
donny wrote:also what will the final option tag look like with that script you made? the whole selected="selected" got me confused.
That's how you specify which item in the select list is currently selected. I'm comparing the current value against the user's value on each iteration and, when they match, marking the current item as selected.

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

Posted: Fri Aug 29, 2014 1:32 pm
by donny
ok what variable needs to be set as the state already in the database

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

Posted: Fri Aug 29, 2014 1:43 pm
by Celauran
Depends on how it's stored. The code I posted assumed you retrieved the user from the database, stored it in an array called $user, and you stored the state value in a DB column called state.

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

Posted: Fri Aug 29, 2014 1:49 pm
by donny
i am getting an error with this string.


Parse error: syntax error, unexpected '[' in /home/newyorki/public_html/order/order/shipping_info_edit.php on line 20

line 20 = $states = ['NY', 'CA', 'VT'];

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

Posted: Fri Aug 29, 2014 1:57 pm
by Celauran
Old version of PHP? You'll need to use array() instead of []

Code: Select all

$states = array('NY', 'CA', 'VT');

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

Posted: Fri Aug 29, 2014 1:59 pm
by donny
Parse error: syntax error, unexpected ';' in /home/newyorki/public_html/order/order/shipping_info_edit.php on line 86


line 86:

Code: Select all

 <option value="<?= $state; ?>" <?= ($state == $tablesqlrow['state'];) ? 'selected="selected"' : ''; ?>>