|
 |
|
ตารางปิดงานของพนักงานวันเดือนปี จะให้โชว์ผลรวมยังไงหรอครับ |
|
 |
|
|
 |
 |
|

Code (PHP)
<center><h3>ตารางวันเวลา LAY</h3></center>
<!--เริ่มต้นตาราง-->
<div class="table-responsive">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table >
<tr>
<td>ระบุเดือน-ปี : </td>
<td>
<select name="txt_month">
<option value="">--------------</option>
<?php
$month = array('01' => 'มกราคม', '02' => 'กุมภาพันธ์', '03' => 'มีนาคม', '04' => 'เมษายน',
'05' => 'พฤษภาคม', '06' => 'มิถุนายน', '07' => 'กรกฎาคม', '08' => 'สิงหาคม',
'09' => 'กันยายน ', '10' => 'ตุลาคม', '11' => 'พฤศจิกายน', '12' => 'ธันวาคม');
$txtMonth = isset($_POST['txt_month']) && $_POST['txt_month'] != '' ? $_POST['txt_month'] : date('m');
foreach($month as $i=>$mName) {
$selected = '';
if($txtMonth == $i) $selected = 'selected="selected"';
echo '<option value="'.$i.'" '.$selected.'>'. $mName .'</option>'."\n";
}
?>
</select>
</td>
<td>
<select name="txt_year">
<option value="">--------------</option>
<?php
$txtYear = (isset($_POST['txt_year']) && $_POST['txt_year'] != '') ? $_POST['txt_year'] : date('Y');
$yearStart = date('Y');
$yearEnd = $txtYear-5;
for($year=$yearStart;$year > $yearEnd;$year--){
$selected = '';
if($txtYear == $year) $selected = 'selected="selected"';
echo '<option value="'.$year.'" '.$selected.'>'. ($year+543) .'</option>'."\n";
}
?>
</select>
</td>
<td><input type="submit" value="ค้นหา" /></td>
</tr>
</table>
</form>
<br>
<?php
//รับค่าตัวแปรที่ส่งมาจากแบบฟอร์ม HTML
$year = isset($_POST['txt_year']) ? mysqli_real_escape_string($con,$_POST['txt_year']) : '';
$month = isset($_POST['txt_month']) ? mysqli_real_escape_string($con,$_POST['txt_month']) : '';
if($year == '' || $month == '') exit('<p class="fail">กรุณาระบุ "เดือน-ปี" ที่ต้องการเรียกรายงาน</p>');
//ดึงข้อมูลพนักงานทั้งหมด
$allEmpData = array();
$strSQL = "SELECT * FROM user a,blockzone b
WHERE a.bz_id = b.bz_id
order by a.bz_id asc";
$qry = mysqli_query($con , $strSQL) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysqli_error());
while($row = mysqli_fetch_assoc($qry)){
$allEmpData[$row['no_user']] = array ('empname'=> $row['user_name'],'blockzone'=>$row['bz_name']);
}
/*echo "<pre>";
print_r($allEmpData);
echo "</pre>";*/
//เรียกข้อมูลการจองของเดือนที่ต้องการ
$allReportData = array();
$strSQL = "SELECT no_user, DAY(`time`) AS time, COUNT(*) AS numBook FROM `lay` ";
$strSQL.= "WHERE `time` LIKE '$year-$month%' ";
$strSQL.= "GROUP by no_user,DAY(`time`)";
$qry = mysqli_query($con , $strSQL) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysqli_error());
while($row = mysqli_fetch_assoc($qry)){
$allReportData[$row['no_user']][$row['time']] = $row['numBook'];
}
echo "<table class='table table-bordered table-sm' >";
echo '<tr class="table-primary">';//เปิดแถวใหม่ ตาราง HTML
echo '<th>blockzone</th>';
echo '<th>รายชื่อพนักงาน</th>';
//วันที่สุดท้ายของเดือน
$timeDate = strtotime($year.'-'.$month."-01"); //เปลี่ยนวันที่เป็น timestamp
$lastDay = date("t", $timeDate); //จำนวนวันของเดือน
//สร้างหัวตารางตั้งแต่วันที่ 1 ถึงวันที่สุดท้ายของดือน
for($day=1;$day<=$lastDay;$day++){
echo '<th>' . substr("0".$day, -2) . '</th>';
}
echo '<th>ผลรวม</th>';
echo "</tr>";
foreach($allEmpData as $noUser => $array){
$emp_name = $array['empname'];
$bz_id = $array['blockzone'];
echo '<tr>';//เปิดแถวใหม่ ตาราง HTML
echo '<td>'. $bz_id .'</td>';
echo '<td>'. $emp_name.'</td>';
//เรียกข้อมูลการจองของพนักงานแต่ละคน ในเดือนนี้
for($j=1;$j<=$lastDay;$j++){
$numBook = isset($allReportData[$noUser][$j]) ? '<div>'.$allReportData[$noUser][$j].'</div>' : 0;
echo "<td class='number'>", $numBook, "</td>";
}
echo '</tr>';//ปิดแถวตาราง HTML
}
echo "</table>";
mysqli_close($con);//ปิดการเชื่อมต่อฐานข้อมูล
?>
</div>
</div>
พอดีผมสงสัยอ่ะครับ ว่าทำไงให้ผลรวมออกมาได้ครับ
Tag : PHP, MySQL

