Forum Moderators: coopster

Message Too Old, No Replies

Populate a Select Tag from MySQL with options

         

recsx

12:24 am on May 30, 2004 (gmt 0)

10+ Year Member



Hi All,

Been working on an APP for sometime now and have been comming along nicely until i came to the part of my Select tag.

this is what i have now and it WORKS!
<?PHP
$package_result = mysql_query("SELECT * FROM packages");

echo '<select name="package" TABINDEX="18"><OPTION>';
echo "</OPTION>";
while ($row_item = mysql_fetch_array($package_result)){
$pname = $row_item["pname"];
echo "<OPTION value=\"$pname\">$pname</OPTION>";
}
echo '</SELECT>';
?>

The VALUE=$pname is the value of the displayed list
when you update the form it will place the VALUE in the clients database which is fine cause i need to know what option was used cause i have another script that lists the clients info and the selected package but what i need to do is based on the selection i need to also get another value from the same row in the same TABLE and enter it into the CLIENTS table in a field called radius_attributes, by using a HIDDEN INPUT or something.

Am i making sense here?

thedagda

5:25 am on May 30, 2004 (gmt 0)

10+ Year Member



This may not be the most efficient way, but why don't you simply do another database call, on the next page, before you insert the data into the client's table?

$sql = "select * from packages where column = 'selected option';"

-- execute query --

$insert = "insert into clients_table values('selected option','stuff from above query');

HTH,

Conor

recsx

12:34 pm on May 30, 2004 (gmt 0)

10+ Year Member



I may not have explained myself

I would like for this to be done within the same form page like $PHP_SELF

what i have is a form that is populated with a query
$sql = mysql_query("SELECT * FROM clients WHERE clientID='$clientID'", $mysql_ID);
$myrow = mysql_fetch_array($sql);

and all the fields are filled with $myrow = ["bla bla"]

along with the <SELECT> tag and the select tag has its own query to have it filled from another table called PACKAGES but it also has the $myrow = ["package"] in it so it can remember what was last used from the above query.

here is the sample script for the <SELECT> tag.
<?PHP

$package_result = mysql_query("SELECT * FROM packages",$mysql_ID);

echo '<select name="package" TABINDEX="18">
// Here is where i have the OPTION to remember what was last used
<OPTION>';
echo $myrow["package"];
echo "</OPTION>";
// Up to here, as you can see its an <OPTION> tag before the while so the first option is always the last a user has used.

// Then i do my array to fill the options in the drop menu
while ($row_item = mysql_fetch_array($package_result)){

$pname = $row_item["pname"];
$attributes = $row_item["radius_attributes_high"];
echo "<OPTION value=\"$pname\">$pname</OPTION>";
}
and here i close the select tag out of reach from the while command.
echo '</SELECT>';
?>

You can see that $pname is the VALUE and the DISPLAYED option for the SELECT tag it needs to be this way so i can enter the name of the package selected into the clients information database.

In the PACKAGE table where pname comes from it also has a field called radius_attributes_high, now based on the selection the user makes i need to take those attributes and enter it into the clients table allong with the package name but have to be entered in the clients table in their own fields.

Am i making sense in short i need more then just one VARIABLE from the select tag and not just one like my above query is laid out.

P.S. like i said i would realy like to have this down within $PHP_SELF i don't want to link to another file.

WHY?:
I've been working on this file for almost a week and it has massive amount of options in it THE LIKE displaying different data and so on based on what you pick and if i send this query to another page now it will screw all my work up plus make the proccess of filling and updating this form long.

Thanks in advanced Guy's and Gall's

coopster

4:09 pm on Jun 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm not sure if I understand entirely, but typically if you want to keep the last value selected from a select list, you need to check any POSTed variable during the list population. Something like...
print '<select name="package">'; 
while ($row_item = mysql_fetch_array($package_result)) {
$pname = $row_item["pname"];
echo "<OPTION value=\"$pname\";
if (isset($_POST['package']) and $pname == $_POST['package']) {
echo ' selected="selected"';
}
echo ">$pname</OPTION>";
}
print "</select>";

[edited by: eelixduppy at 10:46 pm (utc) on Aug. 14, 2007]
[edit reason] fixed minor syntax error [/edit]