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... :

>     $sql = "select department_name from Departments";

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:

>     while($row = mysql_fetch_array($sql_result))
>     {
>         $department = $row["department_name"];
>         echo "<option value = '$department'>$department</option>";
>
>     }

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
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to