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

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换