如何使用php-mysql显示不同表中的每一行?

时间:2012-06-23 13:09:43

标签: php mysql

我有三张桌子:

员工(雇员,FNAME,L-NAME ...)
ProjectHeader(专案编号,LeadID,状态....)
ProjectDetails(ProjectDetailsID,专案编号....)

这是我目前的代码:

$get_projects = "SELECT DISTINCT projectheader.ProjectID, projectheader.ProjectName ,projectheader.Lead, projectheader.StartDate, projectheader.EndDate, projectheader.Status, projectheader.Remarks, CONCAT (employee.Firstname, ' ' ,employee.Lastname) AS EmployeeName
FROM employee, projectheader, projectdetails
WHERE projectheader.ProjectID = projectdetails.ProjectID
AND projectheader.Lead = employee.EmployeeID";

$result = mysql_query($get_projects);

$project_id = "";
$project_name = "";
$project_lead = "";
$project_start = "";
$project_end = "";
$project_status = "";
$project_remarks = "";
$table = "";
$table .= "<table border='1'> <th>ProjectID</th>" 
."<th>Project name</th>" 
."<th>Lead</th>" 
."<th>Start</th>" 
."<th>End</th>" 
."<th>Status</th>" 
."<th>Remark</th>";
while($row = mysql_fetch_array($result))
{
    $project_id = $row['ProjectID'];
    $project_name =  $row['ProjectName'];
    $project_lead = $row['EmployeeName'];
    $project_start = $row['StartDate'];
    $project_end = $row['EndDate'];
    $project_status = $row['Status'];
    $project_remarks = $row['Remarks'];

    $table .= "<tr>";
    $table .= "<td>" .$project_id ."</td>"; 
    $table .= "<td>" .$project_name ."</td>"; 
    $table .= "<td>" .$project_lead ."</td>"; 
    $table .= "<td>" .$project_start ."</td>"; 
    $table .= "<td>" .$project_end ."</td>"; 
    $table .= "<td>" .$project_status ."</td>"; 
    $table .= "<td>" .$project_remarks ."</td>"; 
    $table .= "</tr>";

}
$table .= "</table>";
echo $table;
?>

我现在所拥有的是这样的enter image description here

我想拥有的不是让每一行显示在一个表中,而是每一行都有它的表。

enter image description here

1 个答案:

答案 0 :(得分:2)

只需在table循环内创建一个新的while,即可为每个SQL结果行创建一个新表。请注意,表格标题(tr

周围还缺少th个标记
while($row = mysql_fetch_array($result))
{
    $table .= "<table border='1'>"
    ."<tr><th>ProjectID</th>" 
    ."<th>Project name</th>" 
    ."<th>Lead</th>" 
    ."<th>Start</th>" 
    ."<th>End</th>" 
    ."<th>Status</th>" 
    ."<th>Remark</th></tr>";

...

    $table .= "<tr>";
    $table .= "<td>" .$project_id ."</td>"; 
    $table .= "<td>" .$project_name ."</td>"; 
    $table .= "<td>" .$project_lead ."</td>"; 
    $table .= "<td>" .$project_start ."</td>"; 
    $table .= "<td>" .$project_end ."</td>"; 
    $table .= "<td>" .$project_status ."</td>"; 
    $table .= "<td>" .$project_remarks ."</td>"; 
    $table .= "</tr>";
    $table .= "</table>";
}

echo $table;