我正在開發一個簡單的系統,在每個類別中都有無限的類別和項目。例如,可能有類別內的類別(類別1 - >類別2 - >類別3)都包含項目。我想要顯示每個類別及其所有子類別的總項目。我正在試圖找出一個循環來做到這一點,但基本上我會做得很少。我正在建設PHP/MySQL。我的分類表模式是類別(id,id_parent)id是自動增量,id_parent是它是否位於另一個類別(0是默認值)。我的項目架構是項目(id,id_category)。顯然還有其他專欄,但這些是我認爲唯一重要的。有人可以幫助我開發一個循環結構,併爲它們所在類別提供總數量的項目(也計算所有子類別項目)。儘管我覺得這是非常非常錯誤的,但這是我開始的。MySQL計數所有的孩子,無論有多少
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調用)感謝盧卡斯。
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;
}
待辦事項你需要PHP中的響應或者只是一個僞代碼的答案? PHP中的 – Lucas
最好只是爲了確保我完全理解,但如果它是僞代碼,我可能會弄清楚它。不管你有多少不便。 –
母羊全球變種...... – BRampersad