-3

嗨,大家有一个美好的一天。我有一个关于从 mysql 数据库中回显值的问题现在我有一个表名--->materialachievement 在表内我有 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 章和第 3 章,第 2 章内有第 2.1 小节,第 2.1 小节内有第 2.2 小节,第 2.1 小节有第 1、2、3 部分,第 2.2 小节内有第 1、2 部分

结构如下:

2
 2.1
    1
    2
    3
 2.2
    1
    2

所以我的问题是我怎样才能只重复第 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>";
}

现在的结果假设是:第 2 章、第 2.1 小节、第 2.2 小节、第 2.3 小节和第 3 章、第 3.1 小节、第 3.2 小节

4

2 回答 2

1

希望这有帮助

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

)
于 2013-04-23T02:16:54.830 回答
0
$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>";
}
于 2013-04-23T02:24:12.723 回答