PHP动态表头和基于头的动态表行 - 尝试了一切

时间:2017-04-15 13:28:22

标签: php jquery html mysql html-table

我正在尝试使用动态标头和基于标头的动态行构建一个可编辑的表,但我在这个过程中迷失了方向。 我得到了动态标题:

<div class="jumbotron">
  <div class="container">
    <table class='table'>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Emails</th>
                <?php
                    include("../connection/connection.php");


                    $get_title = "SELECT DISTINCT(title) FROM attendance";
                    $query1 = mysqli_query($con,$get_title);        

                    if ($query1) {
                        # code...
                        while ($row=mysqli_fetch_array($query1)) {
                            # code...
                            $title[] = $row['title'];

                        }
                        $count = count($title);
                        for ($i=0; $i <$count ; $i++) { 
                            # code...
                            echo "<th>".$title[$i]."</th>";

                        }

                    }

                ?>

            </tr>
        </thead>
    <tbody>
        <?php 

            $get_emails = "SELECT * FROM subjects";
            $query2 = mysqli_query($con,$get_emails);

            if ($query2) {
                # code...
                while ($row=mysqli_fetch_array($query2)) {
                    # code...
                    $emails = $row['email'];
                    $id = $row['id'];

                    $get_name = "SELECT * FROM users";
                    $query3 = mysqli_query($con,$get_name);

                    if ($query3) {
                        # code...
                        while ($row=mysqli_fetch_array($query3)) {
                            # code...
                            $fullname = $row['user_fullname'];

                            echo "<tr>";
                            echo "<td contenteditable>".$id."</td>";
                            echo "<td contenteditable>".$fullname."</td>";
                            echo "<td contenteditable>".$emails."</td>";
                            echo "</tr>";
                        }
                    } else {
                            echo "<tr>";
                            echo "<td contenteditable>".$id."</td>";
                            echo "<td contenteditable></td>";
                            echo "<td contenteditable>".$emails."</td>";
                            echo "</tr>";
                    }

                }

            } else {
                echo "<tr>";
                echo "<td contenteditable></td>";
                echo "<td contenteditable></td>";
                echo "<td contenteditable></td>";
                echo "</tr>";
            }
        ?>
    </tbody>
    </table>
  </div>
</div>

现在我需要回复表td,这个表是动态的,基于电子邮件之后的标题 - 在tr内部的td。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

你遇到的主要问题是数组元素不会被称为title而是DISTINCT(title),因为你没有把它作为别的别名,下面的代码显示它是别名回到标题,并消除看似不必要的循环。

$get_title = "SELECT DISTINCT(title) title FROM attendance";
$query1 = mysqli_query($con,$get_title);        

if ($query1) {
    # code...
    while ($row=mysqli_fetch_array($query1)) {
        # code...
        echo "<th>".$title[$i]$row['title']."</th>";
    }
}