我如何格式化这个项目

时间:2014-02-14 04:28:47

标签: php mysql

我有几个表构成了一个时间表。我在Linux上使用PHP和MySQL。代码如下:

<?php
session_start();
if(isset($_SESSION['username'])){    
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"  "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link href="styles/style.css" rel="stylesheet" type="text/css">
<title>View All Employees</title>
</head>
<body>
<div id="wrapper">
<div id="header"><br><br><br>
<p>xxxxxxxxxx</p></div>
<div id="side"><?php
include ('includes/side.php');
?>
</div>
<div id="main">
<center><h3><b>Past Schedules</b></h3></center>
<?php

$db = new mysqli('localhost', 'xxxx', 'xxxx', 'xxxx'); 
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
echo '<table>';
echo '<tr>';
echo '<th>Job Date</th><th>Report</th><th>Start</th><th>Customer</th><th>Crew</th>    <th>Equipment</th><th>Description</th><th>Job Notes</th>';
echo '</tr>';


$result = $db->query("SELECT
            `ScheduledJob`.`ScheduledJobID`,
            `ScheduledJob`.`JobDate`,
            `ScheduledJob`.`ReportTime`,
            `ScheduledJob`.`StartTime`,
            `ScheduledJob`.`CustomerID`,
            `Customer`.`Name` AS `CustomerName`,
            `ScheduledEmployee`.`EmployeeID`,
            CAST(NULL AS SIGNED INTEGER) AS `EquipmentID`,
            `ScheduledJob`.`JobDescription`,
            CONCAT(`Employee`.`FirstName`, ' ', `Employee`.`LastName`) AS   `EmployeeName`,
                '' AS `EquipmentNumber`
FROM
            (`ScheduledJob` INNER JOIN
            `Customer` ON `ScheduledJob`.`CustomerID`=`Customer`.`CustomerID`) LEFT JOIN
            (`ScheduledEmployee` INNER JOIN
            `Employee` ON `ScheduledEmployee`.`EmployeeID`=`Employee`.`EmployeeID`) ON   `ScheduledJob`.`ScheduledJobID`=`ScheduledEmployee`.`ScheduledJobID`
UNION
SELECT
            `ScheduledJob`.`ScheduledJobID`,
            `ScheduledJob`.`JobDate`,
            `ScheduledJob`.`ReportTime`,
            `ScheduledJob`.`StartTime`,
            `ScheduledJob`.`CustomerID`,
            `Customer`.`Name` AS `CustomerName`,
            CAST(NULL AS SIGNED INTEGER) AS `EmployeeID`,
            `ScheduledEquipment`.`EquipmentID`,
            `ScheduledJob`.`JobDescription`,
            '' AS `EmployeeName`,
            `Equipment`.`Number` AS `EquipmentNumber`
FROM
            (`ScheduledJob` INNER JOIN
            `Customer` ON `ScheduledJob`.`CustomerID`=`Customer`.`CustomerID`) LEFT JOIN
            (`ScheduledEquipment` INNER JOIN
            `Equipment` ON `ScheduledEquipment`.`EquipmentID`=`Equipment`.`EquipmentID`)     ON `ScheduledJob`.`ScheduledJobID`=`ScheduledEquipment`.`ScheduledJobID`;");
while($job = $result->fetch_object()){ 
echo '<tr>';    
echo '<td>',$job->JobDate,'</td>';
echo '<td>',$job->ReportTime,'</td>';
echo '<td>',$job->StartTime,'</td>';
echo '<td>',$job->CustomerName,'</td>';
echo '<td>',$job->EmployeeName,'</td>';
echo '<td>',$job->EquipmentNumber,'</td>';
echo '<td>',$job->JobDescription,'</td>';
echo '<td>',$job->JobNotes,'</td>';     
echo '</tr>';
}
echo '</table>';
?>
<?php
} else {
header("location:index.php");
}
?></div>
<div id="footer"></div>
</div>
</body>
</html> 

输出目前是这样的:

-------------------------------------------------------------------------------------------------------------------
Job Date    Report      Start       Customer        Crew              Equipment Description             Job Notes
 2014-02-13 07:00:00    08:00:00    Spruence Genco  Steven Gray                   This is just a sample 
 2014-02-13 07:00:00    08:00:00    Spruence Genco  Phil Dunfy                  This is just a sample   
 2014-02-13 07:00:00    08:00:00    Spruence Genco  Donald Duck                 This is just a sample   
 2014-02-13 07:00:00    08:00:00    Spruence Genco                   5234       This is just a sample   
 2014-02-13 07:00:00    08:00:00    Spruence Genco                   3758       This is just a sample
----------------------------------------------------------------------------------------     ----------------------------

但我需要它是这样的:

----------------------------------------------------------------------------------------    -----------------------------
Job Date    Report      Start       Customer        Crew                Equipment   Description             Job Notes
2014-02-13  07:00:00    08:00:00    Spruence Genco  Steven Gray     5234            This is just a sample   
                                                    Phil Dunfy      3758            
                                                     Donald Duck
----------------------------------------------------------------------------------------------------------------------

我该怎么办?我只希望日期,报告,开始,客户,描述和注释只显示一次......并列出所有工作人员 和设备。

请帮帮我......

非常感谢.....

1 个答案:

答案 0 :(得分:0)

如果要在第一行显示完整数据,可以执行以下操作:

    $i = 0;
        while($job = $result->fetch_object()){ 

    if( $i == 0 ) {
        echo '<tr>';    
        echo '<td>' . $job->JobDate .'</td>';
        echo '<td>' . $job->ReportTime.'</td>';
        echo '<td>' .$job->StartTime.'</td>';
        echo '<td>'.$job->CustomerName.'</td>';
        echo '<td>'.$job->EmployeeName.'</td>';
        echo '<td>'.$job->EquipmentNumber.'</td>';
        echo '<td>'.$job->JobDescription.'</td>';
        echo '<td>'.$job->JobNotes.'</td>';     
        echo '</tr>';

    } else {
            echo '<tr>';    
        echo '<td></td>';
        echo '<td></td>';
        echo '<td></td>';
        echo '<td></td>';
        echo '<td>' . $job->EmployeeName . '</td>';
        echo '<td>' . $job->EquipmentNumber .'</td>';
        echo '<td></td>';
        echo '<td></td>';     
        echo '</tr>';
    }
    $i++;
        }