will35010 Posted July 28, 2010 Share Posted July 28, 2010 I'm trying to create a dynamic option menu with one alert selected based on the first query to the db. Any help would be greatly appreciated. //function to get alerts and create select menu with current alerts pre-selected function getALERTS1($id){ require('db.php'); $alert = mysqli_query($conn, "SELECT alert1 FROM visit_data WEHERE patientid = $id AND discharged IS NULL"); $row = mysqli_fetch_array($alert); $selects=null; $query = mysqli_query($conn, "SELECT alertid, name FROM alerts"); while($row1 = mysqli_fetch_array($query)) { $selects .= "<option value=\"" . $row1['alertid'] . "\"> if($row1['alertid']==$row['alert1']) { echo ' selected'; } ".$row1['name']."</option>"; } return $selects; } Link to comment https://forums.phpfreaks.com/topic/209118-dynamic-option-menu-with-selected-not-working/ Share on other sites More sharing options...
lemmin Posted July 28, 2010 Share Posted July 28, 2010 What is not working? "WHERE" is spelled wrong in your first query. Link to comment https://forums.phpfreaks.com/topic/209118-dynamic-option-menu-with-selected-not-working/#findComment-1092167 Share on other sites More sharing options...
will35010 Posted July 28, 2010 Author Share Posted July 28, 2010 What is not working? "WHERE" is spelled wrong in your first query. Thanks for catching that. I want it to output something like this: <select name="example"> <option value="1" selected>First</option> <option value="2">Business</option> <option value="3">Economy</option> <option value="4">Premium Economy</option> </select> I want the line selected based on the current alert from the first query. Link to comment https://forums.phpfreaks.com/topic/209118-dynamic-option-menu-with-selected-not-working/#findComment-1092172 Share on other sites More sharing options...
will35010 Posted July 28, 2010 Author Share Posted July 28, 2010 I want it to echo the selected when alert1 from the first query matches alertid on the second query. Link to comment https://forums.phpfreaks.com/topic/209118-dynamic-option-menu-with-selected-not-working/#findComment-1092174 Share on other sites More sharing options...
lemmin Posted July 28, 2010 Share Posted July 28, 2010 Your echo of " selected" needs to be inside the opening option tag. The way you have it written should throw a syntax error. Try this: while($row1 = mysqli_fetch_array($query)) { $selects .= "<option value=\"" . $row1['alertid']; if($row1['alertid']==$row['alert1']) { $selects .= ' selected'; } $selects .= "\">".$row1['name']."</option>"; } Also, make sure that "alert1" is the same field type as "alertid." Link to comment https://forums.phpfreaks.com/topic/209118-dynamic-option-menu-with-selected-not-working/#findComment-1092181 Share on other sites More sharing options...
will35010 Posted July 28, 2010 Author Share Posted July 28, 2010 Your echo of " selected" needs to be inside the opening option tag. The way you have it written should throw a syntax error. Try this: while($row1 = mysqli_fetch_array($query)) { $selects .= "<option value=\"" . $row1['alertid']; if($row1['alertid']==$row['alert1']) { $selects .= ' selected'; } $selects .= "\">".$row1['name']."</option>"; } Also, make sure that "alert1" is the same field type as "alertid." It works great except it's missing the " after the value so it's outputting this: <option value="19">Combative</option> <option value="18">Allergies</option> <option value="17 selected">Lab Ordered</option> How do I fix the apostrophe problem? Thanks!!! Link to comment https://forums.phpfreaks.com/topic/209118-dynamic-option-menu-with-selected-not-working/#findComment-1092188 Share on other sites More sharing options...
will35010 Posted July 28, 2010 Author Share Posted July 28, 2010 Never mind. I fixed it with this: while($row1 = mysqli_fetch_array($query)) { $selects .= "<option value=\"" . $row1['alertid']."\""; if($row1['alertid']==$row['alert1']) { $selects .= ' selected'; } $selects .= "\">".$row1['name']."</option>"; } Thanks! Link to comment https://forums.phpfreaks.com/topic/209118-dynamic-option-menu-with-selected-not-working/#findComment-1092195 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.