在PHP中发布Group by

时间:2015-04-21 09:28:47

标签: php html mysql

解决了问题 谢谢大家。

2 个答案:

答案 0 :(得分:0)

认为您的sql结果如下所示,按原因,日期和时间排序:

reason.reasonName | examDate | examTime | prefix | studentName | studentSurname | studentCode | ProgramName | courseName

有一些记录,例如:

1 | someDate1 | someTime1 | A | James | Jim | 123 | PA | PACourse
1 | someDate1 | someTime1 | A | Tommy | Tim | 222 | PC | PCCourse
1 | someDate2 | someTime2 | B | James | Jim | 123 | PB | PBCourse

2 | someDate1 | someTime1 | C | Jamie | Jam | 444 | PD | PDCourse
2 | someDate1 | someTime1 | E | Willy | Wil | 555 | PE | PECourse
2 | someDate2 | someTime2 | F | Cathy | Cat | 777 | PF | PFCourse
2 | someDate2 | someTime2 | G | David | Dav | 844 | PL | PLCourse
2 | someDate3 | someTime3 | L | James | Jim | 123 | PM | PMCourse

3 | someDate4 | someTime4 | W | Ibear | Ibe | 999 | PX | PXCourse

在这里,你得到这样的数据,你有, 原因1:

  • 在同一天/时间(时间1)参加考试的2名学生
  • 1名学生在另一个时间(时间2)使用axam

原因2:

  • 在同一天/时间(时间1)参加考试的2名学生
  • 在同一天/时间(时间2)参加考试的2名学生
  • 1名学生在另一个时间(时间3)参加考试

原因3:

  • 1名考试时间为4的学生

所以,你要显示:

2个表原因1(time1 / time2)

3个表格原因2(time1 / time2 / time3)

1表格原因3

考虑到这个表,当你从SQL中获取它时,真的像我上面写的一样,你必须对fetch进行检查:

<?php
    //YOU WILL HAVE TO CHECK THESE VARS TO MAKE IT CLEAR, IF SAME TABLE OR NOT
    $i = false;
    //I use $i as a table detector, false at start, once a table has been opened it is turned as true

    $reason = "";
    //I will keep the latest reasonName in this

    $examDate = "";
    //I will keep the latest examDate in this

    $examTime = "";
    //I will keep the latest examTime in this

    $yourDisplay = "";
    //In yourDisplay, i keep the text i have to display (tables, data...)

    $closeTable = "</tbody></table>";
    //This is the code you need to close a table.

    while($rowfet = mysql_fetch_array($myselect)){
        if(($rowfet['ReasonName']!=$reason)||(DateThai($rowfet['EXAMDATE'])!=$examDate)||(MorningEvening($rowfet['EXAMTIME'])!=$examTime)){
            //YOU GET HERE IF YOU HAVE DIFFERENT REASON/DATE/TIME, SO YOU HAVE TO MAKE A NEW TABLE

            $reason = $rowfet['ReasonName'];
            $examDate = DateThai($rowfet['EXAMDATE']);
            $examTime = MorningEvening($rowfet['EXAMTIME']);

            //NOW CHECK IF YOU ALREADY HAVE WRITTEN A TABLE BEFORE
            if($i == true){
                //You already have written a table before
                $yourDisplay.=$closeTable;
            }else{
                //This is the first table you write
                $i=true;
            }
            $yourDisplay.=" <h2>".$reason."</h2>
                            <h3>".DateThai($examDate)." ".MorningEvening($examTime)."</h3>
                            <table width='100%' border='1'>
                            <thead>
                                <tr>
                                    <th>No.</th>
                                    <th>studentid</th>
                                    <th>fullname</th>
                                    <th>Program</th>
                                    <th>Coursename</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>".$rowfet['STUDENTCODE']."</td>
                                    <td>".$rowfet['PREFIXABB']." ".$rowfet['STUDENTNAME']." ".$rowfet['STUDENTSURNAME']."</td>
                                    <td>".$rowfet['PROGRAMNAME']."</td>
                                    <td>".$rowfet['COURSENAMEENG']."</td>
                                </tr>";

        }else{
            //YOU ARE ALREADY IN A TABLE! WITH SAME REASON/TIME/DATE
            //YOUR RECORD JUST NEEDS TO BE ADDED IN A NEW LINE
            $yourDisplay.="<tr>
                            <td>".$rowfet['STUDENTCODE']."</td>
                            <td>".$rowfet['PREFIXABB']." ".$rowfet['STUDENTNAME']." ".$rowfet['STUDENTSURNAME']."</td>
                            <td>".$rowfet['PROGRAMNAME']."</td>
                            <td>".$rowfet['COURSENAMEENG']."</td>
                           </tr>";
        }
    }
    //ONCE OUT, CLOSE THE LAST TABLE AND DISPLAY YOUR DATA
    if($i==1){
        $yourDisplay.=$closeTable;
    }

    echo $yourDisplay;
