我正在开发一个简单的系统,该系统具有无限的类别和每个类别中的项目。例如,类别内可能有类别等(类别 1 -> 类别 2 -> 类别 3)都包含项目。我想显示每个类别及其所有子类别的总项目。我正试图找出一个循环来做到这一点,但基本上是不够的。我在 PHP/MySQL 中构建。我的类别表架构是 category(id, id_parent) id 是自动递增的,id_parent 是它是否位于另一个类别中(默认为 0)。我的项目架构是项目(id,id_category)。显然还有其他专栏,但我相信这些是唯一重要的专栏。有人可以帮我开发一个循环结构,得出他们所在类别的项目总数(也计算所有子类别项目)。
function CountChildDownloads($id_category)
{
global $smcFunc;
$x = array();
$total = 0;
$request = $smcFunc['db_query']('', '
SELECT COUNT(*) AS items FROM {db_prefix}xld_downloads
WHERE id_category = '.$id_category.''
);
$total += $request['items'];
$request = $smcFunc['db_query']('', '
SELECT id FROM {db_prefix}xld_categories
WHERE id_parent = '.$id_category.''
);
if($smcFunc['db_num_rows']($request) > 0) {
while($row = $smcFunc['db_fetch_assoc']($request)) {
$x[] = $row['id'];
}
}
foreach ($x as $id)
{
$y = array();
$z = 0;
$request = $smcFunc['db_query']('', '
SELECT COUNT(*) AS items FROM {db_prefix}xld_downloads
WHERE id_category = '.$id.''
);
$z += $request['items'];
$request = $smcFunc['db_query']('', '
SELECT id FROM {db_prefix}xld_categories
WHERE id_parent = '.$id.''
);
if($smcFunc['db_num_rows']($request) > 0) {
while($row = $smcFunc['db_fetch_assoc']($request)) {
$y[] = $row['id'];
}
}
while (count($y) > 0)
{
$id_y = $y[0];
$request = $smcFunc['db_query']('', '
SELECT id FROM {db_prefix}xld_categories
WHERE id_parent = '.$id_y.''
);
if($smcFunc['db_num_rows']($request) > 0) {
while($row = $smcFunc['db_fetch_assoc']($request)) {
$y[] = $row['id'];
}
}
$request = $smcFunc['db_query']('', '
SELECT COUNT(*) AS items FROM {db_prefix}xld_downloads
WHERE id_category = '.$id_y.''
);
$z += $request['items'];
unset($y[0]);
array_values($y);
}
$total += $z;
}
return $total;
}
$smcFunc 只是进行查询的系统方式。它是一个 MySQL 后端。
工作功能(如果不使用 SMF,则需要更新到标准 MySQL 调用)谢谢 Lucas。
function CountChildDownloads($id_category)
{
global $smcFunc;
$x = array();
$total = array();
$total['downloads'] = 0;
$total['views'] = 0;
$request = $smcFunc['db_query']('', '
SELECT views FROM {db_prefix}xld_downloads
WHERE id_category = '.$id_category.''
);
if($smcFunc['db_num_rows']($request) > 0) {
while($row = $smcFunc['db_fetch_assoc']($request)) {
$total['downloads']++;
$total['views'] += $row['views'];
}
}
$request = $smcFunc['db_query']('', '
SELECT id FROM {db_prefix}xld_categories
WHERE id_parent = '.$id_category.''
);
if($smcFunc['db_num_rows']($request) > 0) {
while($row = $smcFunc['db_fetch_assoc']($request)) {
$x[] = $row['id'];
}
}
foreach ($x as $id)
{
$y = array();
$z = 0;
$w = 0;
$request = $smcFunc['db_query']('', '
SELECT views FROM {db_prefix}xld_downloads
WHERE id_category = '.$id.''
);
if($smcFunc['db_num_rows']($request) > 0) {
while($row = $smcFunc['db_fetch_assoc']($request)) {
$z++;
$w += $row['views'];
}
}
$request = $smcFunc['db_query']('', '
SELECT id FROM {db_prefix}xld_categories
WHERE id_parent = '.$id.''
);
if($smcFunc['db_num_rows']($request) > 0) {
while($row = $smcFunc['db_fetch_assoc']($request)) {
$y[] = $row['id'];
}
}
while (count($y) > 0)
{
$id_y = $y[0];
if (!empty($id_y))
{
$request = $smcFunc['db_query']('', '
SELECT id FROM {db_prefix}xld_categories
WHERE id_parent = {int:id_parent}',
array(
'id_parent' => $id_y,
)
);
if($smcFunc['db_num_rows']($request) > 0) {
while($row = $smcFunc['db_fetch_assoc']($request)) {
$y[] = $row['id'];
}
}
$request = $smcFunc['db_query']('', '
SELECT views FROM {db_prefix}xld_downloads
WHERE id_category = {int:id_category}',
array(
'id_category' => $id_y,
)
);
if($smcFunc['db_num_rows']($request) > 0) {
while($row = $smcFunc['db_fetch_assoc']($request)) {
$z++;
$w += $row['views'];
}
}
}
unset($y[0]);
$y = array_values($y);
}
$total['downloads'] += $z;
$total['views'] += $w;
}
return $total;
}