2016-06-26 27 views
0

用戶表:如何向這些數據庫表添加列或添加全新的數據庫表,然後在適當的html表格單元格中插入結果?

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

UPDATE

我的新表稱爲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  |  | 
------------------------------------------------ 
+1

讓第三個表,有用戶ID,choreid,和choredate作爲列。然後,您可以對每個用戶,雜項類型和日期(如果需要)進行組計數。 – chris85

+0

羣由哪個字段? choreid? – leoarce

+0

查看更新的問題。 – leoarce

回答

0

這裏是我想出了似乎工作:

$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>"; 
相關問題