1

I have an array of errors for file upload. It display errors message if the upload went wrong. But if the array is getting no errors, it display "Array" on my view.

How to remove the "Array" if it's empty?

My controller:

if (!is_null($avatar) && !empty($avatar)) {
            if ($_FILES && $_FILES['avatar']['name'] !== "") {
                if (!$this->upload->do_upload('avatar')) {
                    $data['errors'] = array('error' => $this->upload->display_errors());
                } else {
                    $image = $this->upload->data();
                    $avatar = $image['file_name'];
                }
            }
        }
$this->load->view('auth/edit_profile_form', $data);

My view:

<div class="errors">
<?php echo $errors; ?>
</div>
0

4 Answers 4

3

Check the amount of entries on your errors array:

echo (count($errors) > 0 ? implode("<br>", $errors) : '');
Sign up to request clarification or add additional context in comments.

Comments

1

In your view you want to do

<?php if(is_array($errors) && !empty($errors)) { ?>
    <div class="errors">
        <?php foreach($errors as $error) { ?>
            <?php echo $error; ?>
        <?php } ?>
    </div>
<?php } ?>

This checks to see if there is anything in the array and the loop through each item in the array and echo's it

Comments

0

This checks if the $errors variable is set. If $errors['error'] it contains something, the <div class="errors"> will be printed.

<?php
if ($errors['error']) {
     echo '<div class="errors">'.$errors['error'].'</div>';
}
?>

Comments

0

In your view:

<?php if ($errors['error']): >
<div class="errors">
  <?php echo $errors['error']; ?>
</div>
<?php endif; >

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.