5

I am trying to use a foreach statement to display a list ul>li. In case the list has no elements how can i handle that criteria.

    <ul>
    <?php foreach($person['CastsMovie'] as $cast): ?>
      <li><?php echo $this->Html->link($cast['Movie']['name'], array('controller' => 'movies', 'action' => 'view', $cast['movie_id'],  'admin' => true), array('escape' => false)); ?> (<?php echo date("Y", strtotime($cast['Movie']['MovieDetail']['release_date'])); ?>)</li>
      <?php endforeach; ?>
    </ul>

3 Answers 3

6

Check whether it is empty or not:

if ( !empty( $person ) ) {
  foreach ( $person as $cast ) {
    echo "<li>$cast</li>";
  }
} else {
  echo "<li>This play has no cast members.</li>";
}
3
  • 2
    a simple ( ! $person ) should suffice if boolean is expected !
    – Philip
    Apr 29, 2012 at 5:53
  • 1
    @Philip Likely, though I am not 100% certain of how PHP will implicitly handle that so I went with the explicitly clear empty function which is documented to work on empty arrays.
    – Sampson
    Apr 29, 2012 at 5:54
  • simple if($person) it is. PHP will handle empty array all right. the only case for the empty() is when variable is not set, but it is set apparently here. -1 though for switching from clean PHP-based template to echoing HTML, making HTML un-highligtable, unindented and unreadable. Apr 29, 2012 at 10:24
1

Actually, you need to surround the UL with a check. Otherwise you end up with an empty "ul" tag which is unnecessary:

<?php if (!empty($person['CastsMovie'])) { ?>
<ul>
<?php foreach($person['CastsMovie'] as $cast) { ?>
  <li><?php echo $this->Html->link($cast['Movie']['name'], array('controller' => 'movies', 'action' => 'view', $cast['movie_id'],  'admin' => true), array('escape' => false)); ?> (<?php echo date("Y", strtotime($cast['Movie']['MovieDetail']['release_date'])); ?>)</li>
  <?php } ?>
</ul>
<?php } ?>

this means: you check if there are any list elements (in your case CastsMovies to this person) and if so you display the list (ul + li). if not the complete list will be omitted (not just the child li elements).

0

FIRST, in whatever "controller" you are using, prepare your data, to make it fit for the template.

foreach($person['CastsMovie'] as $key => $cast) {
  $cast['html_link'] = $this->Html->link($cast['Movie']['name'], 
                                         array('controller' => 'movies', 
                                               'action' => 'view', 
                                               $cast['movie_id'], 
                                               'admin' => true), 
                                         array('escape' => false)
                               ); 
  $cast['html_date'] = date("Y", strtotime($cast['Movie']['MovieDetail']['release_date']));
  $person['CastsMovie'][$key] = $cast;
}

Then make your template a sane and readable

<?php if($person['CastsMovie']: ?>
<ul>
<?php     foreach($person['CastsMovie'] as $cast): ?>
  <li>
    <?=$cast['html_link']?> (<?=$cast['html_date']?>)
  </li>
<?php     endforeach ?>
</ul>
<?php endif ?>
10
  • is it a good practice to prepare the data in the controller ?
    – Harsha M V
    Apr 29, 2012 at 10:50
  • 1
    The is not just "good". this is just the only acceptable practice, making your templates readable and editable. that is what controllers are for. controller is for the preparing data and view for the displaying it. Look at the resulting template! it is readable at last. Look at the resulting HTML - it is properly indented, letting you to debug whatever HTML errors. Also you have your HTML code readable and editable too Apr 29, 2012 at 10:56
  • what about Skinny Controllers and Fat Models ?
    – Harsha M V
    Apr 29, 2012 at 11:16
  • 1
    Most weird and bushy part of your code is $this->Html->link methos. Which hardly can be moved to model at all. Apr 29, 2012 at 11:20
  • Seriously? I think you may be confusing "preparing data" with "preparing markup output", which is what the V does in MVC. Controller should only handle application logic, and let the view take care of output formatting. And what the heck is $this->Html in a controller anyway? If that's a cake helper, you're doing it wrong. Apr 29, 2012 at 19:51

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.