mellis95 Posted March 12, 2009 Share Posted March 12, 2009 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%\""; Link to comment https://forums.phpfreaks.com/topic/149162-solved-default-value-from-mysql-query-for/ Share on other sites More sharing options...
sasa Posted March 12, 2009 Share Posted March 12, 2009 try echo"<option value={$row['specialty_id']}",$row['specialty_id']==$formVars["specialty_id"]? " selected='selected'" : '',">{$row['specialty']}</option>"; Link to comment https://forums.phpfreaks.com/topic/149162-solved-default-value-from-mysql-query-for/#findComment-783227 Share on other sites More sharing options...
redarrow Posted March 12, 2009 Share Posted March 12, 2009 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/149162-solved-default-value-from-mysql-query-for/#findComment-783236 Share on other sites More sharing options...
mellis95 Posted March 12, 2009 Author Share Posted March 12, 2009 This works perfectyl. Thanks for the help. try echo"<option value={$row['specialty_id']}",$row['specialty_id']==$formVars["specialty_id"]? " selected='selected'" : '',">{$row['specialty']}</option>"; Link to comment https://forums.phpfreaks.com/topic/149162-solved-default-value-from-mysql-query-for/#findComment-783304 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.