Forum Moderators: coopster

Message Too Old, No Replies

Drop down box values from table field?

Drop down box values from table field?

         

portrower8

4:52 am on Nov 14, 2004 (gmt 0)

10+ Year Member



I am new to this -- so I created one table for this.

I have a list of potential sponsors for our nonprofit.

Each sponsor has a volunteer solicitor "assigned" to them.

I want Volunteer Solicitors to select their name from a drop down box - and have the search show all records associated with their name.

Is there a way to have a drop down box of a form use for option values UNIQUE names in the "VOLUNTEER_ASSIGNED" field of chrsponsors table?

I have read places that I can tell it to do a distinct query. But I got no idea how to do this. I have spent hours looking around the net. And even more hours trying different things that look like they might work...no luck.

Can someone help?

Salsa

7:14 am on Nov 14, 2004 (gmt 0)

10+ Year Member



If I'm understanding you correctly, first connect to MySQL, select a db, then:

// DISTINCT is the keyword you need to SELECT unique volunteers: 
$sql = "SELECT DISTICT VOLUNTEER_ASSIGNED FROM chrsponsors ORDER BY VOLUNTEER_ASSIGNED";
// query the database ($result does not contain the results, only a pointer to them):
$result = mysql_query($sql);
// if there is an error in the query, find out what it is:
if (!$result) die ("query error: ".mysql_error());
// start your form's select block before looping through the volunteer options:
$volunteer_options = "<select name=\"VOLUNTEER_ASSIGNED\">\n";
// loop throught the results of your query:
while ($query_data = mysql_fetch_array($result) {
// concatenate to your form's select block an option for each volunteer:
$volunteer_options .= " <option>".$query_data['VOLUNTEER_ASSIGNED']."\n";
}
// finish off your form's select block:
$volunteer_options .= "</select>\n";

In your form:

echo $volunteer_options;

I hope this helps.

portrower8

2:25 pm on Nov 14, 2004 (gmt 0)

10+ Year Member



hmm...

I got this this response:

query error: Unknown column 'DISTICT' in 'field list'

but a posting in another forum seemed to generate a working response it looks the same -- so why would the one here give question about DISTINT, but the other not? strange...:

<?php

$dbh=mysql_connect ("localhost", "sponsors_chr", "*****") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("radiohot_chr");
$query="SELECT DISTINCT VOLUNTEER_ASSIGNED from chrsponsors";
$result=mysql_query($query);
echo "<select name=\"volunteer_assigned\">";
while($row=mysql_fetch_array($result)) {
echo "<option value=\"$row[VOLUNTEER_ASSIGNED]\">$row[VOLUNTEER_ASSIGNED]";
}
echo "</select>";
?>

what do you think?

Salsa

3:51 pm on Nov 14, 2004 (gmt 0)

10+ Year Member



DISTINCT is misspelled in the query is all.

Sorry, but it's rare for me to get a code block typo-free the first time around, without testing...at three in the morning;)

That's why returning error messages is important. Look at them closely, and they'll give you your clues.

portrower8

7:07 pm on Nov 14, 2004 (gmt 0)

10+ Year Member



hehe.. ah I see.

Well with that - it works perfectly. But for some reason I cannot get the tables to show up correctly. I think I might have something wrong with where I put the echo. I am trying to put this in a php nuke block.

Anyway, below is the full code below. Unfortunately is shows up funny...

I FINALLY have the drop down box right -- but can't get it to show up right in the block -- it's killing me!

Here is what I have:

<?
if (eregi("block-SearchSponsors.php", $_SERVER['PHP_SELF'])) {
Header("Location: index.php");
die();
}
$dbh=mysql_connect ("localhost", "userid", "****") or die ('I cannot connect to the database because: ' . mysql_error());

///////// THIS IS WHAT I FOUND THAT WILL GIVE ME THE DROP DOWN BOX OF UNIQUE VOLUNTEERS IN THE TABLE
//////// I JUST DON'T KNOW WHERE IN THE WORLD TO PUT IT BELOW -- AND STILL HAVE BLOCK LOOK RIGHT
//////// EVERYTHING I HAVE TRIED SCREWS UP THE TABLES FOR THE WHOLE WEBSITE
// DISTINCT is the keyword you need to SELECT unique volunteers:
$sql = "SELECT DISTINCT VOLUNTEER_ASSIGNED FROM chrsponsors ORDER BY VOLUNTEER_ASSIGNED";
// query the database ($result does not contain the results, only a pointer to them):
$result = mysql_query($sql);
// if there is an error in the query, find out what it is:
if (!$result) die ("query error: ".mysql_error());
// start your form's select block before looping through the volunteer options:
$volunteer_options = "<select name=\"VOLUNTEER_ASSIGNED\">\n";
// loop throught the results of your query:
while ($query_data = mysql_fetch_array($result)) {
// concatenate to your form's select block an option for each volunteer:
$volunteer_options .= " <option>".$query_data['VOLUNTEER_ASSIGNED']."\n"; }
// finish off your form's select block:
$volunteer_options .= "</select>\n";
//In your form:
//echo $volunteer_options;
$content = "<table width=\"100%\" border=\"0\" align=\"center\"> <tr>

<snipped irrelevant code>

<td width=\"117\" align=right><font color=red><b>Lookup : </font></b> </td><td width=\"136\">
"<? echo .$volunteer_options."?>;
</td>
</tr>
<tr>
<snipped irrelevant code>
</table>";
?>

[edited by: coopster at 11:08 am (utc) on Nov. 15, 2004]
[edit reason] removed urls per TOS [webmasterworld.com] [/edit]

Salsa

7:38 pm on Nov 14, 2004 (gmt 0)

10+ Year Member



The problem is that, down where you say:

<td width=\"117\" align=right><font color=red><b>Lookup : </font></b> </td><td width=\"136\">  
"<? echo .$volunteer_options."?>;
</td>

...you have been putting the entire form into the variable, $content, so your php tag, <?, is already open. You don't want to open them again, and you don't want to try to echo at this point because later you are going to echo (or print) the entire $content variable.

Try changing the above code simply to:

<td width=\"117\" align=right><font color=red><b>Lookup : </font></b> </td><td width=\"136\"> 
$volunteer_options
</td>

portrower8

9:29 pm on Nov 14, 2004 (gmt 0)

10+ Year Member



Ah HA!

It works! Perfectly!

THANK YOU! THANK YOU! THANK YOU!

Salsa

10:58 pm on Nov 14, 2004 (gmt 0)

10+ Year Member



I'm glad I could help. And welcome to WebMasterWorld!