使用 PHP MYSQL 生成月度日期和小时报表
Posted
技术标签:
【中文标题】使用 PHP MYSQL 生成月度日期和小时报表【英文标题】:Monthly Date and Hour wise report generation using PHP MYSQL 【发布时间】:2019-11-17 18:38:45 【问题描述】:php 中的考勤门户
我在生成最终出勤报告时遇到问题
我被困在项目的最后阶段:
我有一个出勤表,如果学生在场,我将其标记为 1,如果缺席 0。 我们有日期,星期顺序,小时(我们遵循日顺序时间表)
Table
dayorder:
id day
1 day1
2 day2
3 day3
4 day4
5 day5
6 SAT
7 day6
Table
Hour:
id
1
2
3
4
5
6
7
餐桌出勤率:
我必须像下面这样生成一个月度报告
我尝试过的代码:
SELECT student_id,deptno,Month, Year_c,
branch.description as bdesc,
course.coursecode as ccd,
users.firstname as ufn,
users.lastname as uln,
course.description as ccdes,schedules.hour as hhour,
h,dayorder,AttdDate,
CONCAT(AttdDate,h,dayorder) AS fate,
IF(Day_c=1, p, " ") AS '1',
IF(Day_c=2, p, " ") AS '2',
IF(Day_c=3, p, " ") AS '3',
IF(Day_c=4, p, " ") AS '4',
IF(Day_c=5, p, " ") AS '5',
IF(Day_c=6, p, " ") AS '6',
IF(Day_c=7, p, " ") AS '7',
IF(Day_c=8, p, " ") AS '8',
IF(Day_c=9, p, " ") AS '9',
IF(Day_c=10, p, " ") AS '10',
IF(Day_c=11, p, " ") AS '11',
IF(Day_c=12, p, " ") AS '12',
IF(Day_c=13, p, " ") AS '13',
IF(Day_c=14, p, " ") AS '14',
IF(Day_c=15, p, " ") AS '15',
IF(Day_c=16, p, " ") AS '16',
IF(Day_c=17, p, " ") AS '17',
IF(Day_c=18, p, " ") AS '18',
IF(Day_c=19, p, " ") AS '19',
IF(Day_c=20, p, " ") AS '20',
IF(Day_c=21, p, " ") AS '21',
IF(Day_c=22, p, " ") AS '22',
IF(Day_c=23, p, " ") AS '23',
IF(Day_c=24, p, " ") AS '24',
IF(Day_c=25, p, " ") AS '25',
IF(Day_c=26, p, " ") AS '26',
IF(Day_c=27, p, " ") AS '27',
IF(Day_c=28, p, " ") AS '28',
IF(Day_c=29, p, " ") AS '29',
IF(Day_c=30, p, " ") AS '30',
IF(Day_c=31, p, " ") AS '31'
FROM
(SELECT *,DAY(date) AS Day_c,
MONTHNAME(date) AS Month,
Year(date) AS Year_c,
date(date) AS AttdDate,hour as h, day as dayorder,
(CASE WHEN present = 1
THEN 'P'
WHEN present = 0
THEN 'A'
WHEN present is null
THEN ' '
END) AS p
FROM attendance a
WHERE date between '$from' AND '$to' And branch = $branchid AND coursecode = $courseid AND batch = $batchid
GROUP BY student_id
ORDER BY student_rollno ASC
)
as report
LEFT JOIN branch on branch.id = report.branch
LEFT JOIN course on course.id = report.coursecode
LEFT JOIN users on users.id = report.user
LEFT JOIN schedules on schedules.id = report.hour
ORDER BY Month DESC, Year_c DESC
PHPMYADMIN 中的输出 但我无法使用 PHP 和 MY SQL 显示为 html 页面
【问题讨论】:
你需要规范你的数据库。 @Dharman 你能帮忙举几个例子吗 未定义:student_rollno
。请修复。
phpmyadmin 中的输出与“必须生成”示例不匹配。我们应该为哪一个而努力?
主表请提供SHOW CREATE TABLE
。
【参考方案1】:
我为你的情况写了一个 PHP mysql 的例子,但没有得到你的 SQL,为什么你有一天的描述也是第一个头不知道的。此外,您必须在几个小时内添加代码,这只是 PHP MySQL 示例的正确方法,如果您卡住了,我建议您使用 ez_SQL 更好的对象编程与 oho MySQL,不要犹豫评论
<?php
$sql="SELECT * FROM attendance ORDER BY student_id ASC, date ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0)
// output data of each row
echo "<table><thead><tr><td># Student ID</td><td>Dept No</td><td>Description</td>";
for ($i=0; $i < $day; $i++)
echo "<td>".$i."</td>";
echo "</tr></thead><body><tr>";
while($row = $result->fetch_assoc())
$student_id = $row->student_id;
$deptno = $row->deptno;
$decp = $row->date." ".$row->semester." ".$row->day;
$absence = $row->present;
if(isset($temp) and $temp != $student_id)
echo "</tr>";
echo "<tr>";
echo "<td>".$student_id."</td><td>".$deptno."</td><td>".$decp."</td>"
elseif(!isset($temp))
echo "<td>".$student_id."</td><td>".$deptno."</td><td>".$decp."</td>";
echo "<td>".$absence."</td>";
$temp = $student_id;
echo "</tr></tbody></table>";
else
echo "0 attendance taken";
【讨论】:
我们有日订单系统,例如从第 1 天到第 6 天和星期六,有 7 个时段,每个日期都分配有一个天订单。在给定的图片中,您可以查看描述“2019-06-17”是日期“5”是期间“Day6”是日订单 因此,只需在 foreach 中添加 Dayorder 并检查订单中当前日期的位置,这样您就可以安排 tahat 但尝试代码看看发生了什么,它会让您对它有所了解【参考方案2】:我是用 PHP 做的
<?php
include 'includes/session.php';
if(isset($_POST["from"], $_POST["to"],$_POST["branchid"],$_POST["courseid"],$_POST["batchid"]))
$from = $_POST["from"];
$to = $_POST["to"];
$branchid = $_POST["branchid"];
$courseid = $_POST["courseid"];
$batchid = $_POST["batchid"];
$presentd = array();
$datea = array();
$daya = array();
$houra = array();
$TotStudents = array();
$th="";
$td="";
$perpre = "";
$A = array();
$Studenttblbodypresent = "<table id='Studentpresent' class='table table-bordered dataTable no-footer '>
<thead class = 'thead-present'><tr><th>DeptNo</th>";
$Studenttblbody = "";
$stdsql = "SELECT deptno,name
FROM attendance a WHERE
date between '$from' AND '$to' AND a.branch = $branchid AND a.coursecode = $courseid AND a.batch = $batchid
GROUP BY student_id
ORDER BY student_rollno ASC";
$querystd = $conn->query($stdsql)or die($conn->error);
$studentNo = $querystd->num_rows;
if($studentNo >0)
// report common header
$sql = "SELECT MONTHNAME(date) AS Month,
branch.description as bdesc,
course.coursecode as ccd,
users.firstname as ufn,
users.lastname as uln,
course.description as ccdes,
schedules.hour as hhour,
day.description as dday,
a.hour as hourid,
day,course.semester as sem,date(date) AS AttdDate
FROM attendance a
LEFT JOIN branch on branch.id = a.branch
LEFT JOIN course on course.id = a.coursecode
LEFT JOIN users on users.id = a.user
LEFT JOIN schedules on schedules.id = a.hour
LEFT JOIN day on day.id = a.day
WHERE date between '$from' AND '$to' AND a.branch = $branchid AND a.coursecode = $courseid AND a.batch = $batchid
GROUP BY AttdDate";
$query = $conn->query($sql)or die($conn->error);
$dateNo = $query->num_rows;
$rowd = $query->fetch_assoc();
if($rowd['sem'] == 6)$sem = 'VI';
else if($rowd['sem'] == 5)$sem = 'V';
else if($rowd['sem'] == 4)$sem = 'IV';
else if($rowd['sem'] == 3)$sem = 'III';
else if($rowd['sem'] == 2)$sem = 'II';
else if($rowd['sem'] == 1)$sem = 'I';
else $sem = "";
//report header
$RepHdrtblbody = "<tr role='row'>
<td>".$rowd['Month']."</td>
<td>".$sem."</td>
<td>".$rowd['bdesc']."</td>
<td>".$rowd['ccdes']."</td>
<td>".$rowd['ccd']."</td>
<td>".$rowd['ufn']." ".$rowd['uln']."</td>
<td>".$dateNo."</td>
</tr>";
$th ='<td class="dispdates"><span>'.$rowd['AttdDate'].'<br>'.$rowd['hhour'].'<br>'.$rowd['dday'].'</span></td>';
$datea[] = $rowd['AttdDate'];
$daya[] = $rowd['day'];
$houra[] = $rowd['hourid'];$hhour ='';$day = '';
while($prow = $query->fetch_assoc())
$date = $prow['AttdDate'];
$hhour = $prow['hhour'];
$day = $prow['dday'];
$hourid = $prow['hourid'];
$datea[] = $prow['AttdDate'];
$daya[] = $prow['day'];
$houra[] = $prow['hourid'];
//student table header
$th.= '<td class="dispdates"><span>'.$date.'<br>'.$hhour.'<br>'.$day.'</span></td>';
$th.= "<td>Present%</td></tr>";
$Studenttblbodypresent.=$th."</thead>";
while($prowstd = $querystd->fetch_assoc())
//student list
//$Studenttblbody.= '<tr role="row"><td>'.$prowstd['deptno'].'</td><td>'.$prowstd['name'].'</td></tr>';
$TotStudents[] = $prowstd['deptno'];
$datei=$datea;
$dayi=$daya;
$houri=$houra;
$curStud = $prowstd['deptno'];
$countsql = "SELECT a.student_id as regno,
a.student_rollno AS rollno,
a.name,
a.deptno,
a.year,
branch.description as bdesc,
course.coursecode as ccd,
users.firstname as ufn,
users.lastname as uln,
course.description as ccdes
, SUM(1) AS tot
, SUM(a.present = 1) AS P
, SUM(a.present = 0 ) AS A
, 100.0
* SUM(a.present = 1)
/ SUM(1) AS perpre
, 100.0
* SUM(a.present = 0 )
/ SUM(1) AS perabs
FROM attendance a
LEFT JOIN branch on branch.id = a.branch
LEFT JOIN course on course.id = a.coursecode
LEFT JOIN users on users.id = a.user
where a.date BETWEEN '$from' AND '$to' AND
a.branch = $branchid and a.coursecode = $courseid and a.batch = $batchid
AND a.active = 1 AND a.user = $usnid AND a.deptno = '$curStud'
GROUP BY a.student_id
ORDER BY a.student_rollno ASC";
$countquery = $conn->query($countsql);
$countrow = $countquery->fetch_assoc();
$perpre = $countrow['perpre'];
$h = array();
$da = array();
$d = array();
//$th = "";
$td = "<tr class='student'><th class = 'thead-present'>".$curStud."</th>";
for ($i=0;$i < $dateNo;$i++)
$h = $houri[$i];
$da = $datei[$i];
$d = $dayi[$i];
$b = $branchid;
$c = $courseid;
$ba = $batchid;
$sqls = "SELECT *,deptno,AttdDate,p
FROM
(SELECT *,DAY(date) AS Day_c,
MONTHNAME(date) AS Month,
Year(date) AS Year_c,
date(date) AS AttdDate,hour as h, day as dayorder,
(CASE WHEN present = 1
THEN ''
WHEN present = 0
THEN 'A'
END) AS p
FROM attendance a
WHERE a.hour = $h AND a.date = '$da' AND a.day = '$d' AND a.branch = $b AND a.coursecode = $c
AND a.batch = $ba AND deptno = '$curStud'
ORDER BY student_rollno ASC
)
as report
LEFT JOIN schedules on schedules.id = report.hour
ORDER BY Month DESC, Year_c DESC,student_rollno ASC,AttdDate ASC";
$querys = $conn->query($sqls)or die($conn->error);
while($prows = $querys->fetch_assoc())
$td.= "<td>".$prows['p']."</td>";
$td.="<td>".$perpre."</td></tr>";
$Studenttblbodypresent.= $td;
echo json_encode(array('RepHdrtblbody'=>$RepHdrtblbody,
'present'=>$Studenttblbodypresent,
));
else
echo "You have not given attendance";
?>
【讨论】:
以上是关于使用 PHP MYSQL 生成月度日期和小时报表的主要内容,如果未能解决你的问题,请参考以下文章