View Full Version : An Easier way to put a DropBox of a tables-field entrys

08-20-2007, 06:39 AM

@mysql_select_db($database) or die( "Unable to select database");
$query=" SELECT * FROM ca_spots WHERE zone='CA1'";
while($i<$num) {

echo "<option value=\"$spot_name\">$spot_name</option>";


Iv list that same code in alot of places and im just wondering if theres a way easyer way to make a Dropbox fill with my mySQL database content

08-20-2007, 12:06 PM
First you should put your DB code in an include file - then you only need to include the file rather than write out all that connection code.

Then theres a much easier way to write the code that you have there:

$sql = "SELECT * FROM ca_spots WHERE zone = 'CA1'";
$result = mysql_query($sql) or die(mysql_error())
echo '<option value = "'.$row['spot_name'].'">'.$row['spot_name'].'</option>';

And ofcourse you could write it into a function eg.

function dropDownOptions($query, $id, $value)
$result = mysql_query($query);
while($row = mysql_fetch_assoc($query))
echo '<option value = "'.$row[$id].'">'.$row[$value].'</option>';

then you would just call it like

dropDownOptions("SELECT * FROM ca_spots WHERE zone = 'CA1'", 'spot_name', 'spot_name');