如何在php循环中单击时折叠/展开表格

时间:2019-06-03 10:54:41

标签: javascript php

我正在尝试让特定的表折叠并在每个语句下展开。这是在php循环的上下文中。 php循环从我的管理员php获取数据,并以此填充语句和每个语句下的表。因此,我想要的是不显示表,并在单击语句时显示与之对应的表。

我尝试过这样创建脚本:

<script>
window.onload = function() {
    var a = false;

    var collapse = "p"+ "<?php echo $collapse ?>";
    var collapse2 = "btn"+"<?php $collapse ?>";




    document.getElementById(collapse2).onclick = function (){
        if(a== false){
            document.getElementById(collapse).style.height = "auto";
            a =true;
        }else{
            document.getElementById(collapse).style.height = "0px";
            a =false;
        }
        }
    }
</script>

因为我认为每个ID都应该是特定的。[编辑]现在,我将ID的值在每次重复循环时都进行专门更改。但这仍然行不通。

这是php循环


<?php

if ($result = mysqli_query($con, "SELECT * FROM stellingen WHERE REGIOID=1;")) {
    $amountOfStellingen = 0;
    while ($amountOfStellingen < 4) {
        mysqli_data_seek($result, $amountOfStellingen);
        $row = mysqli_fetch_assoc($result);
        $collapse = $amountOfStellingen;
        $stellingid = $row["Stelling_ID"];
        echo '<div class="stelling-wrapper" id="btn' . $collapse . '">
        <img src="images/button.svg" alt="Show more..." class="btn" id="bton">
        <p class="stelling">';
        echo "<p>{$row["Stelling"]}</p>";
        echo '<div id="p' . $collapse . '">
        <table id="tabel" border=1px class="data">
        <tr>
            <th>Title</th>
            <th>Source</th>
            <th>Weight</th>
            <th>Date</th>
        </tr>
        </div>
        </div>';
        echo '<script type="text/javascript" src="script/collapse.js">';
        echo '</script>';
        $sql1 = "SELECT * FROM stelling WHERE stelling_iD=$stellingid;";
        $records1 = mysqli_query($con, $sql1);
        $recordscheck = mysqli_num_rows($records1);

        if ($recordscheck > 0) {
            while ($stelling = mysqli_fetch_assoc($records1)) {
                echo "<tr>";
                echo "<td>" . $stelling['Title'] . "</td>";
                echo "<td>" . $stelling['Source'] . "</td>";
                echo "<td>" . $stelling['Wheight'] . "</td>";
                echo "<td>" . $stelling['Timer'] . "</td>";
                echo "</tr>";
            }
        }
        $amountOfStellingen += 1;
        echo '</table>';


    }
}

?>

我希望每个表在单击指定的btn / statement时会折叠/展开。

0 个答案:

没有答案
相关问题