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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Option List

Status
Not open for further replies.

carlg

Programmer
Joined
Jun 23, 2004
Messages
88
Location
US
I'm kinda new to the PHP stuff, so this may be a really basic question.

I want to make a php page that has an option list. The thing where you click the arrow and you get to choose something.

What I want to do is get the values for the option list from a mysql database table. So if there are 10 rows in the table, my option list will have 10 choices. If there are 2 rows in table, there will be 2 choices.

Anybody know how I could do this with php/html

Thanks for the help



 
Hi,
Here's your php code:
Code:
$connection = connect_to_db();
$sql = "SELECT * FROM TABLE ORDER BY FIELD";
$results = mysql_query($sql,$connection)
  or die("Couldn't execute query.");
$option_list = "<OPTION value=\"$id\" selected>Select an option</OPTION>";
while ($row = mysql_fetch_array($results)) {
  $id = $row['ID'];
  $option = stripslashes($row[FIELD]);
  $option_list .= "<OPTION value=\"$id\">$option</OPTION>"; 
  }

in your html:
<select name="choose"><? echo "$option_list"; ?></select>

[cheers]
Cheers!
Laura
 
There are a few problems with the above code:
1) You won't have a value in $id before the while statement
2) $row[FIELD] should be $row['field']

Here's my stab at the code:
Code:
$connection = connect_to_db();
$sql = "SELECT * FROM TABLE ORDER BY FIELD";
$results = mysql_query($sql,$connection)
  or die("Couldn't execute query.");
$option_list = array();
while ($row = mysql_fetch_array($results)) {
  $id = $row['ID'];
  $option = stripslashes($row['FIELD']);
  $option_list[] = '<OPTION value="' . $id . '">' . $option . '</OPTION>'; 
  }

Then in your html:
Code:
<select name="choose"><? echo implode("\n",$option_list); ?></select>

Ken
 
Good point Ken,
In my haste to answer the question, I had a little typo in the code = $row[FIELD};

However, for the no value in $id, that was intentional. What's the point in assigning a value to "Select an option"?

[cheers]
Cheers!
Laura
 
I just did a snippet to do exactly what you are trying to do, so I'll post it here.

Code:
<?php
  /*
   * grab series info and put it in a drop down box
   */
  $recordset = mysql_query("SELECT series_id, series_name FROM Series ORDER BY series_name") or die("<FONT COLOR=\"red\">Unable to run query.</FONT>");

  echo "<SELECT NAME=\"series\">";

  echo "<OPTION VALUE=\"0\">.: Select from list :.</OPTION>";

  while ($row = mysql_fetch_row($recordset))
    echo "<OPTION VALUE=\"$row[0]\">$row[1]</OPTION>";

  echo "</SELECT>\r\n";
?>

When I did this, I created the first record (with an ID of 0) before adding the rest of the values into the combobox. When the form is loaded, the user sees '.: Select from list :.' in all of the combobox fields, and when I validate the form, if the value is 0 (zero) then the user forgot to choose one, and I can take an appropriate action. The field series_id is an auto-increment field, and is the PK.
 
Would you not actually have a value in $id, as you assigned in from $row on entry to your loop ??
 
Hi
You would once you enter the loop. Before there is any value assigned to $id , the value is nonexistent - NULL;

[cheers]
Cheers!
Laura
 
Thanks for the info everyone, this will really help!!!
 
I must have missed some point here, does it matter if it contains null, it's not used until it has a value assigned.
 
Hi,
No you didn't miss a point. Ken had stated that there was a problem with the code I supplied, but I think he missed the purpose of that particular line of code.

Essentially, I had an option with the value $id (before any value was assigned) to the option "Select an option". Ken stated that there was a problem with the code because $id didn't have any value assigned. My point was there shouldn't be any value assigned, because "Select an option" isn't a valid choice anyways.

That portion of my code is intentional, because I find it more user-friendly to have the "Select an option" message rather than just presenting values, so it's very clear to the end user what to do (perhaps it's an end user's first time filling out a form, and seeing an option list).

[cheers]
Cheers!
Laura
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top