|
|
 |
 |
 |
 |
Date :
2018-12-19 15:12:31 |
By :
ttouch |
View :
1974 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<center><h3>ตารางวันเวลา LAY</h3></center>
<!--เริ่มต้นตาราง-->
<div class="table-responsive">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table >
<tr>
<td>ระบุเดือน-ปี : </td>
<td>
<select name="txt_month">
<option value="">--------------</option>
<?php
$month = array('01' => 'มกราคม', '02' => 'กุมภาพันธ์', '03' => 'มีนาคม', '04' => 'เมษายน',
'05' => 'พฤษภาคม', '06' => 'มิถุนายน', '07' => 'กรกฎาคม', '08' => 'สิงหาคม',
'09' => 'กันยายน ', '10' => 'ตุลาคม', '11' => 'พฤศจิกายน', '12' => 'ธันวาคม');
$txtMonth = isset($_POST['txt_month']) && $_POST['txt_month'] != '' ? $_POST['txt_month'] : date('m');
foreach($month as $i=>$mName) {
$selected = '';
if($txtMonth == $i) $selected = 'selected="selected"';
echo '<option value="'.$i.'" '.$selected.'>'. $mName .'</option>'."\n";
}
?>
</select>
</td>
<td>
<select name="txt_year">
<option value="">--------------</option>
<?php
$txtYear = (isset($_POST['txt_year']) && $_POST['txt_year'] != '') ? $_POST['txt_year'] : date('Y');
$yearStart = date('Y');
$yearEnd = $txtYear-5;
for($year=$yearStart;$year > $yearEnd;$year--){
$selected = '';
if($txtYear == $year) $selected = 'selected="selected"';
echo '<option value="'.$year.'" '.$selected.'>'. ($year+543) .'</option>'."\n";
}
?>
</select>
</td>
<td><input type="submit" value="ค้นหา" /></td>
</tr>
</table>
</form>
<br>
<?php
//รับค่าตัวแปรที่ส่งมาจากแบบฟอร์ม HTML
$year = isset($_POST['txt_year']) ? mysqli_real_escape_string($con,$_POST['txt_year']) : '';
$month = isset($_POST['txt_month']) ? mysqli_real_escape_string($con,$_POST['txt_month']) : '';
if($year == '' || $month == '') exit('<p class="fail">กรุณาระบุ "เดือน-ปี" ที่ต้องการเรียกรายงาน</p>');
//ดึงข้อมูลพนักงานทั้งหมด
$allEmpData = array();
$strSQL = "SELECT * FROM user a,blockzone b
WHERE a.bz_id = b.bz_id
order by a.bz_id asc";
$qry = mysqli_query($con , $strSQL) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysqli_error());
while($row = mysqli_fetch_assoc($qry)){
$allEmpData[$row['no_user']] = array ('empname'=> $row['user_name'],'blockzone'=>$row['bz_name']);
}
/*echo "<pre>";
print_r($allEmpData);
echo "</pre>";*/
//เรียกข้อมูลการจองของเดือนที่ต้องการ
$allReportData = array();
$strSQL = "SELECT no_user, DAY(`time`) AS time, COUNT(*) AS numBook FROM `lay` ";
$strSQL.= "WHERE `time` LIKE '$year-$month%' ";
$strSQL.= "GROUP by no_user,DAY(`time`)";
$qry = mysqli_query($con , $strSQL) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysqli_error());
while($row = mysqli_fetch_assoc($qry)){
$allReportData[$row['no_user']][$row['time']] = $row['numBook'];
}
echo "<table class='table table-bordered table-sm' >";
echo '<tr class="table-primary">';//เปิดแถวใหม่ ตาราง HTML
echo '<th>blockzone</th>';
echo '<th>รายชื่อพนักงาน</th>';
//วันที่สุดท้ายของเดือน
$timeDate = strtotime($year.'-'.$month."-01"); //เปลี่ยนวันที่เป็น timestamp
$lastDay = date("t", $timeDate); //จำนวนวันของเดือน
//สร้างหัวตารางตั้งแต่วันที่ 1 ถึงวันที่สุดท้ายของดือน
for($day=1;$day<=$lastDay;$day++){
echo '<th>' . substr("0".$day, -2) . '</th>';
}
echo '<th>ผลรวม</th>';
echo "</tr>";
foreach($allEmpData as $noUser => $array){
$emp_name = $array['empname'];
$bz_id = $array['blockzone'];
echo '<tr>';//เปิดแถวใหม่ ตาราง HTML
echo '<td>'. $bz_id .'</td>';
echo '<td>'. $emp_name.'</td>';
//เรียกข้อมูลการจองของพนักงานแต่ละคน ในเดือนนี้
$sumBook = 0;
for($j=1;$j<=$lastDay;$j++){
$numBook = isset($allReportData[$noUser][$j]) ? '<div>'.$allReportData[$noUser][$j].'</div>' : 0;
echo "<td class='number'>", $numBook, "</td>";
$sumBook += @$allReportData[$noUser][$j];
}
echo "<td>", $sumBook, "</td>";
echo '</tr>';//ปิดแถวตาราง HTML
}
echo "</table>";
mysqli_close($con);//ปิดการเชื่อมต่อฐานข้อมูล
?>
</div>
</div>
|
 |
 |
 |
 |
Date :
2018-12-19 17:38:15 |
By :
Jatmentz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|