skip to Main Content

hi I’m beginner in php coding i have a problem to display the previously selected value in drop down list when im editing its blank regardless of its selected value as I have two tables table admin_profile which i keep record of users and table of current_jobs I’m fetching the username of the users to be displayed in a drop down list to be stored in table column checker

here is my code

            </select>   
        </div>
    </div> 
            
    <div class="col">
        <div class="form-group">
<?php 
    $query ="SELECT user_name FROM admin_profile 
            where  user_role='Checker' or user_role='Designer' ";
    $result = $conn->query($query);
    if($result->num_rows> 0){
        $options = mysqli_fetch_all($result, MYSQLI_ASSOC);
    }
?>
            <label>Checker</label>
            <select class="form-control" name="checker" id="checker" >
                <option value=""  ></option>
<?php 
    foreach ($options as $option) {
?>
                <option value=""><?php echo $option ['user_name']; ?> </option>
<?php 
    }
?>
            </select>


I tried this code but its not working

<option  <?php if (!empty($checker) && $checker == $option['user_name']) echo "selected"; ?>>   </option>

2

Answers


  1. Chosen as BEST ANSWER

    i was able to solve it thanks

    <option value="<?php echo $option['user_name']; ?>"  <?php if (!empty($checker) && $checker == $option['user_name']) echo "selected"; ?>   ><?php echo $option['user_name']; ?> </option>
    

  2. It appears blank because in your code, the value attribute is not assigned anything (value=""). What you need to do is assign the correct value to each option. Essentially, the code you attempted to test should be placed within the value attribute:

    <option value="<?php echo $option['user_name']; ?>"><?php echo $option['user_name']; ?></option>
    

    Let me know if it works

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search