treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] If a div has no content - add class for display:none ?

  • I'm trying to come up with a solution, where if a div is empty, I would like to add a class so that I can display:none;. I tried searching without any luck.

    I don't know how to write this expression though? Any help would be appreciated :)



    <div class="<?php echo '$hiddenclass'; ?>">
    <?php
    echo $errors['amount'];
    echo $errors['weight'];
    echo $errors['date'];
    echo $errors['time'];
    ?>
    </div>


  • I don't really know what you're doing... but what if it was more like this:
    <?php if ( empty($errors) ) {
    // do nothing
    } else { ?>
    <div class.....
  • I have a div that contains all my errors on a form. I have it styled and positioned where needed, but when there are no errors, I would like the div to not be visible.

    I'm going to see if I can work with the idea above, thanks.
  • Well that's not going over too well lol. I tried writing that function ( I always get mixed up about the names of php [statements/expressions/functions]) outside the div, inside the class="" itself and lots of different ways, I could never get it to work. I think I was doing it wrong, but not sure?
  • I'm not the strongest with PHP, so my solution might not be EXACTLY what you need to write.
  • Thanks for trying @TheDoc.
  • Even with my limited knowledge, there has to be something we can do! What code did you try?
  • Sweet, I asked a buddy and he helped out. I was thinking of it the same way and trying like you said, if empty do not display vice if not empty display.



    <?php
    if(!empty($errors)){
    echo '<div id="error-container">';
    echo $errors['example'];
    echo '</div>';
    }
    ?>

  • wait, if you're using php why have an empty div in the first place? use php to output the div only when there is an error
  • I think that's what he finished with there.
  • yup, that'll teach me not to refresh the page after having a cup of coffee...