本文介绍了如何显示来自MySQL的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个在线时间表,并且工作正常,用户或我可以通过使用一些数据来添加新的时间表行,最终结果是这样的.

I created an Online Time Table and it's working fine and user or me can add new time-table row by using some data and my end result is like.

这是我的SQL查询(请不要警告我使用MySQLi或PDO,我知道它们,但这是一个私有应用程序,我可以写出不安全的代码.)

This is my SQL query (Please don't warn me to use MySQLi or PDO, I know about them, but this is a private application and I can write unsafe codes thought.)

 $date = date("d/m/y");
    $sql = "SELECT * FROM `s_timetable` WHERE `date` = '$date'";
    $res = mysql_query($sql) or die(mysql_error());

     while ($fetch_data = mysql_fetch_array($res)) {
        echo "<tr>";
        echo "<td>" .$fetch_data['batch_info']. "</td>";
        echo "<td>" .$fetch_data['class_info']. "</td>";
        echo "<td>" .$fetch_data['subject_info']. "</td>";
        echo "<td>" .$fetch_data['teacher_info'].  "</td>";
        echo "</tr>";
     }
   ?>

但是在查询到数据库后,我得到了很多具有相同值的结果,如何将它们显示为组.
例如:
LDC batch-08:09-science-Ashok B.
LDC batch-09-10-English-Jaky
LDC batch-10-11-Maths-Bella

but after querying this to database, I got lots of results with same value, how can i show them as group.
Example:
LDC batch - 08:09 - science - Ashok B.
LDC batch - 09-10 - English - Jaky
LDC batch - 10-11 - Maths - Bella

现在我如何将这些数据显示为表格,标题为LDC batch,然后全部重复,然后显示time subjectteacher信息为表格主体.

Now how i can display these data as table, with title of LDC batch as that repeat in all and then time subject and teacher info with table body.

推荐答案

此处的一个选项是显示3列HTML表格,其中包含每个批次的时间,主题和教师数据.当批处理类型在结果集中更改时,可以将新行打印为标题.

One option here is to present a 3 column HTML table containing the time, subject, and teacher data for each batch. When the batch type changes in the result set, a new row can be printed as a header.

$header = "";
while ($fetch_data = mysql_fetch_array($res)) {
    if ($header = "" || $header != $fetch_data['batch_info']) {
        echo "<tr><td colspan=\"3\">".$fetch_data['batch_info']."</td></tr>";
        $header = $fetch_data['batch_info'];
    }
    echo "<tr>";
    echo "<td>" .$fetch_data['class_info']. "</td>";
    echo "<td>" .$fetch_data['subject_info']. "</td>";
    echo "<td>" .$fetch_data['teacher_info'].  "</td>";
    echo "</tr>";
 }

更新:

正如@apokryfos指出的那样,您应该通过batch_info列对结果集进行排序,以确保属于同一批的记录始终相邻.因此您的查询可能变为:

As @apokryfos pointed out, you should order your result set by the batch_info column to ensure that records belonging to the same batch are always adjacent. So your query might become:

SELECT *
FROM s_timetable
WHERE date = '$date'
ORDER BY batch_info

这篇关于如何显示来自MySQL的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 09:54