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

从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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度