does anyone know how to write a recursive function that will check all categories and if a category has no products and all its subcategories are empty, it will be automatically deleted.
no one wanted to help …
but i like to share so here it is …
just save it as whatever you want to call it :
cleanup.php
run it and it will delete all empty category .
i hope someone can answer some of my other questions
```php
ini_set(“memory_limit”, “400M”);
set_time_limit(0);
define(‘AREA’, ‘A’);
define(‘AREA_NAME’ , ‘admin’);
require ‘./prepare.php’;
require ‘./init.php’;
require ‘./functions.php’;
function get_products_count($category_id) {
return db_get_field(“SELECT COUNT(*) FROM ?:products_categories WHERE category_id = ?i”, $category_id);
}
function disable_category($category) {
db_query("UPDATE ?:categories SET status = ‘D’ WHERE category_id = " . $category[category_id]);
echo “Category {$category[category]} disabled
”;
}
function delete_category($category) {
db_query("DELETE FROM ?:categories WHERE category_id = " . $category[category_id]);
echo “Category {$category[category]} deleted
”;
}
function cleanup_categories($category = null, $level = 0)
{
if ($category) {
$count = get_products_count($category[category_id]);
}
$categories = $category ? db_get_array(“SELECT * FROM ?:categories LEFT JOIN ?:category_descriptions ON ?:category_descriptions.category_id = ?:categories.category_id WHERE parent_id = ?i AND ?:categories.status = ‘A’”, $category[category_id]) : db_get_array(“SELECT * FROM ?:categories LEFT JOIN ?:category_descriptions ON ?:category_descriptions.category_id = ?:categories.category_id WHERE ?:categories.status = ‘A’”);
if (is_array($categories)) {
foreach ($categories as $c) {
$count += cleanup_categories($c, $level + 1);
}
if ($category && $count == 0) {
disable_category($category);
delete_category($category);
}
return $count;
} else {
if ($category && $count == 0) {
# disable_category($category);
delete_category($category);
}
return $count;
}
}
cleanup_categories();
?> ```