Usually I end up finding a solution by browsing StackOverflow, but here I come up short!
Here is the structure of my categories in the database
Parent category A (category_id = 1)
Child category B (category_id = 2)
Child category C (category_id = 3)
Child category D (category_id = 4)
Child category E (category_id = 5)
Basically my Joomla extension does not allow you to classify a Page in several categories at the same time.
But I managed to get around this by modifying some php files, and adding several category_id values (separated by commas)
My code that works. But it’s just too long. I would like to simplify it.
I had to create all the scenarios, depending on the position of the numbers.
My code :
// Get all child ids from this category
$categoryModel = ES::model('ClusterCategory');
$childs = $categoryModel->getChildCategories($activeCategory->id, [], SOCIAL_TYPE_PAGE, array('state' => SOCIAL_STATE_PUBLISHED));
$childIds = [];
foreach ($childs as $child) {
$childIds[] = "1,$child->id";
$childIds[] = "2,$child->id";
$childIds[] = "3,$child->id";
$childIds[] = "4,$child->id";
$childIds[] = "5,$child->id";
$childIds[] = "$child->id,1";
$childIds[] = "$child->id,2";
$childIds[] = "$child->id,3";
$childIds[] = "$child->id,4";
$childIds[] = "$child->id,5";
$childIds[] = "1,$child->id,2";
$childIds[] = "2,$child->id,3";
$childIds[] = "3,$child->id,4";
$childIds[] = "4,$child->id,5";
$childIds[] = "5,$child->id,1";
$childIds[] = "2,$child->id,1";
$childIds[] = "3,$child->id,2";
$childIds[] = "4,$child->id,3";
$childIds[] = "5,$child->id,4";
$childIds[] = "1,$child->id,5";
$childIds[] = $child->id;
}
My question :
How to insert a variable instead of the number?
I tried Regexp, and many other things.
As in an equation with an unknown.
$childIds[] = X,$child->id,X;
Thank you,
Philippe
2
Answers
@patrick-simard Thank you very much Patrick. It works wonderfully well ! And if there are 4 values with 3 commas, how can I adapt your code ? I tried that, but it doesn't work.
You should be using nested loops instead
This way, you can easily adjust the range of numbers by changing the loop boundaries, and the code will be more maintainable.