Is this correct? Or am I overloading the responsibilities of the view?

<table class="table table-striped table-hover">
                 $class = '';
                 $label = '';
                 foreach ($solicitud as $key => $value) {
                     if( $value['priority'] > 0 && $value['priority'] < 4 ) $class = 'badge-important';
                     if( $value['priority'] > 4 && $value['priority'] < 7 ) $class = 'badge-info';
                     if( $value['priority'] > 7 ) $class = 'badge-warning';

                     if( $value['id'] == 1 ) $label = 'label-info';
                     if( $value['id'] == 2 ) $label = 'label-success';
                     if( $value['id'] == 3 ) $label = 'label-danger';

                    echo '<tr>';
                    echo '  <td><span class="label label-sm '.$label.'">'.ucfirst($value['status']).'</span></td>';
                    echo '   <td><span class="badge '.$class.' ">'.$value['priority'].'</span></td>';
                    echo '   <td>'.$value['sname'].' '.$value['last_name'].'</td>';
                    echo '   <td>'.$value['name'].'</td>';
                    echo '   <td>'.$value['message'].'</td>';
                    echo '   <td>'.$value['created'].'</td>';
                    echo '<td>'.(( $value['closed'] )  ? $value['closed'] : 'pendiente').'</td>';
                    echo '</tr>';

                 } ?>

2 Answers 2


These 0, 4 and 7 means nothing to any code reader, nor 1, 2 and 3 for the label.

Move this logic in the controller and consider using ENUMs in your controller class i.e.:

const BADGE_INFO = 4;
const BADGE_WARNING = 7;

This way it will be more readable for the one who will read and change it later, also it will became less hard changable and more reusable.

Also, try to not echo HTML, I mean, use PHP blocks only when you have PHP in it. HTML should be outside:

<?php foreach ($solicitud as $key => $value): ?>
              <td><span class="label label-sm<?=$label;?>"><?=ucfirst($value['status']);?></span></td>
              <td><span class="badge <?=$class;?>"><?=$value['priority'];?></span></td>
<?php endforeach; ?>

In my opinion, there is way too much logic in your view.

Personally I would manipulate the data in the controller so that $value has a label and a class attribute, and then just loop over the data in the view.

This way view and controller are decoupled and, if you think in terms of testability, you can easily unit-test the controller so that the data always has the required fields.

(Actually, I would do the data manipulation in a model.)

Hope this helps.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.