Jump to content

dynamic option menu with selected not working


will35010

Recommended Posts

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;
}

 

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.

 

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."

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!!!

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.