2
<?php require_once('Connections/dbconnect.php'); ?>
<?php
$c=$_GET['Class'];
$s=$_GET['Section'];
$f=$_GET['fmonth'];
$t=$_GET['tmonth'];
$count=1;
mysql_select_db($database_dbconnect, $dbconnect);
$query_classwise = "SELECT Name_of_Student, Roll_no, Section,
GROUP_CONCAT(if(DAY(`Date`) = 1, `Status`, NULL)) AS 'day1', 
GROUP_CONCAT(if(DAY(`Date`) = 2, `Status`, NULL)) AS 'day2', 
GROUP_CONCAT(if(DAY(`Date`) = 3, `Status`, NULL)) AS 'day3', 
GROUP_CONCAT(if(DAY(`Date`) = 4, `Status`, NULL)) AS 'day4', 
GROUP_CONCAT(if(DAY(`Date`) = 5, `Status`, NULL)) AS 'day5', 
GROUP_CONCAT(if(DAY(`Date`) = 6, `Status`, NULL)) AS 'day6', 
GROUP_CONCAT(if(DAY(`Date`) = 7, `Status`, NULL)) AS 'day7', 
GROUP_CONCAT(if(DAY(`Date`) = 8, `Status`, NULL)) AS 'day8', 
GROUP_CONCAT(if(DAY(`Date`) = 9, `Status`, NULL)) AS 'day9', 
GROUP_CONCAT(if(DAY(`Date`) = 10, `Status`, NULL)) AS'day10',
GROUP_CONCAT(if(DAY(`Date`) = 11, `Status`, NULL)) AS 'day11', 
GROUP_CONCAT(if(DAY(`Date`) = 12, `Status`, NULL)) AS 'day12', 
GROUP_CONCAT(if(DAY(`Date`) = 13, `Status`, NULL)) AS 'day13', 
GROUP_CONCAT(if(DAY(`Date`) = 14, `Status`, NULL)) AS 'day14', 
GROUP_CONCAT(if(DAY(`Date`) = 15, `Status`, NULL)) AS 'day15', 
GROUP_CONCAT(if(DAY(`Date`) = 16, `Status`, NULL)) AS 'day16', 
GROUP_CONCAT(if(DAY(`Date`) = 17, `Status`, NULL)) AS 'day17', 
GROUP_CONCAT(if(DAY(`Date`) = 18, `Status`, NULL)) AS 'day18', 
GROUP_CONCAT(if(DAY(`Date`) = 19, `Status`, NULL)) AS 'day19', 
GROUP_CONCAT(if(DAY(`Date`) = 20, `Status`, NULL)) AS 'day20', 
GROUP_CONCAT(if(DAY(`Date`) = 21, `Status`, NULL)) AS 'day21', 
GROUP_CONCAT(if(DAY(`Date`) = 22, `Status`, NULL)) AS 'day22', 
GROUP_CONCAT(if(DAY(`Date`) = 23, `Status`, NULL)) AS 'day23', 
GROUP_CONCAT(if(DAY(`Date`) = 24, `Status`, NULL)) AS 'day24', 
GROUP_CONCAT(if(DAY(`Date`) = 25, `Status`, NULL)) AS 'day25', 
GROUP_CONCAT(if(DAY(`Date`) = 26, `Status`, NULL)) AS 'day26', 
GROUP_CONCAT(if(DAY(`Date`) = 27, `Status`, NULL)) AS 'day27', 
GROUP_CONCAT(if(DAY(`Date`) = 28, `Status`, NULL)) AS 'day28', 
GROUP_CONCAT(if(DAY(`Date`) = 29, `Status`, NULL)) AS 'day29', 
GROUP_CONCAT(if(DAY(`Date`) = 30, `Status`, NULL)) AS 'day30',  
GROUP_CONCAT(if(DAY(`Date`) = 31, `Status`, NULL)) AS 'day31', 
COUNT(if(`Status`='Y', `Status`, NULL)) AS 'total' FROM `attendance`
WHERE `Date` BETWEEN '$f' AND '$t' GROUP BY Name_of_Student" ;
$classwise = mysql_query($query_classwise, $dbconnect) or die(mysql_error());
$row_classwise = mysql_fetch_assoc($classwise);
$totalRows_classwise = mysql_num_rows($classwise);
?>

