简体   繁体   中英

Get selected value from sql server to dropdown in php

I want to get the value from sql server to dropdown in php as the selected option. First I have null values on the db and when I select values in dropdown it will update on db when submit button is clicked. Now, I want to set the data stored from db as the selected option in every dropdown. And I can choose to update it again if I clicked on submit button. I tried to put an if condition in the option tag but I get an error.

   <?php

        foreach ($records as $key => $data) {
             $dates[] = $data[0];
             $dates1[] = $data[3];
             $dates2[] = $data[2];
             $circuit[] = $data[1];

             $circuitNo[] = $FeederData-> getCircuitNumber($data[1]);

             $parsed = date_parse($key[2]);
             $seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];


             $tripdate[] = date("m/d/Y", strtotime($data[0]) - $seconds);
             $triptime[] = date("H:i:s", strtotime($data[3]) - strtotime($data[2]));

             $selectedCause[] = $data[4];
             $selectedWeather[] = $data[5];
             $selectedDevice[] = $data[6];
             $selectedEquipment[] = $data[7];

           }


     for ($i=0; $i < count($dates); $i++) {
           echo " <tr>
             <form method='POST' name='forms1' class='form-inline'>
                  <input type='hidden' name='selectedMonth' value='$date1'>
                  <input type='hidden' name='selectedYear' value='$year'>
                  <td><input type='hidden' name='tripdate[]' value='$tripdate[$i]'/> $tripdate[$i] </td>
                  <td><input type='text' name='description1[]' class='form-control'/></td>
                  <td><input type='hidden' name='circuit[]' value='$circuit[$i]'>
                      <input type='hidden' name='tripdate1[]' value='$circuitNo[$i]'/> $circuitNo[$i] </td>
                  <td><input type='hidden' name='triptime[]' value='$triptime[$i]'/>$triptime[$i]</td>
                  <td><input type='hidden' name='dates[]' value='$dates[$i]'/>$dates[$i]</td>
                   <td><input type='hidden' name='dates1[]' value='$dates1[$i]'/>$dates1[$i]</td>
                   <td><input type='hidden' name='dates2[]' value='$dates2[$i]'/>$dates2[$i]</td>
                   <td><input type='text' name='description2[]' class='form-control'></td>


          <td>           
              <div class='form-group'>
                <select class='form-control' name='InterruptionCause[]'>
                   <option value='001'" if ($selectedCause[$i] == '1') {
                                                    echo "selected";
                                                  }"> 001 </option>

                    <option value='002'>002 </option>
                    <option value='003'>003 </option>
                    <option value='004'>004 </option>
                 </select>
         </div>
      </td>                 
 }
   ?>

    <input type='submit' class='btn btn-success' name='interruptionreport' value='Submit'> 
 </form>   
 </tbody>
 </table>

Your attempt produces a syntax error. You just end your echo statement without ; .

echo "
<div class='form-group'>
    <select class='form-control' name='InterruptionCause[]'>
        <option value='001' " . $selectedCause[$i] == '1' ? "selected" : "" . "> 001 </option>
        <option value='002' " . $selectedCause[$i] == '2' ? "selected" : "" . ">002 </option>
        <option value='003' " . $selectedCause[$i] == '3' ? "selected" : "" . ">003 </option>
        <option value='004' " . $selectedCause[$i] == '4' ? "selected" : "" . ">004 </option>
    </select>
</div>";

This will check, for every option, if selectedCause[$i] equals the value of your option. If so, it prints "selected", otherwise it does not print it (empty string).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM