Populating An HTML Select List From A PHP Array
I am trying to populate an HTML select list using an array of customer names. I am using the following code, but it does not seem to work. I have a function that query an SQL Serve
Solution 1:
Have you tried doing a print_r()
or var_dump()
of $custNames
. There's nothing wrong with your foreach loop, so most likely the getCustomers()
function is returning a non-array, or an empty array.
And of course, be very careful inserting text that way. A single double-quote in any of the customer names will hose your form:
<option value="John "The Unknown" Doe">John "The Unknown" Doe</option>
At least pass the value portion through htmlspecialchars()
to make it a bit safer.
Solution 2:
A foreach
loop is perfectly fine, provided that getCustomers()
is returning an array (or more specifically, an object that implements the Traversable
interface). You may wish to do a var_dump( $custNames );
to check that what you have is in fact an array, otherwise you'll probably want to change the getCustomers()
function to return an array.
Post a Comment for "Populating An HTML Select List From A PHP Array"