从mysql数据库中回显数据

时间:2013-04-23 01:58:54

标签: php mysql

你好,每个人都有美好的一天。 我对mysql数据库的echo值有疑问 现在我有一个表名---> 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.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部分?与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>";
}

现在结果假设为:第2章,第2.1章,第2.2章,第2.3节 和第3章,第3.1章,第3.2章

2 个答案:

答案 0 :(得分: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
        )

)

答案 1 :(得分: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>";
}