Jump to content

if empty field in repeater?


Roych
 Share

Recommended Posts

Hello,

I need some help as I'm not a coder.  I've created a slider from repeater which is working great. I need to make some slides with a link so I have created a "Link" field (also working) but now all my slides have links. The one with the new link is linking right but all others link to root. Basicaly what I need is if field is empty show this else this. ..;)

I've tried with this:

<?php if($page->Slides->Link): ?>

<?php foreach($page->Slides as $Slides): ?>

       <?php 
       // get the very first image from images list since image field is accepting multiple images (unless you change the value of `Maximum files allowed` under Fields > images > Details tab > Maximum files allowed).      
      $image = $Slides->images->first(); 
       ?>
		<li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" data-link="<?=$Slides->Link ?>" data-target="_blank" >
			<img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>">
			<div class="tp-caption"  data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div>
		</li>

<?php endforeach; ?>									
<?php else: ?>

<?php foreach($page->Slides as $Slides): ?>
       <?php 
       // get the very first image from images list since image field is accepting multiple images (unless you change the value of `Maximum files allowed` under Fields > images > Details tab > Maximum files allowed).      
      $image = $Slides->images->first(); 
       ?>
		<li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" >
			<img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>">
			<div class="tp-caption"  data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div>
		</li>

<?php endforeach; ?>
<?php endif; ?>

Not really working. 

Any help is appreciated. ;)

R

Link to comment
Share on other sites

Hi, not tested, but the issue with your code is that you check for empty link before the for loop and not inside of it, so something like this should do:

<?php foreach($page->Slides as $Slides): ?>
    <?php 
        $image = $Slides->images->first();
        if ($Slides->Link) {
            $link = 'data-link="' . $Slides->Link .'"';
        } else {
            $link = "";
        }
    ?>
    <li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" <?= $link ?> data-target="_blank" >
        <img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>">
        <div class="tp-caption"  data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div>
    </li>
<?php endforeach; ?>

BTW, templates and fields should be named lowercase in the admin, so that you can use lowercase identifiers for your attributes/properties in your code which is a convention for such entities.

Edited by szabesz
typo
  • Like 1
Link to comment
Share on other sites

Oh, sorry for the early morning typo, I left <?php endif; ?> in the example, but <?php endforeach; ?> is needed instead, of course. I'm not good at writing code in the browser, I'm missing my IDE's help ;) 

  • Like 1
Link to comment
Share on other sites

Yes now it is working great. ^-^

I got it working with my code also "I think". I used my version of code and put if-else inside like you've said and somehow works. :D

<?php foreach($page->Slides as $Slides): ?>
       <?php 
       $image = $Slides->images->first(); 
       ?>
<?php if ($Slides->Link): ?>
		<li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" data-link="<?=$Slides->Link ?>" data-target="_blank" >
			<img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>">
			<div class="tp-caption"  data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div>
		</li>
								
<?php else: ?>

       <?php    
       $image = $Slides->images->first(); 
       ?>
		<li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" data-target="_blank" >
			<img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>">
			<div class="tp-caption"  data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div>
		</li>

<?php endif; ?>
<?php endforeach; ?>

I will use your code ofc, shorter and probably better.

Thank you very much

R

  • Like 1
Link to comment
Share on other sites

<?php foreach($page->Slides as $Slides) 
    $image = $Slides->images->first(); 
	if ($Slides->Link) { ?>
		<li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" data-link="<?=$Slides->Link ?>" data-target="_blank" >
			<img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>">
			<div class="tp-caption"  data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div>
		</li>								
	<?php } 
	else { ?> 
		<li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" data-target="_blank" >
			<img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>">
			<div class="tp-caption"  data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div>
		</li>
	<? };
}?>

Something like this might make it shorter. Please excuse me if I forgot to close a { or else as the browser is not my best editor. Basically I removed $image defininition in your second case as it is already defined before the if and is not changing.

 

  • Like 1
Link to comment
Share on other sites

If you want even shorter version of your code, you can remove completely the repeating lines and just leave the differences as @szabesz suggested earlier:

<?php 
foreach($page->Slides as $Slides) {
$image = $Slides->images->first(); 
  if ($Slides->Link) { 
  $link = $slides->Link;
  }
else { 
    $link = ""; 
};
echo "<li data-transition='$page->Slide_Effect' data-slotamount='7' data-link='$link' data-target='_blank' >";		
echo "<img src='$image->url' alt='$image->description'>";
echo "<div class='tp-caption' data-x='100' data-y='115' data-speed='700' data-start='1000' data-easing='easeOutBack'><h2>$Slides->title</h2></div>";
echo "</li>";        
} ?>

Hope that helps ;)

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...