我需要创建一个类似于apple.com/sitemap的HTML站点地图,其中包含父类别和子类别。我已经创建了两个表: 主要类别:
ID |标题| URL
子类别:
ID | Parent_Category_ID | TITLE | URL ...
如何检索它以便显示如下内容:
Parent_Category
编辑: 遵循一些在线建议,所以现在有一个表:
ID Parent Name URL
1 0 Parent1 URL1
2 0 Parent2 URL2
3 1 Sub1 URL3
4 0 Parent3 URL4
5 2 Sub2 URL5
以下是使用该代码生成子类别
的类别数组的代码<?php
include('config.php');
echo '<pre>';
$categories = Sitemap::getTopCategories();
print_r($categories);
echo '</pre>';
class Sitemap
{
public static function getTopCategories()
{
return self::getCategories('parent = 0');
}
public static function getCategories($where = '')
{
if ($where) $where = " WHERE $where";
$result = mysql_query("SELECT * FROM testing $where");
$categories = array();
while ($category = mysql_fetch_array($result)){
$my_id = $category['id'];
$category['children'] = Sitemap::getCategories("parent = $my_id");
$categories[] = $category;
}
mysql_free_result($result);
return $categories;
}
}
?>
现在我必须遵循以便以适当的方式显示apple.com/sitemap。我已准备好使用CSS进行正确放置,但无法显示它。
请帮助!!
答案 0 :(得分:0)
这是我用来以正确的格式获取数组的函数..
function outputCategories($categories, $startingLevel = 0)
{
foreach ($categories as $key => $category)
{
if (count($category['children']) > 0)
{
echo '<li><a title="'.$category['name'].'" href="'.$category['url'].'">'.$category['name'].'</a><ul>';
outputCategories($category['children'], $startingLevel+1);
echo "</li>";
}
else
{
echo '<li><a title="'.$category['name'].'" href="'.$category['url'].'">'.$category['name'].'</a></li>';
}
}
echo "</ul>";
return self;
}
有关详细信息,请参阅此帖:Link