我无法将 mysql 数据拖出到所需的 HTML 格式中,我将尝试给出一个简化的示例来说明我拥有/需要的内容:
示例表:
CREATE TABLE IF NOT EXISTS `tblcat` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`catname` varchar(100) DEFAULT NULL,
`comments` text NOT NULL,
`enabled` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `id` (`id`)
)
CREATE TABLE IF NOT EXISTS `tblsubcat` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`subcatname` varchar(100) DEFAULT NULL,
`catid` int(10) DEFAULT NULL,
`enabled` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `id` (`id`)
)
示例数据:
INSERT INTO `tblcat` (`id`, `catname`, `comments`, `enabled`) VALUES
(100, 'Bells & Horns', '', 0),
(101, 'Bottles & Bottle Cages', '', 0),
(216, 'Brake Blocks - Shimano', '', 0),
(243, 'Brake Discs & Accessories', '', 0)
INSERT INTO `tblsubcat` (`id`, `subcatname`, `catid`, `enabled`) VALUES
(194, 'Bottom Bracket Axles', 102, 0),
(204, 'Bottom Bracket Parts', 102, 0)
目标 HTML:
<li><a href="viewproducts.php?task=showsubs&catID=100">Bells & Horns</a></li>
<li><a href="viewproducts.php?task=showsubs&catID=101">Bottles & Bottle Cages</a></li>
<li>
<a href="viewproducts.php?task=refine&catID=102" class="extends">Bottom Brackets & Accessories</a>
<ul class="subcategory">
<li><a href="viewproducts.php?task=refine&catID=102&subcatID=194">Bottom Bracket Axles</a></li>
<li><a href="viewproducts.php?task=refine&catID=102&subcatID=204">Bottom Bracket Parts</a></li>
</ul>
</li>
<li><a href="viewproducts.php?task=showsubs&catID=216">Brake Blocks - Shimano</a></li>
<li><a href="viewproducts.php?task=showsubs&catID=243">Brake Discs & Accessories</a></li>
到目前为止我的尝试:
$CurrentCatID = 0;
$Categories = mysql_query("SELECT tblcat.id as CatID, tblcat.catname as CatName, IFNULL(tblsubcat.id, -1) as SubCatID, IFNULL(tblsubcat.subcatname, '') as SubCatName FROM tblcat LEFT OUTER JOIN tblsubcat ON tblcat.id = tblsubcat.catid WHERE tblcat.enabled = '0' ORDER BY tblcat.catname ASC, tblsubcat.subcatname ASC");
$CurrentCatID = mysql_result($Categories, 0, "CatID");
while ($row = mysql_fetch_assoc($Categories)) {
echo "<li><a href=\"viewproducts.php?task=showsubs&catID=" . $row["CatID"] . "\">" . htmlspecialchars($row["CatName"]) . "</a></li>";
}
如何扩展上述 PHP 以创建上述目标 HTML?我已经尝试了各种令人费解的向后和向前循环的方法,这些方法很丑陋,我敢肯定一定有一种更清洁的方法-所以我想在这里问:)
编辑:
我的最终工作版本:
$InSub = false;
$PreviousCatID = "";
$Categories = mysql_query("SELECT tblcat.id as CatID, tblcat.catname as CatName, IFNULL(tblsubcat.id, -1) as SubCatID, IFNULL(tblsubcat.subcatname, '') as SubCatName FROM tblcat LEFT OUTER JOIN tblsubcat ON tblcat.id = tblsubcat.catid WHERE tblcat.enabled = '0' ORDER BY tblcat.catname ASC, tblsubcat.subcatname ASC");
while ($row = mysql_fetch_assoc($Categories)) {
if ($InSub === true && ($row["SubCatID"] === "-1" || $PreviousCatID !== $row["CatID"])) {
echo " </ul>\n";
echo "</li>\n";
$InSub = false;
}
if ($row["SubCatID"] === "-1") {
echo "<li><a href=\"viewproducts.php?task=showsubs&catID=" . $row["CatID"] . "\">" . htmlspecialchars($row["CatName"]) . "</a></li>\n";
} else {
if ($PreviousCatID !== $row["CatID"] || $InSub === false) {
echo "<li><a href=\"viewproducts.php?task=showsubs&catID=" . $row["CatID"] . "\" class=\"extends\">" . htmlspecialchars($row["CatName"]) . "</a>\n";
echo " <ul class=\"subcategory\">\n";
$InSub = true;
}
echo " <li><a href=\"viewproducts.php?task=showsubs&catID=" . $row["CatID"] . "&subcatID=" . $row["SubCatID"] . "\">" . htmlspecialchars($row["SubCatName"]) . "</a></li>\n";
}
$PreviousCatID = $row["CatID"];
}