0

How do I check if only 1 individual variable of the array is empty? I would need an element to display or not, depending on if there is content in the variable. Problem is, if I add more than one variable, it hides/shows the element for all, instead of individually. Any help appreciated.

What works:

PHP:

<?php
$texta = CFS()->get( 'sometexta' );

if ($texta !='') {
  $display = 'block';
} else {
  $display = 'none';
}
?>

HTML:

<div class="element" style="display:<?php echo $display; ?>">
   <p><?php echo $texta; ?></p>
</div>
....

What I would like to work:

PHP:

<?php
$texta = CFS()->get( 'sometexta' );
$textb = CFS()->get( 'sometextb' );
$textc = CFS()->get( 'sometextc' );

$alltext = array($texta, $textb, $textc);

foreach ( $alltext as $text) {
  if ($text !='') {
    $display = 'block';
  } else {
    $display = 'none';
  }
}
?>

HTML:

<div class="element" style="display:<?php echo $display; ?>">
   <p><?php echo $texta; ?></p>
</div>
<div class="element" style="display:<?php echo $display; ?>">
   <p><?php echo $textb; ?></p>
</div>
<div class="element" style="display:<?php echo $display; ?>">
   <p><?php echo $textc; ?></p>
</div>
....
2
  • (1) In your example (3 DIVs ), all of them are using the SAME variable $display to determine the display style (block or none) (2). What do you expect the $display variable value be after the end of the foreach loop ? [Hint: will it be determined by the last one of the iteration ?) Commented Dec 23, 2021 at 12:50
  • Yes, that's the point....Hence, why its going through a loop. I know there's a specific way to do it, without writing different variables for each element. What do I need to do to the loop or the array, so they share the same variable but behave differently? Commented Dec 23, 2021 at 12:57

2 Answers 2

2

Something like this:

PHP

<?php
  $texta = CFS()->get( 'sometexta' );
  $textb = CFS()->get( 'sometextb' );
  $textc = CFS()->get( 'sometextc' );

  $alltext = array($texta, $textb, $textc);

  $display = [];
  foreach($alltext as $text){
    if ($text !='') {
      $display[] = 'block';
    } else {
      $display[] = 'none';
    }
  }
?>

HTML

<div class="element" style="display:<?php echo $display[0]; ?>">
   <p><?php echo $texta; ?></p>
</div>
<div class="element" style="display:<?php echo $display[1]; ?>">
   <p><?php echo $textb; ?></p>
</div>
<div class="element" style="display:<?php echo $display[2]; ?>">
   <p><?php echo $textc; ?></p>
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

Personally I will try for something like this :

<div class="element" style="display:<?php ($texta!="") ? echo "block" : echo "none"; ?>">
   <p><?php echo $texta; ?></p>
</div>
<div class="element" style="display:<?php ($textb!="") ? echo "block" : echo "none"; ?>">
   <p><?php echo $textb; ?></p>
</div>
<div class="element" style="display:<?php ($textc!="") ? echo "block" : echo "none"; ?>">
   <p><?php echo $textc; ?></p>
</div>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.