Enable category tree with limits to prevent 500 errors: max depth 5, max 20 items per category
This commit is contained in:
parent
bbc57a9d53
commit
67c402a37a
@ -54,7 +54,7 @@ try {
|
||||
$items = App\Models\Item::getAll($db);
|
||||
|
||||
function buildTree($categories, $items, $parentId = null, &$visited = [], $depth = 0) {
|
||||
if ($depth > 10) return []; // Prevent deep recursion
|
||||
if ($depth > 5) return []; // Limit depth
|
||||
$tree = [];
|
||||
foreach ($categories as $cat) {
|
||||
if ($cat['parent_id'] == $parentId && !in_array($cat['id'], $visited)) {
|
||||
@ -65,9 +65,12 @@ try {
|
||||
'children' => buildTree($categories, $items, $cat['id'], $visited, $depth + 1),
|
||||
'items' => []
|
||||
];
|
||||
// Limit items to 20 per category
|
||||
$itemCount = 0;
|
||||
foreach ($items as $item) {
|
||||
if ($item['category_id'] == $cat['id']) {
|
||||
if ($item['category_id'] == $cat['id'] && $itemCount < 20) {
|
||||
$node['items'][] = $item;
|
||||
$itemCount++;
|
||||
}
|
||||
}
|
||||
$tree[] = $node;
|
||||
@ -79,7 +82,7 @@ try {
|
||||
$categoryTree = buildTree($categories, $items);
|
||||
|
||||
function renderTree($nodes, $depth = 0) {
|
||||
if ($depth > 10) return '';
|
||||
if ($depth > 5) return '';
|
||||
$html = '';
|
||||
foreach ($nodes as $node) {
|
||||
$html .= '<li>';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user