Jump to content

[SOLVED] Default Value from mysql query for <SELECT>


Recommended Posts

I'm not sure whether this is a php or mysql problem, but I am thinking that it is PHP....

I am having trouble making a SELECT list display the value from a query as the default value, but also show all of the other values as options for updating a record....

 

I have "tbl_professional", which includes the field "specialty_id". I have "tbl_specialty" which includes "specialty_id" and "specialty".

 

My goal is for the select list to default to the record where tbl_specialty.specialty_id = tbl_professional.specialty_id, but still show the other results in case that field needs to be edited.

 

How do I format this on my form? Below is the code I am trying to work with.

 

<?
echo'<select name="specialty_id" STYLE="width: 300px">' ;
$res=mysql_query("select specialty_id, specialty from tbl_specialty WHERE specialty_id < 8 ORDER BY specialty");
if(mysql_num_rows($res)==0) echo "there is no data in table..";
else
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
echo"<option value={$row['specialty_id']} selected={$formVars["specialty_id"]}>{$row['specialty']}</option>";
}
echo'</select>';

?>

this

Here is the query I am using to populate the other fields on this form. Not sure if this has anything to do with it.

$query="SELECT professional_id, lname, fname, specialty, specialty_id, add1, add2, city, state, zip, phone, fax, email, license FROM tbl_professional NATURAL JOIN tbl_specialty WHERE professional_id like \"%$trimmed%\"";

if you need to no what ? : for,

 

it ternary operator short for

(? if)  (else 

 

<?php

$row['specialty']="what_ever"; //you can manually set it as well.

echo"<option value={$row['specialty_id']}",$row['specialty_id']==$formVars["specialty_id"]? " selected='selected'" : '',">{$row['specialty']}</option>";
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.