章节表
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>";
答案 0 :(得分: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作为第一个键将页面添加到pages数组中,将第二个键添加到我按顺序排列的页面中,但你可以使用[]代替[$ row ['order']]
答案 1 :(得分: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>";
请注意,这是未经过测试的,所以不要指望它可以正常使用&#34;开箱即用&#34 ;;但它应该指向你的工作轨道。