Re: Populate Popup Menu from Database

From: Date: Mon, 01 Jul 2002 23:21:04 +0000
Subject: Re: Populate Popup Menu from Database
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-104808@lists.php.net to get a copy of this message
Thanks for the tip. > From: Erik Price <pricee@hhbrown.com> > Date: Mon, 1 Jul 2002 17:05:39 -0400 > To: Mike Tuller <php@ce.anoka.k12.mn.us> > Cc: php mailing list <php-general@lists.php.net> > Subject: Re: [PHP] Populate Popup Menu from Database > > > 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 > pricee@hhbrown.com > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#104808) next »