I have made a dropdown box that is filled by a query that looks for company names from company name database, these names also have and ID number that I don’t want displayed but are looked up in the query. I need the ID number to link to sites of the company so somehow when I hit the submit button on the site page it finds the ID number by looking at the position value and relating that to the position on the query array I just don’t know what to do. If it helps this is how I fill the dropbox:
mysql_select_db("DB", $con);
$query = "SELECT Company_Name, ID FROM company_table";
$result = mysql_query($query) or die(mysql_error());
$options ='';
$num = 0;
while ($row=mysql_fetch_array($result)) {
$num = $num+1;
$options.= "<OPTION VALUE=\"$num\">".$row["Company_Name"];
}
<SELECT NAME=thing>
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT>
Any Ideas?
There are two immediate ways this can be done. One is to keep it the way you have it using an index,
$num; the other way is to use the actual company id field,ID. If you stick with the index of$num, when the user submits the form (i.e. – selects a company), you will have to re-query the database and re-loop through the results to find the specific company (or you could use aLIMIT/OFFSET; notes at end of answer).I would recommend using the actual company id in your form:
This will generate a list of options, like you currently have, except the value of each will be the specific company id. When the user submits the form, let’s assume the form is using
POST, you can get the ID with:.. and then process as you desire.
I use
$_POST['thing']in the above example as that is what your code-example has theselectfield named. Also, I make the assumption thatIDis an integer.If the actual
IDneeds to remain hidden, as specified, the index of$numcan be used with MySQL’sLIMIT/OFFSETas follows: