我现在正在从头开始学习PHP,以便从头开始组建薪资系统。
我正在尝试编写PHP代码以从用户和项目数据库中抽出数小时,并按照日期和项目细分为每个用户安排整齐的时间表。
目前,我在给定特定用户和日期范围的情况下从数据库中提取所有记录,然后使用while循环显示数字。它每个项目每天不能处理多个时间表,或者在给定的一天中有0小时。
我正在使用的代码很长很乱,看起来像这样:
<?php
mysql_connect("localhost","root","");
mysql_select_db("gemsclip");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$date_range_start = "2014-02-02";
$date_range_end = "2014-02-08";
$query = ("SELECT
a.user_id,
a.first_name,
a.last_name,
a.department,
a.supervisor,
b.date,
b.clock_in,
b.clock_out,
b.project,
b.project_id,
b.time_diff
FROM user a, date b
WHERE a.user_id = b.user_id and a.user_id = ".$_SESSION['user_id']." and b.date >= '$date_range_start' and b.date <= '$date_range_end'");
$result = mysql_query($query);
$hours = 0;
while ($row = mysql_fetch_assoc($result)) {
$project = $row['project']." (".$row['project_id'].")";
$time_diff = $row['time_diff'];
$user = $row['user_id'];
$date = $row['date'];
$time = [];
$last_name = $row['last_name'];
$first_name = $row['first_name'];
$department = $row['department'];
$supervisor = $row['supervisor'];
}
?>
<table border="1" width="100%" style="font-weight:bold; border-collapse:collapse;">
<tr>
<td rowspan="2" colspan="2"><h3 style="margin-left:2%;">Weekly Timesheet</h3></td>
<td colspan="7" height="40px"></td>
</tr>
<tr>
<td colspan="7" align="right">1875 Campus Commons Dr, Reston, VA 20191 Tel: 703-288-3382 WWW: http://www.ecompex.com</td>
</tr>
<tr>
<td>Name:</td>
<td colspan="8"><?php echo $last_name.", ".$first_name; ?></td>
</tr>
<tr>
<td>Department:</td>
<td colspan="8"><?php echo $department; ?></td>
</tr>
<tr>
<td>Period Start:</td>
<td colspan="8"><?php echo date("m/d/Y",strtotime('last sunday'));?></td>
</tr>
<tr>
<td>Period End:</td>
<td colspan="8"><?php echo date("m/d/Y",strtotime('last sunday + 6 days'));?></td>
</tr>
<tr>
<td rowspan="2" align="center">Job Description</td>
<td width="10%" align="center">SUN</td>
<td width="10%" align="center">MON</td>
<td width="10%" align="center">TUE</td>
<td width="10%" align="center">WED</td>
<td width="10%" align="center">THU</td>
<td width="10%" align="center">FRI</td>
<td width="10%" align="center">SAT</td>
<td></td>
</tr>
<tr>
<td align="center"><?php echo date("m/d/Y",strtotime('last sunday'));?></td>
<td align="center"><?php echo date("m/d/Y",strtotime('last sunday + 1 days'));?></td>
<td align="center"><?php echo date("m/d/Y",strtotime('last sunday + 2 days'));?></td>
<td align="center"><?php echo date("m/d/Y",strtotime('last sunday + 3 days'));?></td>
<td align="center"><?php echo date("m/d/Y",strtotime('last sunday + 4 days'));?></td>
<td align="center"><?php echo date("m/d/Y",strtotime('last sunday + 5 days'));?></td>
<td align="center"><?php echo date("m/d/Y",strtotime('last sunday + 6 days'));?></td>
<td align="center">TOTAL</td>
</tr>
<?php
mysql_connect("localhost","root","");
mysql_select_db("gemsclip");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Later this will be defined via user input
$date_range_start = "2014-02-02";
$date_range_end = "2014-02-08";
$query = ("SELECT a.user_id, a.first_name, a.last_name, a.department, b.date, b.clock_in, b.clock_out, b.project, b.project_id, b.time_diff
FROM user a, date b
WHERE a.user_id = b.user_id and a.user_id = ".$_SESSION['user_id']." and b.date >= '$date_range_start' and b.date <= '$date_range_end'");
$result = mysql_query($query);
$total_hours= 0;
$hours = 0;
$saturday=5;
$sunday=4;
$friday=3;
$thursday=2;
$wednesday=1;
$tuesday=0;
$sunday_hours = 0;
$monday_hours = 0;
$tuesday_hours = 0;
$wednesday_hours = 0;
$thursday_hours = 0;
$friday_hours = 0;
$saturday_hours = 0;
while ($row = mysql_fetch_assoc($result)) {
$sunday++;
$saturday++;
$tuesday++;
$wednesday++;
$thursday++;
$friday++;
$project = $row['project']." (".$row['project_id'].")";
$time_diff = $row['time_diff'];
$user = $row['user_id'];
$date = $row['date'];
$time = [];
if ($sunday % 5 == 0){
echo "<tr>";
echo "<td align='right'>"." ".$project." </td>";
echo "<td align='center'>"." 0.00 </td> ";
echo "<td align='center'>"." ".$time_diff." </td> ";
$hours += $time_diff;
$total_hours += $time_diff;
$monday_hours += $time_diff;
}
if($tuesday % 5 == 0){
echo "<td align='center'>"." ".$time_diff." </td> ";
$hours += $time_diff;
$total_hours += $time_diff;
$tuesday_hours += $time_diff;
}
if($wednesday % 5 == 0){
echo "<td align='center'>"." ".$time_diff." </td> ";
$hours += $time_diff;
$total_hours += $time_diff;
$wednesday_hours += $time_diff;
}
if($thursday% 5 == 0){
echo "<td align='center'>"." ".$time_diff." </td> ";
$hours += $time_diff;
$total_hours += $time_diff;
$thursday_hours += $time_diff;
}
if($friday % 5 == 0){
echo "<td align='center'>"." ".$time_diff." </td> ";
$hours += $time_diff;
$total_hours += $time_diff;
$friday_hours += $time_diff;
}
if($saturday % 5 == 0){
echo "<td align='center'>"." 0.00 </td> ";
echo "<td align='center'>"." ".number_format($hours, 2, '.', '')." </td> ";
echo "</tr>";
$hours = 0;
}
}
$last_name = $row['last_name'];
$first_name = $row['first_name'];
$department = $row['department'];
$supervisor = $row['supervisor'];
?>
<tr>
<td align="right"> TOTAL: </td>
<td align="center"><?php echo number_format($sunday_hours, 2, '.', ''); ?></td>
<td align="center"><?php echo number_format($monday_hours, 2, '.', ''); ?></td>
<td align="center"><?php echo number_format($tuesday_hours, 2, '.', ''); ?></td>
<td align="center"><?php echo number_format($wednesday_hours, 2, '.', ''); ?></td>
<td align="center"><?php echo number_format($thursday_hours, 2, '.', ''); ?></td>
<td align="center"><?php echo number_format($friday_hours, 2, '.', ''); ?></td>
<td align="center"><?php echo number_format($saturday_hours, 2, '.', ''); ?></td>
<td align="center"><?php echo number_format($total_hours, 2, '.', ''); ?></td>
</tr>
<tr>
<td rowspan="1">Explanation of Changes:</td>
<td rowspan="1" colspan="8"></td>
</tr>
<tr>
<td rowspan="1" colspan="9" style="border:0px;">:</td>
</tr>
<tr>
<td rowspan="1" colspan="9">:</td>
</tr>
<tr>
<td colspan="4">Employee Signature:</td>
<td colspan="5">Approval Signature:</td>
</tr>
<tr>
<td colspan="3" align="right">Date:</td>
<td colspan="1" align="center"><?php echo date("Y-m-d",time());?></td>
<td colspan="1">Print Name:</td>
<td colspan="2"><?php echo $supervisor; ?></td>
<td align="right">Date:</td>
<td align="center"><?php echo date("Y-m-d",time());?></td>
</tr>
<tr>
<td colspan="5"></td>
<td colspan="4" align="center">FOR OFFICIAL USE ONLY</td>
</tr>
<tr>
<td colspan="5"></td>
<td colspan="3" align="right">Approved Regular Hours:</td>
<td align="center"></td>
</tr>
<tr>
<td colspan="5"></td>
<td colspan="3" align="right">Approved Overtime Hours:</td>
<td align="center"></td>
</tr>
<tr>
<td colspan="5"></td>
<td colspan="3" align="right">Others:</td>
<td align="center"></td>
</tr>
</table>
输出看起来像这样
是否有更高效/更简单的方法将数据从SQL加载到这样的表中?当我需要稍后使用PHPExcel将其转换为excel时,我是否会遇到以这种方式格式化数据的问题?