Answer the question
In order to leave comments, you need to log in
How to simplify and reduce php code in opencart?
In the list of products in the admin panel, there is a task to display custom fields under the name of the product with the corresponding class.
Only 8 fields (number may vary). Known data is entered into these 8 fields, but their order is unpredictable. Input data must be received, changed and displayed.
Conditions (if elseif) seem to be silly to use (the code turns out to be too cumbersome), but I don’t know another option, because. i'm newbie.
How can this be done correctly? Maybe in the controller it would be correct to do this ...
<!-- Поле 1-->
<?php if ($product['entry_1'] == "Коля") {?>
<span class="book"><?php echo $product['entry_1'];?></span>
<?php } elseif ($product['entry_1'] == "Стас") {?>
<span class="velosiped"><?php echo $product['entry_1'];?></span>
<?php } elseif ($product['entry_1'] == "Лена") {?>
<span class="kamen"><?php echo $product['entry_1'];?></span>
<?php} elseif ($product['entry_1'] == "Фома") {?>
<span class="armeniya"><?php echo $product['entry_1'];?></span>
<?php } elseif ($product['entry_1'] == "Олег") {?>
<span class="buldog"><?php echo $product['entry_1'];?></span>
<?php } elseif ($product['entry_1'] == "Саша") {?>
<span class="kirpich"><?php echo $product['entry_1'];?></span>
<?php } else {?>
<span class="default"><?php echo $product['entry_1'];?></span>
<?php }?>
<!-- Поле 2 -->
<?php if ($product['entry_2'] == "Коля") {?>
<span class="book"><?php echo $product['entry_2'];?></span>
<?php } elseif ($product['entry_2'] == "Стас") {?>
<span class="velosiped"><?php echo $product['entry_2'];?></span>
<?php } elseif ($product['entry_2'] == "Лена") {?>
<span class="kamen"><?php echo $product['entry_2'];?></span>
<?php} elseif ($product['entry_2'] == "Фома") {?>
<span class="armeniya"><?php echo $product['entry_2'];?></span>
<?php } elseif ($product['entry_2'] == "Олег") {?>
<span class="buldog"><?php echo $product['entry_2'];?></span>
<?php } elseif ($product['entry_2'] == "Саша") {?>
<span class="kirpich"><?php echo $product['entry_2'];?></span>
<?php } else {?>
<span class="default"><?php echo $product['entry_2'];?></span>
<?php }?>
Answer the question
In order to leave comments, you need to log in
<?
$class = ['Коля'=>"book",'Стас'=>"velosiped",'Лена'=>"kamen",...];
?>
<span class="<?=$calass[$product['entry_1']];?>"><?=$product['entry_1'];?></span>
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question