これを解決しようとして、いくつかの問題に直面しています。勘定科目チャートがあり、お金の列を要約したいと考えています。私のテーブル「カテゴリ」には自動関係があります:
category_table
- id
- parent_id
- category
** アカウント チャートの深さがわかりません。1、2、3、4、5...複数のレベルを持つことができます。
次に例を示します。
私が欲しいもの:
Category Name |$
-------------------------------
Category 1 |160.00
Category 1.1 |130.00
some expense | 50.00
some expense | 80.00
Category 1.2 | 30.00
some expense | 10.00
some expense | 20.00
-------------------------------
Category 2 |160.00
Category 2.1 |150.00
some expense |100.00
some expense |150.00
Category 2.1 | 70.00
some expense | 50.00
some expense | 20.00
-------------------------------
私が持っているもの:
Category Name |$
-------------------------------
Category 1 | 0.00
Category 1.1 | 0.00
some expense | 50.00
some expense | 80.00
Category 1.2 | 0.00
some expense | 10.00
some expense | 20.00
-------------------------------
Category 2 | 0.00
Category 2.1 | 0.00
some expense |100.00
some expense |150.00
Category 2.1 | 0.00
some expense | 50.00
some expense | 20.00
-------------------------------
PHPでこの再帰関数を試しましたが、うまくいきませんでした=/
function display_children($parent) {
$sql = "SELECT c.id, c.category,
(SELECT COUNT(*) FROM category c1 WHERE c1.parent_id = c.id) As Total,
(SELECT SUM(p.value) FROM payables p WHERE p.category_id = c.id) As TotalToPay
FROM category c
WHERE c.parent_id = {$parent}
ORDER BY c.category";
$result = mysql_query($sql);
$ret = "<ul>";
$totalcategory = 0;
while ($row = mysql_fetch_assoc($result)) {
if ($row['Total'] > 0) {
$totalcategory = $totalcategory + (empty($row['TotalToPay']) ? 0 : $row['TotalToPay']);
$ret .= "<li><a href='#" . $row['id'] . "'>" . $row['category'] . " - " . $totalcategory . "</a>";
$ret .= display_children($row['id']);
$ret .= "</li>";
} elseif ($row['Total']==0) {
$totalcategory = ($totalcategory + empty($row['TotalToPay']) ? 0 : $row['TotalToPay']);
$ret .= "<li><a href='" . $row['id'] . "'>" . $row['category'] . " - " . $totalcategory . "</a></li>";
} else;
}
$ret .= "</ul>";
return $ret;
}
echo display_children(0);
それは私に示しています:
Category 1 - 0
Category 1.1 - 0
Category 1.2 - 0
expense 1 - 25.90
expense 2 - 0
expense 3 - 3.80
Category 2 - 0
Category 2.1 - 0
Category 2.2 - 0
Category 2.3 - 0
Category 3 - 0
Category 3.1 - 52.00
...何か案が?...ありがとう :)