faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

4 of 7 people (57%) answered Yes
Recently 4 of 7 people (57%) answered Yes

Entry

How do I populate an array from a SELECT string?

Feb 24th, 2008 19:53
dman, Ilya Poropudas, Stephen van Egmond, Mark Howison, http://www.ttnr.org


Easy.
Say you have a table with a list of vegetables that you want in a 
pulldown.  There is, among other things, a vegetable_id and a the 
vegetable's name.
I'm assuming you're using Postgres.
print '<select name="vegetable">';
$result = pg_exec($conn, "SELECT vegetable_id, name FROM vegetables");
for ($i=0; $i<pg_numrows($result) {
  $row = pg_fetch_row($result, $i);
  print '<option value="';
  print $row[0];
  print '">';
  print $row[1];
  print '</option>';
}
print "</select>";
pg_free($result);