Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selected option in drop down box 1

Status
Not open for further replies.

tsorik

Programmer
May 24, 2001
7
AU
Hi,

I am trying to set the selected option in a dynamically generated drop box using a variable passed from another page. Here's my code:

$query_venue = "select venue_name from venues";
$query_result = mysql_db_query($db,$query_venue);

for ($i=1; $i<=mysql_num_rows($query_result); $i++) {
$object_venue = mysql_fetch_object($query_result);

echo &quot;<option value=\&quot;$gig_venue\&quot; selected>$gig_venue</option>\n&quot;;
echo &quot;<option value=\&quot;&quot;.$object_venue->venue_name.&quot;\&quot;>&quot;.$object_venue->venue_name.&quot;</option>\n&quot;;

}
echo &quot;</select>\n&quot;;


The variable $gig_venue is on another table, but matches one entry in the $venue_name array.

Generating the list is okay, but once I try to start with an option selected I get $gig_venue selected, but then it also appears in the list once for every entry in the $venue_name array.

Am I asking the impossible?

Cheers,
Dan

 
I just answered my own question. Nothing like writing down your problem to get to thinking about it!

Here's the solution:

You need to pass the $gig_venue parameter as part of the url, i.e.

<a href=&quot;edit_gig.php?gig_id=<? echo $row->gig_id; ?>&gig_venue=<? echo $row->gig_venue; ?>&quot;>edit</a>

and the drop down code on the edit_gig page now looks like this:

<?
echo &quot;<select name=\&quot;gig_venue\&quot;>\n&quot;;
// open database connection
$link = mysql_connect($host, $user, $pass) or die (&quot;Unable to connect!&quot;);
// select database
mysql_select_db($db) or die (&quot;Unable to select database!&quot;);

$query_venue = &quot;select venue_name from venues&quot;;
$query_result = mysql_db_query($db,$query_venue);

for ($i=1; $i<=mysql_num_rows($query_result); $i++) {
$object_venue = mysql_fetch_object($query_result);


echo &quot;<option value=\&quot;&quot;.$object_venue->venue_name.&quot;\&quot;>&quot;.$object_venue->venue_name.&quot;</option>\n&quot;;
}
echo &quot;<option selected>$gig_venue</option>\n&quot;;
echo &quot;</select>\n&quot;;
?>

The option is still repeated, but only once.

Hope this helps some else.

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top