So I have a menu that I converted over to a mega menu and it works great, but I wanted to clean the code up a bit and get some input from professionals here.
Here is the PHP
code:
<?php $count = count($sub_items);
$parent_columns = "columns";
$child_column = "column";
switch ($count) {
case 1:
case 2:
case 3:
$parent_columns .= " is-justify-content-center";
$child_column .= " is-one-quarter";
break;
default:
$parent_columns .= " is-multiline";
$child_column = ($count >= 4) ? " column is-one-quarter" : " column";
} ?>
- The variable
$sub_items
returns an array and I grab the count using count()
.
- Then I base the parent and child variables on cases, 1-3 are the same, while 4+ is different classes.
Then I pull them inside my <div>
like this:
<div class="<?= $parent_columns ?>"></div>
<div class="<?= $child_column; ?>"></div>
Does anyone know a better method with my PHP code? Are there other better methods than a switch statement? All help would be appreciated.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…