?>

在我的意见中,如果SQL QUERY返回如上所述的行,这种方法应该可以解决您的问题。 (似乎是这样)

编辑:

你要求&#34;不要显示&#34;如果它是相同的原因名称,那么只要看看你看到的那一行:

$yourDisplay.=" <h2>".$reason."</h2>
                <h3>".DateThai($examDate)." ................ code continue

并将其替换为:

if($rowfet['ReasonName']!=$reason){
    $yourDisplay.="<h2>".$reason."</h2>";
}
$yourDisplay.="<h3>".DateThai($examDate)." ".MorningEvening($examTime)."</h3>
                        <table width='100%' border='1'>
        //.... continue the code

答案 1 :(得分:-1)

您循环遍历所有行并为每个行创建一个表。 您要做的是仅在新ReasonName的序列开始时才创建新表。

$myselect = mysql_query("SELECT reason.ReasonName,tblexam.EXAMDATE,tblexam.EXAMTIME,prefix.PREFIXABB,studentmaster.STUDENTNAME,studentmaster.STUDENTSURNAME,studentmaster.STUDENTCODE,program.PROGRAMNAME,course.COURSENAMEENG FROM reason, formlate, tblexam, studentmaster,prefix, program, course WHERE reason.ReasonID = formlate.ReasonID AND formlate.CLASSID = tblexam.CLASSID AND formlate.COURSEID = formlate.COURSEID AND formlate.ROOMID = formlate.ROOMID AND prefix.PREFIXID = studentmaster.PREFIXID AND formlate.STUDENTID = studentmaster.STUDENTID AND program.PROGRAMID = studentmaster.PROGRAMID AND course.COURSEID = tblexam.COURSEID GROUP BY reason.ReasonID,studentmaster.STUDENTID ORDER BY reason.ReasonID ASC");

<body>
    <?php $i =1;
        while($rowfet = mysql_fetch_array($myselect)){
         ?>
        <h2><?php echo $rowfet['ReasonName']; ?></h2>
        <h3><?php echo DateThai($rowfet['EXAMDATE']) . ' ' . MorningEvening($rowfet['EXAMTIME']); ?></h3>
        <table width="100%" border="1">
        <thead>
            <tr>
            <th>No.</th>
            <th>studentid </th>
            <th>Fullname </th>
            <th>Program  </th>
            <th>Coursename </th>
            </tr>
        </thead>
        <tbody>
            <?php echo "You should create a loop here to loop through your ReasonNames"; ?>
            <tr>
            <td align="center"><?php echo $i; ?></td>
            <td><?php echo $rowfet['STUDENTCODE']; ?></td>
            <td><?php echo $rowfet['PREFIXABB'] . $rowfet['STUDENTNAME'] . ' '.$rowfet['STUDENTSURNAME']; ?></td>
            <td><?php echo $rowfet['PROGRAMNAME']; ?></td>
            <td><?php echo $rowfet['COURSENAMEENG']; ?></td>
            </tr>
            <?php echo "Your loop should end here"; ?>
        </tbody>
        </table>
    <?php $i++; } ?>