dsw8292301 2019-08-15 07:11
浏览 103

如何使html表列<th> </ th>动态更改mysql中的选定列

I have multiple select options. however, i want when i select 1 option or two or whatever to change the columns <th></th> of html dynamically and get data from database

Currently I have created the multiple select option and html table

<select class="selectpicker" id="sensor" name="sensor[]" multiple>
                                <option 
     value="temperature">Temperature</option>
                                <option value="humidity">Humidity</option>
                                <option value="presure">Presure</option>
                                <option 
     value="level">Level</option>
                                </select>

     $query = "SELECT ".$selectedSensorOption." from p1";       
                    $result = $db_handle->runQuery($query);
    foreach ($result as $key => $value) {
    <table>
    <tr>
    <th>$result</th>
    </tr> 
    <tr>
    <td>$result</td>

I want to get dynamic columns and fields as well from MySQL db based on multiple select options

  • 写回答

3条回答 默认 最新

  • douchen2011 2019-08-15 07:24
    关注

    first you have to get the headers, you can use explode to get the header keys. you'll use two loops, the first one to build the header and the second one to build the body. inside the body loop, you have to use another loop to get the correct value for each column

    
    <select class="selectpicker" id="sensor" name="sensor[]" multiple>
        <option value="temperature">Temperature</option>
        <option value="humidity">Humidity</option>
        <option value="presure">Presure</option>
        <option value="level">Level</option>
    </select>
    <?php
    $query = "SELECT ".$selectedSensorOption." from p1";
    $results = $db_handle->runQuery($query);
    //get the headers if $selectedSensorOption is not an array
    $headers = explode(',', $selectedSensorOption)
    ?>
    <table>
        <thead>
            <tr>
                <!-- loop and fill the header -->
                <?php foreach ($headers as $header) : ?>
                    <th><?= ucfirst($header) ?></th>
                <?php endforeach; ?>
            </tr>
        </thead>
        <tbody>
            <!-- loop and fill the body-->
            <?php foreach ($results as $result) : ?>
                   <tr>
                      <!-- loop and fill each column -->
                       <?php foreach ($headers as $header) : ?>
                         <th><?= $result[$header] ?></th>
                       <?php endforeach; ?>
                   </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算