简体   繁体   中英

How do I hide a section of a collapsible FAQ if the data is empty?

I have a collapsible FAQ section that sits on each members profile page. I'd like to hide the question and answer if the data field is empty.

    <div class="row">
        <div class="col-md-10">
            <div class="panel panel-default1">>
                <div class="panel-heading">
                    <h3 class="panel-title"><b>Who is the registered manager of <?php echo $user['full_name']?>?</b></h3>
                    <span class="pull-right clickable"><i class="glyphicon glyphicon-chevron-up"></i></span>
                </div>
                <div class="panel-body">
                                      <?php echo $user['registered_manager']?> is the registered manager of <?php echo $user['full_name']?>.
                </div>
                 </div>  </div>
                
           <div class="col-md-10">
            <div class="panel panel-default2">
                <div class="panel-heading">
                    <h3 class="panel-title"><b>How many beds are there at <?php echo $user['full_name']?>?</b></h3>
                    <span class="pull-right clickable"><i class="glyphicon glyphicon-chevron-up"></i></span>
                </div>
                <div class="panel-body">
                There are <?php echo $user['number_of_beds']?> beds in total at <?php echo $user['full_name']?>.</div>
                </div> </div>
               
                 <div class="col-md-10">
            <div class="panel panel-default3">
                <div class="panel-heading">
                    <h3 class="panel-title"><b>Who owns <?php echo $user['full_name']?>?</b></h3>
                    <span class="pull-right clickable"><i class="glyphicon glyphicon-chevron-up"></i></span>
                </div>
                <div class="panel-body">
                <?php echo $user['full_name']?> is owned and operated by <?php echo $user['group_name']?>.</div>
                </div> </div>
                   
 
  </div>
  </div>
<script type="text/javascript">
    jQuery(function ($) {
        $('.panel-heading span.clickable').on("click", function (e) {
            if ($(this).hasClass('panel-collapsed')) {
                // expand the panel
                $(this).parents('.panel').find('.panel-body').slideDown();
                $(this).removeClass('panel-collapsed');
                $(this).find('i').removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-up');
            }
            else {
                // collapse the panel
                $(this).parents('.panel').find('.panel-body').slideUp();
                $(this).addClass('panel-collapsed');
                $(this).find('i').removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-down');
            }
        });
    });
</script>
.panel-heading span
    {
        margin-top: -20px;
        font-size: 15px;
    
    }

    .row
    {
        margin-top: 40px;
        padding: 0 20px;
    
    }

    .clickable
    {
        cursor: pointer;
    }

The $user['full_name'] will always be populated so I don't need to worry about that. In question one the field that needs to not be blank is $user['registered_manager']

Question 2 is $user['number_of_beds'] and Question 3 is ```<?php echo $user['group_name']

thanks in advance

You can achieve that by adding CSS class .hidden and checking if $user is empty in PHP

CSS:

.hidden { display: none; }

PHP:

<div class="col-md-10 <?php if( empty($user['registered_manager']) ) echo 'hidden' ?> ">
<div class="panel-heading">
    <h3 class="panel-title">
      <b>Who is the registered manager of <?php echo $user['full_name']?> 
      </b>.
    </h3>
<span class="pull-right clickable"><i class="glyphicon glyphicon-chevron-up"></i></span>
</div>
    <div class="panel-body">
    <?php echo $user['registered_manager']?> is the registered manager of <?php 
    echo $user['full_name']?>.
    </div>
</div>  

You can wrap parts of your html with php if statement which says when to render it. For example this part willl be only rendered when $user['full_name'] is not falsy (empty string, null, zero...).

<?php if ($user['full_name']): ?>
<div class="col-md-10">
...
<?php echo $user['full_name']?>
...
</div>
<?php endif ?>

See more examples here: https://www.w3schools.com/php/php_if_else.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM