Jump to content

repeater if/else – output parts of a repeate


ngrmm
 Share

Recommended Posts

i have a repeater with 2 fields

– image

– text

if there are images uploaded then the output should be only the image field.

if no images uploaded, then only the text-field should be shown

right now, if the image-field is empty and the text-field is filled. i'm getting two boxes. and the first one is empty.

<?php foreach($page->repeater as $box) { 

    if($box->images) { ?>
        <div class="box images">
            <?php foreach($box->images as $image) {
            echo "<img src='$image->url'/>";
            } ?>
            </div>
            <?php }
    else {
    }  
   			
    if($box->text) { ?>
        <div class="box text"><?php echo $box->text; ?></div>
    <?php }
			
} ?>
Link to comment
Share on other sites

You must use the else block.

http://php.net/manual/en/control-structures.elseif.php

<?php 
  foreach($page->repeater as $box) { 
      
      //if there are images uploaded output images
      if($box->images) { 
?>
          <div class="box images">

              <?php 
                foreach($box->images as $image) {
                  echo "<img src='$image->url'/>";
                } 
              ?>

          </div>

<?php 
      }
      else {
      //if not only output text
?>
          <div class="box text">
            <?php echo $box->text; ?>
          </div>
<?php

      }  
       
  } 

?>
Link to comment
Share on other sites

thanks

but it doesn't work

i still get this

<div class="box images" >…</div>
<div class="box images" >…</div>
<div class="box images" >…</div>
<!-- this one below ist the repeater without images -->
<div class="box images" ></div> 
Link to comment
Share on other sites

I like the pure php style to output more, because it's not filled by all those open and close tags. The essential thing is that $page->images is always equal to true, as it's an pageimages object. You need to check for the action number of images:

<?php 
foreach($page->repeater as $box) { 
  //if there are images uploaded output images
  if($box->images->count()) { 
    echo '<div class="box images">';
    echo $box->images->implode("<img src='{url}'/>");
    echo '</div>';

  }else{ // maybe }else if($page->text){
    //if not only output text
    echo '<div class="box text">';
    echo $box->text;
    echo '</div>';
  }     
} 
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...