Re: Populate Popup Menu from Database
| From: | Erik Price | Date: | Mon, 01 Jul 2002 21:05:39 +0000 |
| Subject: | Re: Populate Popup Menu from Database | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-104782@lists.php.net to get a copy of this message | ||
On Monday, July 1, 2002, at 04:22 PM, Mike Tuller wrote:
Thanks. Here is what I did for future reference.Good. What you chose to do is exactly what I do except for one thing... :
I generally grab the primary key column value as well, in the same query, and then I use that as the "value" attribute for the <option> tags:$sql = "select department_name from Departments";
while ($row = mysql_fetch_assoc($result)) { $dept_id = $row['department_id']; $dept_name = $row['department_name']; echo "<option value='$dept_id'>$department_name</option>\n"; } The reason I do this is because I end up using the ID far more than the "name" of a database record -- while I might echo the "name" to the user where needed (such as in the above dropdown listbox), the ID comes in handy as a reference in hyperlinks, form fields, etc -- it provides something that I've discovered is really missing in writing HTML-based applications: a unique handle on an object. This is very hard to replicate given the statelessness of HTTP, but with a database record's primary key, you always have this unique identifier by which to refer to the object. and a number is more pithy than a name. It'll avoid situations where someone enters the same "name" value twice, too. But it's not really a big deal unless you're doing a lot of work with a lot of data. Erik ---- Erik Price Web Developer Temp Media Lab, H.H. Brown pricee@hhbrown.comwhile($row = mysql_fetch_array($sql_result)) { $department = $row["department_name"]; echo "<option value = '$department'>$department</option>";}