我的查询很长,我不明白我的查询是否正确,我的php知识非常有限,我尝试使用以下php代码输出:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {color: #666600}
.style2 {color: #000000}
-->
</style>
</head>
<body>
<table width="640" border="0" align="center" cellpadding="4" cellspacing="4">
<tr>
<td width="48" bgcolor="#000000"><div align="center">SL No</div></td>
<td width="225" bgcolor="#000000"><div align="center">Name of Student</div></td>
<td width="45" bgcolor="#000000"><div align="center">Roll no</div></td>
<td width="45" bgcolor="#000000">Day 1 </td>
<td width="45" bgcolor="#000000">Day 2</td>
<td width="45" bgcolor="#000000">Day 3 </td>
</tr>
<?php do { ?>
<tr>
<td bgcolor="#333333"><div align="center"><?php echo $count++; ?></div></td>
<td bgcolor="#333333"><?php echo $row_classwise['Name_of_Student']; ?></td>
<td bgcolor="#333333"><div align="center"><?php echo $row_classwise['Roll_no'];
?> </div></td>
<td bgcolor="#333333"><?php echo $row_classwise['day1']; ?></td>
<td bgcolor="#333333"><?php echo $row_classwise['day2']; ?></td>
<td bgcolor="#333333"><?php echo $row_classwise['day3']; ?></td>
</tr>
<?php } while ($row_classwise = mysql_fetch_assoc($classwise)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($classwise);
?>

我想在每一天(第 1 天、第 2 天等)列下获得正确的 Y 或 N。到目前为止它没有输出所需的结果如果需要我可以提供表格...

这是我的新查询,我想将 -/- 和 - 放在星期六和星期日或假日的位置:

SELECT Students.Name_of_Student, Students.Roll_no, Students.Class, Students.Section, allDays.aDay, 
GROUP_CONCAT(IFNULL(attendance.Status, '-') ORDER BY TimePeriod.`time` DESC SEPARATOR '/') AS Status
FROM (SELECT DISTINCT Name_of_Student, Roll_no, Class, Section FROM attendance ) AS Students
CROSS JOIN (SELECT 1 AS aDay UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION 
SELECT 10 AS aDay UNION SELECT 11 UNION SELECT 12 UNION SELECT 13 UNION SELECT 14 UNION SELECT 15 UNION SELECT 16 UNION SELECT 17 UNION SELECT 18 UNION SELECT 19 UNION 
SELECT 20 AS aDay UNION SELECT 21 UNION SELECT 22 UNION SELECT 23 UNION SELECT 24 UNION SELECT 25 UNION SELECT 26 UNION SELECT 27 UNION SELECT 28 UNION SELECT 29 UNION 
SELECT 30 UNION SELECT 31) allDays
CROSS JOIN (SELECT 'Morning' AS `time` UNION SELECT 'Afternoon') TimePeriod
LEFT OUTER JOIN attendance ON Students.Name_of_Student = attendance.Name_of_Student AND allDays.aDay = DAY(`Date`) 
AND BINARY TimePeriod.`time` = BINARY attendance.`time` where Date Between '$f' and '$t' and Students.Section='$s' and Students.Class='$c' 
GROUP BY Students.Name_of_Student, Students.Roll_no, Students.Section, allDays.aDay 
ORDER BY Students.Name_of_Student, allDays.aDay
4

1 回答 1

3

与其试图获得一整条线那样,可能更容易获得每人每天一排。然后在您的 php 代码中,当人员更改时,您会抛出一个新行。

SQL 是这样的:-

SELECT Students.Name_of_Student, Students.Roll_no, Students.Section, allDays.aDay, attendence.Status
FROM (SELECT DISTINCT Name_of_Student FROM attendence WHERE `Date` BETWEEN '$f' AND '$t') AS Students
CROSS JOIN (
SELECT 1 AS aDay UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION 
SELECT 10 AS aDay UNION SELECT 11 UNION SELECT 12 UNION SELECT 13 UNION SELECT 14 UNION SELECT 15 UNION SELECT 16 UNION SELECT 17 UNION SELECT 18 UNION SELECT 19 UNION 
SELECT 20 AS aDay UNION SELECT 21 UNION SELECT 22 UNION SELECT 23 UNION SELECT 24 UNION SELECT 25 UNION SELECT 26 UNION SELECT 27 UNION SELECT 28 UNION SELECT 29 UNION 
SELECT 30 UNION SELECT 31 
) allDays
LEFT OUTER JOIN attendence
ON Students.Name_of_Student = attendence.Name_of_Student AND allDays.aDay = DAY(`Date`)
WHERE `Date` BETWEEN '$f' AND '$t'

这是获取学生列表(如果您有学生表会更容易)和一个月中所有日期的列表。然后加入那些左加入反对出席。

你的 php 将是这样的(未经测试)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {color: #666600}
.style2 {color: #000000}
-->
</style>
</head>
<body>
<table width="640" border="0" align="center" cellpadding="4" cellspacing="4">
<tr>
<td width="48" bgcolor="#000000"><div align="center">SL No</div></td>
<td width="225" bgcolor="#000000"><div align="center">Name of Student</div></td>
<td width="45" bgcolor="#000000"><div align="center">Roll no</div></td>
<td width="45" bgcolor="#000000">Day 1 </td>
<td width="45" bgcolor="#000000">Day 2</td>
<td width="45" bgcolor="#000000">Day 3 </td>
</tr>
<?php
$PrevName_of_Student = '';
while ($row_classwise = mysql_fetch_assoc($classwise))
{ 
    if ($PrevName_of_Student != $row_classwise['Name_of_Student'])
    {
        if ($PrevName_of_Student ! = '')
        {
            echo '</tr>';
        }
        ?><tr>
        <td bgcolor="#333333"><div align="center"><?php echo $count++; ?></div></td>
        <td bgcolor="#333333"><?php echo $row_classwise['Name_of_Student']; ?></td>
        <td bgcolor="#333333"><div align="center"><?php echo $row_classwise['Roll_no'];?></div></td>
        <?php
        $PrevName_of_Student = $row_classwise['Name_of_Student'];
    }
?>
    <td bgcolor="#333333"><?php echo $row_classwise['Status']; ?></td>
    </tr>
<?php 
} 
if ($PrevName_of_Student ! = '')
{
    echo '</tr>';
}
?>
</table>
</body>
</html>
<?php
mysql_free_result($classwise);
?>

把它们放在一起你会得到这样的东西(经过测试和工作)

<?php

$link = mysql_connect('localhost', 'root', '');
If (!$link) 
{
    die ('Could not connect: ' . mysql_error());
}
@mysql_select_db('testarea') or die ('Unable to select database');

$query = "SELECT Students.Name_of_Student, Students.Roll_no, Students.Section, allDays.aDay, attendance.Status, TimePeriod.`time`
FROM (SELECT DISTINCT Name_of_Student, Roll_no, Section FROM attendance ) AS Students
CROSS JOIN (
SELECT 1 AS aDay UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION 
SELECT 10 AS aDay UNION SELECT 11 UNION SELECT 12 UNION SELECT 13 UNION SELECT 14 UNION SELECT 15 UNION SELECT 16 UNION SELECT 17 UNION SELECT 18 UNION SELECT 19 UNION 
SELECT 20 AS aDay UNION SELECT 21 UNION SELECT 22 UNION SELECT 23 UNION SELECT 24 UNION SELECT 25 UNION SELECT 26 UNION SELECT 27 UNION SELECT 28 UNION SELECT 29 UNION 
SELECT 30 UNION SELECT 31 
) allDays
CROSS JOIN (SELECT 'Morning' AS `time` UNION SELECT 'Afternoon') TimePeriod
LEFT OUTER JOIN attendance
ON Students.Name_of_Student = attendance.Name_of_Student AND allDays.aDay = DAY(`Date`) AND BINARY TimePeriod.`time` = BINARY attendance.`time`
ORDER BY Students.Name_of_Student, allDays.aDay, TimePeriod.`time` DESC";

$classwise = mysql_query($query);

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {color: #666600}
.style2 {color: #000000}
-->
</style>
</head>
<body>
<table width="640" border="0" align="center" cellpadding="4" cellspacing="4">
<tr>
<td width="48" ><div align="center">SL No</div></td>
<td width="225" ><div align="center">Name of Student</div></td>
<td width="45" ><div align="center">Roll no</div></td>
<?php
for($iCnt = 1; $iCnt <=31; $iCnt++)
{
    foreach(array("Morning", "Afternoon") AS $TimePeriod)
    {
        echo "<td width='45' >Day $iCnt $TimePeriod</td>";
    }
}
echo "</tr>";
$PrevName_of_Student = '';
$count = 0;
while ($row_classwise = mysql_fetch_assoc($classwise))
{ 
    if ($PrevName_of_Student != $row_classwise['Name_of_Student'])
    {
        if ($PrevName_of_Student != '')
        {
            echo '</tr>';
        }
        echo "<tr>";
        echo "<td ><div align='center'>".$count++."</div></td>";
        echo "<td >".$row_classwise['Name_of_Student']."</td>";
        echo "<td ><div align='center'>".$row_classwise['Roll_no']."</div></td>";
        $PrevName_of_Student = $row_classwise['Name_of_Student'];
    }
    echo "<td >".$row_classwise['Status']."</td>";
} 
if ($PrevName_of_Student != '')
{
    echo '</tr>';
}
mysql_free_result($classwise);
?>
</table>
</body>
</html>

编辑-合并一个人一天的上午和下午记录:-

<?php

$link = mysql_connect('localhost', 'root', '');
If (!$link) 
{
    die ('Could not connect: ' . mysql_error());
}
@mysql_select_db('testarea') or die ('Unable to select database');

$query = "SELECT Students.Name_of_Student, Students.Roll_no, Students.Section, allDays.aDay, GROUP_CONCAT(IFNULL(attendance.Status, '-') ORDER BY TimePeriod.`time` DESC SEPARATOR '/') AS Status
FROM (SELECT DISTINCT Name_of_Student, Roll_no, Section FROM attendance ) AS Students
CROSS JOIN (
SELECT 1 AS aDay UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION 
SELECT 10 AS aDay UNION SELECT 11 UNION SELECT 12 UNION SELECT 13 UNION SELECT 14 UNION SELECT 15 UNION SELECT 16 UNION SELECT 17 UNION SELECT 18 UNION SELECT 19 UNION 
SELECT 20 AS aDay UNION SELECT 21 UNION SELECT 22 UNION SELECT 23 UNION SELECT 24 UNION SELECT 25 UNION SELECT 26 UNION SELECT 27 UNION SELECT 28 UNION SELECT 29 UNION 
SELECT 30 UNION SELECT 31 
) allDays
CROSS JOIN (SELECT 'Morning' AS `time` UNION SELECT 'Afternoon') TimePeriod
LEFT OUTER JOIN attendance
ON Students.Name_of_Student = attendance.Name_of_Student AND allDays.aDay = DAY(`Date`) AND BINARY TimePeriod.`time` = BINARY attendance.`time`
GROUP BY Students.Name_of_Student, Students.Roll_no, Students.Section, allDays.aDay
ORDER BY Students.Name_of_Student, allDays.aDay";

$classwise = mysql_query($query);

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {color: #666600}
.style2 {color: #000000}
-->
</style>
</head>
<body>
<table width="640" border="0" align="center" cellpadding="4" cellspacing="4">
<tr>
<td width="48" ><div align="center">SL No</div></td>
<td width="225" ><div align="center">Name of Student</div></td>
<td width="45" ><div align="center">Roll no</div></td>
<?php
for($iCnt = 1; $iCnt <=31; $iCnt++)
{
    echo "<td width='45' >Day $iCnt </td>";
}
echo "</tr>";
$PrevName_of_Student = '';
$count = 0;
while ($row_classwise = mysql_fetch_assoc($classwise))
{ 
    if ($PrevName_of_Student != $row_classwise['Name_of_Student'])
    {
        if ($PrevName_of_Student != '')
        {
            echo '</tr>';
        }
        echo "<tr>";
        echo "<td ><div align='center'>".$count++."</div></td>";
        echo "<td >".$row_classwise['Name_of_Student']."</td>";
        echo "<td ><div align='center'>".$row_classwise['Roll_no']."</div></td>";
        $PrevName_of_Student = $row_classwise['Name_of_Student'];
    }
    echo "<td >".$row_classwise['Status']."</td>";
} 
if ($PrevName_of_Student != '')
{
    echo '</tr>';
}
mysql_free_result($classwise);
?>
</table>
</body>
</html>

编辑 - 更新查询。

请注意,我已将条款限制在获取学生列表的子选择中。进一步检查我在 LEFT OUTER JOIN 的 ON 子句中输入的日期,以获得实际出勤率。即使学生在该时间段内没有参加任何课程,他们仍然会出现在报告中(每天只有 -/- )。

SELECT Students.Name_of_Student, Students.Roll_no, Students.Class, Students.Section, allDays.aDay, 
GROUP_CONCAT(IFNULL(attendance.Status, '-') ORDER BY TimePeriod.`time` DESC SEPARATOR '/') AS Status
FROM (SELECT DISTINCT Name_of_Student, Roll_no, Class, Section FROM attendance where Section='$s' AND Class='$c' ) AS Students
CROSS JOIN (SELECT 1 AS aDay UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION 
SELECT 10 AS aDay UNION SELECT 11 UNION SELECT 12 UNION SELECT 13 UNION SELECT 14 UNION SELECT 15 UNION SELECT 16 UNION SELECT 17 UNION SELECT 18 UNION SELECT 19 UNION 
SELECT 20 AS aDay UNION SELECT 21 UNION SELECT 22 UNION SELECT 23 UNION SELECT 24 UNION SELECT 25 UNION SELECT 26 UNION SELECT 27 UNION SELECT 28 UNION SELECT 29 UNION 
SELECT 30 UNION SELECT 31) allDays
CROSS JOIN (SELECT 'Morning' AS `time` UNION SELECT 'Afternoon') TimePeriod
LEFT OUTER JOIN attendance ON Students.Name_of_Student = attendance.Name_of_Student AND allDays.aDay = DAY(`Date`) 
AND BINARY TimePeriod.`time` = BINARY attendance.`time` 
AND Date Between '$f' and '$t' 
GROUP BY Students.Name_of_Student, Students.Roll_no, Students.Section, allDays.aDay 
ORDER BY Students.Name_of_Student, allDays.aDay
于 2013-07-04T14:11:03.060 回答