php - 我如何向这些数据库表添加列或添加全新的数据库表,然后将结果插入适当的 html 表格单元格?

标签 php mysql relational-database

用户表:

id | name
----------
1  | admin
2  | brian
3  | frank
4  | wendy

chore_list 表:

id | chore
----------
1  | cleaned coffee table
2  | vacuumed rug
3  | did dishes

HTML 表格:

------------------------------------------------
| chores               | brian | frank | wendy |
------------------------------------------------
| Cleaned coffee table | -     | -     | -     |
------------------------------------------------
| Vacuumed rug         | -     | -     | -     |
------------------------------------------------
| Did dishes           | -     | -     | -     |
------------------------------------------------

除了破折号之外,一切都是动态的。我也没有选择管理员。我想知道扩展我拥有的表和/或创建新表的最佳方法是什么,以便我可以动态插入破折号是每个人完成特定家务的次数的位置。这是我到目前为止所拥有的......

<?php
$result=$dbcon->query("SELECT name FROM users WHERE id != 1 ORDER BY name ASC");
$result_chores=$dbcon->query("SELECT chore FROM chore_list");
$x = $result->num_rows;
//echo $x; //test
echo "
<table id='choretable'>
    <thead>
        <tr>";
            echo "<th>chores</th>";
            while($row=$result->fetch_array()){
                echo "<th>".$row['name']."</th>";
            }
        echo "
        </tr>
    </thead>
    <tbody>";
        while($row_chores=$result_chores->fetch_array()){
            echo "
            <tr>";
                echo "<td>".$row_chores['chore']."</td>";
                $y = 1;
                while($y <= $x) {
                    echo "<td>-</td>";
                    $y++;
                }
            echo "
            </tr>";
        }
    echo "
    </tbody>
</table>";
?>

更新

我的名为 user_chore_relations 的新表如下所示:

id | userid | choreid | count
-----------------------------
1  | 2      | 1       | 1
2  | 2      | 2       | 2
3  | 3      | 3       | 1
4  | 4      | 1       | 1

新的 tbody while 循环现在是这样的:

while($row_chores=$result_chores->fetch_array()){
    echo "
    <tr>";
        echo "<td>".$row_chores['chore']." (".$row_chores['id'].")</td>";
        $y = 1;
        $row_chores_id = $row_chores['id'];
        while($y <= $x) {
            echo "<td>";
            $result_count=$dbcon->query("SELECT * FROM user_chore_relations WHERE choreid = ".$row_chores_id."");
            while($row_count=$result_count->fetch_array()){
                echo $row_count['userid']; //not needed but wanted to see what comes out, and it seems wrong. shouldn't it be same id all the way down the whole column? How is that done?
                echo $row_count['count'];
            }
            echo "
            </td>";
            $y++;
        }
    echo "
    </tr>";
}

这使得 HTML 表格看起来像这样:

------------------------------------------------
| chores               | brian | frank | wendy |
------------------------------------------------
| Cleaned coffee table | 2141  | 2141  | 2141  |
------------------------------------------------
| Vacuumed rug         | 22    | 22    | 22    |
------------------------------------------------
| Did dishes           | 31    | 31    | 31    |
------------------------------------------------

我正在寻找的是:

------------------------------------------------
| chores               | brian | frank | wendy |
------------------------------------------------
| Cleaned coffee table | 1     |       | 1     |
------------------------------------------------
| Vacuumed rug         | 2     |       |       |
------------------------------------------------
| Did dishes           |       | 1     |       |
------------------------------------------------

最佳答案

这是我想出的似乎有效的方法:

$result=$dbcon->query("SELECT id, name FROM users WHERE id != 1 ORDER BY name ASC");
$result_chores=$dbcon->query("SELECT id, chore FROM chore_list");
$x = $result->num_rows;
echo "
<table id='choretable'>
    <thead>
        <tr>";
            echo "<th>chores</th>";
            while($row=$result->fetch_array()){
                echo "<th>".$row['name']."</th>";
                $ids[] = $row['id'];
            }
        echo "
        </tr>
    </thead>
    <tbody>";
        while($row_chores=$result_chores->fetch_array()){
            echo "
            <tr>";
                echo "<td>".$row_chores['chore']."</td>";
                $y = 0;
                $row_chores_id = $row_chores['id'];
                while($y < $x) {
                    echo "<td>";
                    $result_count=$dbcon->query("SELECT * FROM user_chore_relations WHERE userid = ".$ids[$y]." AND choreid = ".$row_chores_id."");
                    while($row_count=$result_count->fetch_array()){
                        echo $row_count['count'];
                    }
                    echo "
                    </td>";
                    $y++;
                }
            echo "
            </tr>";
        }
    echo "
    </tbody>
</table>";

关于php - 我如何向这些数据库表添加列或添加全新的数据库表,然后将结果插入适当的 html 表格单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38035188/

相关文章:

php - 有没有像chrome的时间线这样的php基准测试工具

php - Android - Php Echo 字符串不工作

php - 如何在 Zend_Form 中添加 '[]' 到表单元素名称?

html - 如何通过单击按钮更新mysql数据?

mysql - 如果其中一个连接表具有另一个连接表的外键,如何执行两个连接表的连接?

relational-database - 什么是部分依赖

javascript - 更改 WooCommerce 中某些产品的 "Sold out"

MySql求和查询

mysql - 如何在 Mosaic 中创建与任何外部数据库的连接?

mysql - 将多个数据表存储在单个数据库表中