2表(数据库)中的1个表(html)视图

时间:2015-01-08 02:11:23

标签: php html mysql

伙计..我真的不知道如何解决这个问题,并希望有人可以帮助我。我有2个表数据库,1个想要在1个HTML表中查看..我使用PHP,mysql ..这里的示例数据..


tbl_taskmain

| task_id | task_name  | task_status | task_date    | taks_period
------------------------------------------------------------------
| 1       | test 1     | 1           | 2014-10-03   | 10
| 2       | test 2     | 1           | 2014-11-15   | 15
| 3       | test 3     | 1           | 2014-12-21   | 10


tbl_tasksub

| tasksub_id | task_id | tasksub_name       | tasksub_status | tasksub_date   
-------------------------------------------------------------------------------
| 1          | 1       | tasksub 1 main 1   | 1              | 2014-10-03
| 2          | 1       | tasksub 2 main 1   | 1              | 2014-10-07
| 3          | 1       | tasksub 3 main 1   | 1              | 2014-10-09
| 4          | 2       | tasksub 1 main 2   | 1              | 2014-11-15
| 5          | 2       | tasksub 2 main 2   | 1              | 2014-11-28
| 6          | 3       | tasksub 1 main 3   | 1              | 2014-12-21
| 7          | 3       | tasksub 2 main 3   | 1              | 2014-12-22
| 8          | 3       | tasksub 3 main 3   | 1              | 2014-12-25
| 9          | 3       | tasksub 4 main 3   | 1              | 2014-12-31


我希望html表视图在上面的两个表数据库中都是这样的。

| # | # | Task Name         | Task Period | Task Date  
-------------------------------------------------------------------------------
| 1 |   | test 1            | 10          | 2014-10-03
|   | 1 | tasksub 1 main 1  |             | 2014-10-03
|   | 2 | tasksub 2 main 1  |             | 2014-10-07
|   | 3 | tasksub 3 main 1  |             | 2014-10-09
| 2 |   | test 2            | 15          | 2014-11-15
|   | 4 | tasksub 1 main 2  |             | 2014-11-15
|   | 5 | tasksub 2 main 2  |             | 2014-11-28
| 3 |   | test 3            | 10          | 2014-12-21
|   | 6 | tasksub 1 main 3  |             | 2014-12-21
|   | 7 | tasksub 2 main 3  |             | 2014-12-22
|   | 8 | tasksub 3 main 3  |             | 2014-12-25
|   | 9 | tasksub 4 main 3  |             | 2014-12-31


人?应该查询我需要使用什么?我需要一些循环吗?帮帮我..

2 个答案:

答案 0 :(得分:0)

您可以使用双循环解决此问题:

<table>
<th>
<td>#</td><td>#</td><td>Task Name</td><td>Task Period</td><td>Task Date</td>
</th>
<?php
$dbhost = 'host';
$dbuser = 'user';
$dbpass = 'passwd';
$dbname = 'thedatabase';
$mysqli = new mysqli("$dbhost", "$dbuser", "$dbpass", "$dbname");//logging in

//perhaps do some error handling if the query fails

$result = $mysqli->query('SELECT task_id, task_name, task_period, task_date FROM tbl_taskmain');//query for main tasks
$result->execute();
while($row = mysqli_fetch_array($result)) {//iterate over the main tasks
    echo "<tr><td>$row[0]</td><td></td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td></tr>"; //write a row to the output
    $subresult = $mysqli->query('SELECT tasksub_id, tasksub_name tasksub_date FROM tbl_tasksub WHERE task_id=?'); //a subquery: all subtasks for the given task id
    $stmt->bind_param("i",$row[0]); //bind the task id
    $subresult->execute();
    while($subrow = mysqli_fetch_array($subresult)) { //iterate over subtasks
        echo "<tr><td></td><td>$subrow[0]</td><td>$subrow[1]</td><td></td><td>$subrow[2]</td></tr>"; //print row
    }
}
mysql_free_result($result);
?>
</table>

答案 1 :(得分:0)

我使用给定的数据创建了数据表,并可以使用以下代码生成预期的表。

// Changes DB details according to your setup
$dbConnection = mysqli_connect('localhost', 'root', 'root', 'task_test');
// Selecting all would be optimal since except status other fields are used
$query = "SELECT * FROM tbl_taskmain LEFT JOIN tbl_tasksub ON tbl_taskmain.task_id = tbl_tasksub.task_id";
$result = mysqli_query($dbConnection, $query);

echo '<table border="1">';
echo "<tr><th>#</th><th>#</th><th>Task Name</th><th>Task Period</th><th>Task Date</th></tr>";

$taskIdCheck = 0;

while ($row = mysqli_fetch_assoc($result)) {
    if ($taskIdCheck != $row['task_id']) { // A new main task
        echo "<tr><td>{$row['task_id']}</td><td></td><td>{$row['task_name']}</td><td>{$row['task_period']}</td><td>{$row['task_date']}</td></tr>";
        echo "<tr><td></td><td>{$row['tasksub_id']}</td><td>{$row['tasksub_name']}</td><td></td><td>{$row['tasksub_date']}</td></tr>"; // First sub task
        $taskIdCheck = $row['task_id'];
    } else { // Sub tasks
        echo "<tr><td></td><td>{$row['tasksub_id']}</td><td>{$row['tasksub_name']}</td><td></td><td>{$row['tasksub_date']}</td></tr>";
    }
}

echo '</table>';

听起来你缺乏这方面所需的PHP和MySQL知识。以下资源可能有所帮助。

http://www.w3schools.com/php/

http://www.phpknowhow.com/mysql/