dimanche 26 juin 2016

how do i add columns to these database tables or add whole new database table then insert results in appropriate html table cells?


users table:

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

chore_list table:

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

HTML table:

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

Everything is dynamic, except the dashes. Also I have it not selecting admin. I would like to know what the best way to extend the tables I have and/or create new table so that I can dynamically insert where the dashes are the amount of times each person did a particular chore. Here is what I have so far...

<?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>";
?>

UPDATE

My new table called user_chore_relations looks like this:

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

The new tbody while loop is now like this:

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>";
}

Which makes HTML Table look like this:

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

What I am looking for is this:

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

Aucun commentaire:

Enregistrer un commentaire