dptrmt4366 2013-04-23 01:58
浏览 95
已采纳

从mysql数据库中回显数据

hi everybody have a good day . i have an question regarding to echo value form mysql database Now i have a table name ---> materialachievement Inside the table i have 3 column (chapter ,subchapter and part)

the data is like below:

chapter      subchapter         part
-------------------------------------
2               2.1               1
2               2.1               2
2               2.1               3
2               2.2               1
2               2.2               2
3               3.1               1
3               3.1               1
-------------------------------------

Now i want do a table to echo chapter 2 and 3 and inside chapter 2 has subchapter 2.1 and subchapter 2.2 inside subchapter 2.1 has part 1 , 2 , 3 and inside subchapter 2.2 has part 1 , 2

the structure is like :

2
 2.1
    1
    2
    3
 2.2
    1
    2

So my question is how can i just echo chapter 2 for once , then subchapter 2.1 for once , then go to part 1 , 2 , 3 ? same with sunchapter 2.2.

$query="SELECT * FROM materialachievement WHERE sID=$id GROUP BY (chapter and subchapter) ORDER BY chapter ASC , subchapter ASC ";

$result=mysql_query($query,$conn);

if($result === FALSE) 
{
die(mysql_error()); // TODO: better error handling
}

while($row=mysql_fetch_array($result))
{
    $chapter = $row['chapter'];
    $subchapter = $row['subchapter'];

    echo "<br>Chapter :".$chapter."<br>";
    echo "<br>Subchapter :".$subchapter."<br>";
}

now the result suppose be : chapter 2 , subchapter 2.1 , subchapter 2.2 , subchapter 2.3 and chapter 3 , subchapter 3.1 , subchapter 3.2

  • 写回答

2条回答 默认 最新

  • dtn36013 2013-04-23 02:24
    关注
    $query="SELECT * FROM materialachievement WHERE sID=$id ORDER BY chapter ASC, subchapter ASC, part ASC ";
    
    $result=mysql_query($query,$conn);
    
    if($result === FALSE) 
    {
    die(mysql_error()); // TODO: better error handling
    }
    
    $last_chapter = null;
    while($row=mysql_fetch_array($result))
    {
        $chapter = $row['chapter'];
        $subchapter = $row['subchapter'];
        $part = $row['part'];
        if ($last_chapter !=== $chapter) {
            echo "Chapter: $chapter<br>";
            $last_chapter = $chapter;
            $last_subchapter = null;
        }
        if ($subchapter !== $last_subchapter) {
            echo "Subchapter: $subchapter<br>";
            $last_subchapter = $subchapter;
        }
        echo "Part: $part<br>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化