0

章节表

CHID | Name | Active | Order
 1     Ch2      Y        2
 2     Ch5      N        10
 3     Ch1      Y        1

页表

PID | Name  | Active | CHID | Order
 1    Ch1P1     Y       1       1
 2    Ch1P2     Y       1       2
 3    Ch2P2     Y       3       2
 4    Ch2P1     Y       3       1
 5    Ch5P1     N       2       1

当前结果列出了每一章,但也列出了每一章下的每一页。我将当前脚本的“foreach”部分与一些 Dreamweaver 构建的 PHP 合并。

我在找:

Ch1
    Ch1P1
    Ch1P2
Ch2
    Ch2P1
    Ch2P2

我当前的 php/mysql 脚本。

    $myarray = array();
    mysql_select_db($database, $connection);
    $query_GetModuleChaptersPages = sprintf("SELECT chapters.CHID, chapters.Chapter_Name AS ChapterName, chapters.MID, chapters.`Order` AS ChapterOrder, pages.PID, pages.Page_Name AS PageName, pages.CHID, pages.Is_Active, pages.`Order` AS PageOrder FROM chapters, pages WHERE chapters.MID = 1"));
    $GetModuleChaptersPages = mysql_query($query_GetModuleChaptersPages, $connection) or die(mysql_error());
    $row_GetModuleChaptersPages = mysql_fetch_assoc($GetModuleChaptersPages);
    $totalRows_GetModuleChaptersPages = mysql_num_rows($GetModuleChaptersPages);

    while ($row = mysql_fetch_array($GetModuleChaptersPages, MYSQL_ASSOC)) { $myarray[$row['ChapterName'] ][]  = $row['PageName']; }

    print "<ul>";
    foreach ($myarray as $key => $val) {
        print "<li><h5>";
        print $key;
        print "</h5><ul>";
        for($i=0;$i<count($val);$i++){
            print "<li>";
            print $val[$i];
            print "</li>";
        }
        print "</ul></li>";
    }
    print "</ul>";
4

2 回答 2

0

一种蛮力的方式。

//get all the chapters we want, add criteria as needed
$chapterQuery = "SELECT * FROM `chapters` ORDER BY `order`";
$chapterResult = mysql_query($chapterQuery);
while ($row = mysql_fetch_assoc($chapterResult)) {
    $chapters[] = $row;
}

//get all the pages we want, add criteria as needed
$pagesQuery = "SELECT * FROM `pages` ORDER BY `order`";
$pagesResult = mysql_query($pagesQuery);
while ($row = mysql_fetch_assoc($pagesResult)) {
    $pages[$row['CHID']][$row['order']] = $row;
}

echo "<ul>";
foreach ($chapters as $chapter) {
    echo "<li><h5>" . $chapter['Name'] . "</h5><ul>";
    foreach ($pages[$chapter['CHID']] as $page) {
        echo "<li>".$page['Name']."</li>";
    }
    echo "</ul></li>";
}
echo "</ul>";

这里的关键是将页面添加到页面数组中,使用章节 id 作为第一个键,第二个键在我按顺序放置的页面中,但您可以只使用 [] 而不是 [$row['order']]。

于 2013-01-22T03:18:09.763 回答
0

如果您不介意在输出之前将它们存储(缓存)在数组中:

$data=array();
$result=$mysqli->query("SELECT chapters.chid,chapters.name as cname,pages.pid,pages.name as pname FROM chapters LEFT JOIN pages ON (chapters.chid=pages.chid) WHERE chapters.active='Y' and pages.active='Y' ORDER BY chapters.order ASC,pages.order ASC");
while($row=$result->fetch_assoc())
{
    if(empty($data[$row["chid"]))
        $data[$row["chid"]]=array("name"=>$row["cname"],"pages"=>array());
    $data[$row["chid"]]["pages"][$row["pid"]=$row["pname"];
}
echo "<ul>";
foreach($data as $chid=>$chapter)
{
    echo "<li><h5>".htmlentities($chapter["name"],ENT_COMPAT,"UTF-8")."</h5><ul>";
    foreach($chapter["pages"] as $page)
    {
        echo "<li>".htmlentities($page,ENT_COMPAT,"UTF-8")."</li>";
    }
    echo "</ul></li>";
}
echo "</ul>";

如果要获取它们时输出它们:

$ch=$mysqli->query("SELECT chid,name FROM chapters WHERE active='Y' ORDER BY `order` ASC");
$pg=$mysqli->prepare("SELECT pid,name FROM pages WHERE active='Y' AND chid=? ORDER BY `order` ASC");
$pg->bind_param("i",$chid);
echo "<ul>";
while($row=$ch->fetch_assoc())
{
    echo "<li><h5>".htmlentities($row["name"],ENT_COMPAT,"UTF-8")."</h5><ul>";
    $chid=intval($row["chid"]);
    $pg->execute();
    $pgrst=$pg->get_result();
    while($pages=$pgrst->fetch_assoc())
    {
        echo "<li>".htmlentites($pages["name"],ENT_COMPAT,"UTF-8")."</li>";
    }
    echo "</ul></li>";
}
echo "</ul>";

请注意,这未经测试,因此不要期望它“开箱即用”;但它应该指向一个工作轨道。

于 2013-01-22T04:34:18.313 回答