2

I have created a list of employees with few fields which includes dropdown too. the problem is whenever i select edit option and redirected to the edit page the value in the dropdown is getting set to the first value in the database from which i am querying it. i want to set the value of the dropdown same as the predefined one before editing option was selected i.e if i select a territory to edit and its value was T5 before editing i want the same to be selected not T1 instead the code i am using in the edit page is

 <?php                                          
  $sql = "SELECT DISTINCT `territory` FROM se_ae ";
 ?>
                    <select name="territory">
                    <?php foreach ($dbo->query($sql) as $row) { ?>
                    <option value="<?php echo $row['territory']; ?>">
                     <?php echo $row['territory']; ?></option> 
    <?php }
     ?>

Can anyone help me on this.

Akshay Vasu
  • 445
  • 1
  • 12
  • 33
  • And where is the value that needs to be shown while editing means where you were saving or getting the value within variable – Narendrasingh Sisodia Jul 03 '15 at 11:42
  • @Uchiha i am querying the values from the database i have stored the values in a column called territory and querying it to display in the dropdown – Akshay Vasu Jul 03 '15 at 11:46
  • You will have to write selected attribute with previously selected employee and you will have to apply IF statement for that – Paras Pitroda Jul 03 '15 at 12:52

2 Answers2

0

You probably have a variable that stores the fields of the employee, right? If you don't, you should, after all you are editing its data, so you should display the current data so you can change what you want.

Let's call this variable $employee.

<?php $sql = "SELECT DISTINCT `territory` FROM se_ae "; ?>
<select name="territory">
  <?php foreach ($dbo->query($sql) as $row) { ?>
    <?php $selected = ($row['territory'] == $employee['territory']) ? 'selected' : '' ?>
    <option value="<?php echo $row['territory']; ?>" <?php echo $selected; ?>>
      <?php echo $row['territory']; ?>
    </option>
  <?php } ?>
</select>

This way, for each territory, you check if it's equal to the employee territory, and if it is, it will add the selected parameter to the tag option.

Lucas Ferreira
  • 858
  • 8
  • 18
0

As you have saved your value within variable. You can try as

<?php foreach ($dbo->query($sql) as $row) { ?>
    <option value="<?php echo $row['territory']; ?>" <?php echo ($territory == $row['territory']) ? 'selected' : '';?>>
        <?php echo $row['territory']; ?></option> 
<?php } ?>

Added <?php echo ($territory == $row['territory']) ? 'selected' : '';?>

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54