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

 

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.