1

I'm here trying to make if else condition to hiding the html part if $addons_facts var is empty. I am making the addon visible on the food item if the $addons_facts var is not empty.

 <?php    $addon_data = $this->addons_model->get_addon_by_menu_id($menu_details['id']);
          $addons_facts = json_decode($addon_data->addon_fact, true);
    ?>
        <?php if(!empty($addons_facts) && $addons_facts !=='') : ?>
         <div class="form-group">
               <div class="row">
                    <div class="col-md-12">
                    <?php 
                            foreach ($addons_facts as $key => $addon_fact) : ?>
                              
                                 <div class="row">
                                    <div class="col-md-1">
                                            <div class="checkbox">
                                                <label for="check">
                                                    <input type="checkbox" id="check" />
                                                    <span class="fake-input"></span>
                                                </label>
                                            </div>
                                    </div>
                                    <div class="col-md-7 text-left">
                                       <?php echo sanitize($key); ?>
                                    </div> 
                                    <div class="col-md-3">
                                        <?php echo currency(sanitize($addon_fact));?>
                                    </div>
                                </div>
                                
                            <?php endforeach; ?>
                    </div>
                </div>
            </div>
            <?php endif; ?>

When data is present in $addons_facts

here

When data is absent $addons_facts

here

As you can clearly see that when data is not present and the html is still visible (checkbox or $). I already tried if else both but unable to figure it out what is going wrong and why if condition not hiding the whole content within if{}.

3
  • If you print_r or var_dump $addons_facts when its suppost to be empty, what does it output? Commented Feb 28, 2021 at 16:16
  • Array([]) is the output when its empty Commented Feb 28, 2021 at 16:17
  • Does this answer your question? PHP: if !empty & empty Commented Feb 28, 2021 at 16:41

1 Answer 1

0

I figure it out the solution by this: <?php if(!empty($key)) : ?>

<div class="form-group">
                   <div class="row">
                        <div class="col-md-12">
                        <?php 
                                foreach ($addons_facts as $key => $addon_fact) : ?>
                                  <?php if(!empty($key)) : ?>
                                    <?php $randomNum = substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyzABCDEFGHIJKLMNOPQRSTVWXYZ"), 0, 10); ?>
                                     <div class="row">
                                        <div class="col-1">
                                                <div class="checkbox">
                                                    <label for="<?php echo $randomNum; ?>">
                                                        <input type="checkbox" id="<?php echo $randomNum; ?>" />
                                                        <span class="fake-input"></span>
                                                    </label>
                                                </div>
                                        </div>
                                        <div class="col-7 text-left">
                                           <?php echo sanitize($key); ?>
                                        </div> 
                                        <div class="col-3">
                                            <?php echo sanitize($addon_fact) ? currency(sanitize($addon_fact)) :'';?>
                                        </div>
                                    </div>
                                     <?php endif; ?>
                                <?php endforeach; ?>
                        </div>
                    </div>
                </div>
Sign up to request clarification or add additional context in comments.

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.