简体   繁体   中英

Create table column headers from selected values of a drop down list

I am using this example of a multiple selection dropdown list

index.php

<!DOCTYPE html>
<html>
 <head>
  <title>Webslesson Tutorial | How to Use Bootstrap Select Plugin with PHP JQuery</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>

 </head>
 <body>
  <br /><br />
  <div class="container">
   <br />
   <h2 align="center">How to Use Bootstrap Select Plugin with PHP JQuery</h2>
   <br />
   <div class="col-md-4" style="margin-left:200px;">
    <form method="post" id="multiple_select_form">
     <select name="framework" id="framework" class="form-control selectpicker" data-live-search="true" multiple>
      <option value="Laravel">Laravel</option>
      <option value="Symfony">Symfony</option>
      <option value="Codeigniter">Codeigniter</option>
      <option value="CakePHP">CakePHP</option>
      <option value="Zend">Zend</option>
      <option value="Yii">Yii</option>
      <option value="Slim">Slim</option>
     </select>
     <br /><br />
     <input type="hidden" name="hidden_framework" id="hidden_framework" />
     <input type="submit" name="submit" class="btn btn-info" value="Submit" />
    </form>
    <br />

   </div>
  </div>
 </body>
</html>

<script>
$(document).ready(function(){
 $('.selectpicker').selectpicker();

 $('#framework').change(function(){
  $('#hidden_framework').val($('#framework').val());
 });

 $('#multiple_select_form').on('submit', function(event){
  event.preventDefault();
  if($('#framework').val() != '')
  {
   var form_data = $(this).serialize();
   $.ajax({
    url:"insert.php",
    method:"POST",
    data:form_data,
    success:function(data)
    {
     //console.log(data);
     $('#hidden_framework').val('');
     $('.selectpicker').selectpicker('val', '');
     alert(data);
    }
   })
  }
  else
  {
   alert("Please select framework");
   return false;
  }
 });
});
</script>

It works fine but I am trying to adjust the code mentioned on the page according my needs. I want to create a dynamic table with columns name what i selected from the Dropdown list

so instead of creating the table manually and giving names of the columns as my code here

<?php   

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


    $ser="10.90.294.23";
    $db="Framework"; 
    $user="test"; 
    $pass="test0";
    $query = 'SELECT Laravel, Symfony, Codeigniter, CakePHP FROM Framework.list';

    $dbDB = new PDO("odbc:Driver=ODBC Driver 13 for SQL Server;Server=10.90.294.23,1456;Database=Framework;Port=1456", $user, $pass);

     ?>  
     <!DOCTYPE html>  
     <html>  
          <head>  
               <title>HTML table using Jquery with PHP</title>  
               <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
               <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
               <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
          </head>  
          <body>  
               <br />  
               <div class="container" style="width:700px;">  
                    <h3 class="text-center">HTML table using Jquery with PHP</h3><br />  
                    <div class="table-responsive" id="employee_table">  
                         <table class="table table-bordered">  
                              <tr>  
                                   <th width="10%">Laravel</th>  
                                   <th width="30%">Symfony</th>  
                                   <th width="10%">Codeigniter</th>  
                                   <th width="50%">CakePHP</th>  
                              </tr>  
                            <?php   
    foreach ($dbDB->query($query) as $row) {
                              ?>  
                              <tr>  
                                   <td><?php echo $row['Laravel']; ?></td>  
                                   <td><?php echo $row['Symfony']; ?></td>  
                                   <td><?php echo $row['Codeigniter']; ?></td>  
                                   <td><?php echo $row['CakePHP']; ?></td>  
                              </tr>  
                              <?php                           
                              }  
                              ?>  
                         </table>  
                    </div>   
               </div>  
               <br />  
          </body>  
     </html>  

I want the columns headers will be created automatically from the selected values. I was thinking about storing the selected value in variable for example : Laravel,CakePHP,Yii,Slim then I create the header based on each text between the comma ","

Can anyone help with this please ? I have tried many ways but still can't make it work. Thank you.

If this is the same as the example then I would have thought your table would have one row and be comma delimited. This really isn't the best way but for your example you need to do.

     $row = $dbDB->query($query);
         $rows = explode(",",$row);
        ?>
          <tr>  
    <?php
         foreach ($rows as $r ) {
                          ?>  

                               <td><?php echo $r; ?></td>  

                          <?php                           
                          }  
?>
        </tr>  

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