Php 从mysql数据库回显数据

Php 从mysql数据库回显数据,php,mysql,Php,Mysql,大家好,祝大家今天愉快。 我对mysql数据库中的echo值有一个问题 现在我有了一个表名-->materialachieve 在表中,我有3列(章节、分章和部分) 数据如下: chapter subchapter part ------------------------------------- 2 2.1 1 2 2.1 2 2

大家好,祝大家今天愉快。 我对mysql数据库中的echo值有一个问题 现在我有了一个表名-->
materialachieve
在表中,我有3列(章节、分章和部分)

数据如下:

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
-------------------------------------
现在我想做一个表格来回应第二章和第三章 第2章内有第2.1分章和第2.2分章 第2.1分章内部包含第1、2、3部分,第2.2分章内部包含第1、2部分

结构如下:

2
 2.1
    1
    2
    3
 2.2
    1
    2
所以我的问题是,我怎样才能重复第二章一次,然后重复第2.1分章一次,然后转到第1、2、3部分?与第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>";
}
$query=“从MaterialAchievent中选择*,其中sID=$id GROUP BY(章和分章)顺序BY章ASC,分章ASC”;
$result=mysql\u查询($query,$conn);
如果($result==FALSE)
{
die(mysql_error());//TODO:更好的错误处理
}
while($row=mysql\u fetch\u数组($result))
{
$chapter=$row['chapter'];
$subchapter=$row['subchapter'];
回声“
章:“.$Chapter.”
”; 回声“
分章:“.$Subchapter.”
”; }
现在假设结果是:第2章,第2.1分章,第2.2分章,第2.3分章 第三章,第3.1分章,第3.2分章希望能有所帮助

$data=array();
$data[] = array("chapter" => "2",                "subchapter" => "2.1",               "part" => "1"              );
$data[] = array("chapter" => "2",                "subchapter" => "2.1",               "part" => "2"              );
$data[] = array("chapter" => "2",                "subchapter" => "2.1",               "part" => "3"              );

$data[] = array("chapter" => "2",                "subchapter" => "2.2",               "part" => "1"              );
$data[] = array("chapter" => "2",                "subchapter" => "2.2",               "part" => "2"              );

$data[] = array("chapter" => "3",                "subchapter" => "3.1",               "part" => "1"              );
$data[] = array("chapter" => "3",                "subchapter" => "3.1",               "part" => "1"              );

$new_array=array();

foreach($data AS $v){

    if(!array_key_exists($v['chapter'],$new_array )){
        $new_array[$v['chapter']]=array();
    }
     if(!array_key_exists($v['subchapter'],$new_array[$v['chapter']]) ){
        $new_array[$v['chapter']][$v['subchapter']]=array();
    }
    $new_array[$v['chapter']][$v['subchapter']]=$v['part'];

}

print_r($new_array);
输出

    Array
(
    [2] => Array
        (
            [2.1] => 3
            [2.2] => 2
        )

    [3] => Array
        (
            [3.1] => 1
        )

)
$query=“从MaterialAchievent中选择*,其中sID=$id按章节ASC、分章ASC、部分ASC排序”;
$result=mysql\u查询($query,$conn);
如果($result==FALSE)
{
die(mysql_error());//TODO:更好的错误处理
}
$last_chapter=null;
while($row=mysql\u fetch\u数组($result))
{
$chapter=$row['chapter'];
$subchapter=$row['subchapter'];
$part=$row['part'];
如果($last_chapter!==$chapter){
回声“章:$Chapter
”; $last_chapter=$chapter; $last_分章=空; } 如果($subchapter!==$last_subchapter){ echo“分章:$Subchapter
”; $last_分章=$subchapter; } 回显“部分:$Part
”; }
展示您的尝试,我们将帮助您修复。基本上,您只需将最后一章和子章保存在一个变量中,并检查新行是否不同。$query=“SELECT*FROM materialachievement,其中sID=$id GROUP BY(chapter and subchapter)ORDER BY chapter ASC,subchapter ASC”;$result=mysql\u查询($query,$conn);如果($result==FALSE){die(mysql_error());//TODO:better error handling}而($row=mysql_fetch_array($result)){$chapter=$row['chapter'];$subchapter=$row['subchapter'];echo“
chapter:.$chapter.”
chapter:$subchapter.“
”;}把它放在你的问题中,这样它的格式就正确了。但是这段代码只回应第2章和第2.1章,假设有第2章,第2.1、2.2、2.3和第3章,第3.1、3.2分章,我看不懂你的代码。编辑您的问题,使其格式正确。谢谢您的帮助,但我的问题是mysql数据库中的数据不是静态的。例如:现在我的数据库只有第2章和第3章,然后是第2章和第3章。。。。。但以后用户可以添加更多章节,如第4章、第5章。。。。所以…$data只是示例,sql查询到数组,然后从它到2D数组,然后使用…所以第4章,第5章。。。。。。我可以再次感谢你的帮助,但我想我已经从Barmar那里得到了答案,无论如何,再次感谢你的帮助。非常感谢你,专业人士,你真的很专业,你能帮助我解决这个问题,我已经坚持了很多天,再次感谢你的帮助,我想我找到了正确的渠道来问我的问题,谢谢Barmer。要用多少天来解决这个小问题?仅仅设置一个变量来查看是否已经打印了一些内容有多难?呵呵,因为我还是php、mysql的新手,而且我的basic还不够强大,所以上次我又遇到了一个问题,这个问题已经解决了好几周了,真让人难过。。。但幸运的是,你能在一秒钟内帮我解决问题,再次感谢
$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>";
}