...

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



glish_dreams
08-20-2007, 06:39 AM
<?php

mysql_connect(localhost,$user_db,$pass_db);
@mysql_select_db($database) or die( "Unable to select database");
$query=" SELECT * FROM ca_spots WHERE zone='CA1'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while($i<$num) {
$zone=mysql_result($result,$i,"zone");
$spot_name=mysql_result($result,$i,"spot_name");

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

$i++;
}
?>

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

NancyJ
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())
while($row=mysql_fetch_assoc($result))
{
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');



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum