Home > database >  How to display the HTML which is the inside foreach if the array is empty?
How to display the HTML which is the inside foreach if the array is empty?

Time:01-10

I have the below code on my page and it's working when there is any data in the $getbanking variable. But if $getbanking is empty then I am getting the error

Warning : Invalid argument supplied for foreach() in

I have to run the below code if there $getbanking is empty at least once. So that it will display the upload option to the user.

<?php 
  $getbanking = unserialize($info['banking_details']); 

  $i=1;
  foreach ($getbanking as $bankdoc => $b) {   ?>
<div >
  <div >
    <label>Bank <?php   echo $i;   ?></label>
    <div  imgext uploaded "; }   ?>">
      <input type="hidden" name="banking[<?php   echo $bankdoc;   ?>]" value="<?php   echo $b;   ?>">
      <input type="file" name="banking[<?php   echo $bankdoc;   ?>]"  accept="image/png, image/jpeg, application/pdf, .doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
      <div >
        <div ></div>
        <p>Drop your image here, or <span>browse</span></p>
        <span>Supports: JPEG, PNG, DOC, PDF</span>
      </div>
      <div >
        <a href="uploads/<?php   echo $b;   ?>" target="_blank">
          <div ></div>
        </a>

        <p ><?php   echo $b;   ?></p>
      </div>
      <?php   if($i>=2) {   ?>
      <div ><img src="assets/images/x-circle.svg"></div>
      <?php   }   ?>
    </div>
  </div>
</div>
<?php   $i  ; }   ?>

CodePudding user response:

You can do something like ---

<?php 
  $getbanking = unserialize($info['banking_details']); 
if(!empty($getbanking)){
  $i=1;
  foreach ($getbanking as $bankdoc => $b) { ?>
<div >
  <div >
    <label>Bank <?php echo $i;?></label>
    <div  imgext uploaded ";} ?>">
    <input type="hidden" name="banking[<?php echo $bankdoc;?>]" value="<?php echo $b;?>">
      <input type="file" name="banking[<?php echo $bankdoc;?>]"  accept="image/png, image/jpeg, application/pdf, .doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
      <div >
        <div ></div>
        <p>Drop your image here, or <span>browse</span></p>
        <span>Supports: JPEG, PNG, DOC, PDF</span>
      </div>
      <div >
        <a href="uploads/<?php echo $b;  ?>" target="_blank">
          <div ></div>
        </a>

        <p ><?php echo $b;  ?></p>

      </div>
      <?php if($i>=2){?>
      <div ><img src="assets/images/x-circle.svg"></div>
      <?php }?>
    </div>
  </div>
</div>
<?php $i  ; }
}else{
///you can write a div here to show no information or something
} ?>

CodePudding user response:

You could do something like this, so that if $getbanking is empty, we populate it with an empty row, but I have no idea if blank values will work with your upload process

$getbanking = unserialize($info['banking_details']);

if (empty($getbanking)) {
    $getbanking = [0 => ''];
}
  •  Tags:  
  • Related