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; } Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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." Quote Link to comment 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!!! Quote Link to comment 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.