0

I have a user table, where the user is currently logged on and has not upload a photo (that the image column in user table is empty) images shown are Default.png. and if the user has uploaded the picture then the image is displayed.

what wrong in my code?

this is my table 'user' on my database (i'm sorry i can't write database structure in here).

| id | gb_user | = | 1 | me.jpg |

this view (headers.php):

<?php $user_log = info_user_login();?>
                <?php if(!empty($user_log->gb_user) == NULL):?>
                  <img class="img-circle" src="../assets/img/default.png" width="70" style="margin:20px 0 0 10px;">
                 <?php else:?>
                  <img class="img-circle" src="../assets/img/<?php echo $user_log('gb_user');?>" width="70" style="margin:20px 0 0 10px;">
                  <?php endif;?>

this helper:

function info_user_login(){
$CI = & get_instance();
$CI->load->model('admin/setting_adm');
$get_data_login = $CI->setting_adm->get_data_log_user();
return $get_data_login;

}

and this model :

function get_data_log_user(){
    $id_login = $this->session->userdata('id');
    $this->db->where('id', $id_login);
    $this->db->from('user');
    $q = $this->db->get();
    return $q->result();
}

Where ID = session_id_login

  • What is the actual behavior of your code? In which cases it doesn't work: when user **has** photo, or when he **doesn't**, or **in both**? What is output in non-working cases? – Sasha Feb 17 '17 at 10:16

4 Answers4

0
<?php if(!empty($user_log->gb_user) == NULL):?>

This condition does not make sense, empty() will return a boolean.

try one of the following:

<?php if(empty($user_log->gb_user)):?>
<?php if($user_log->gb_user == NULL):?>
kscherrer
  • 5,486
  • 2
  • 19
  • 59
0

Try like this...

Use ternary operator(:?) to check whether you have image or not.

View:

<?php $user_log = info_user_login();
     $image = isset($user_log->gb_user)?"../assets/img/$user_log->gb_user":"../assets/img/default.png";//sets your image
    ?>
      <img class="img-circle" src="<?php echo $image;?>" width="70" style="margin:20px 0 0 10px;">
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
0

Just use empty() function to check image are exist or not. That function will return true if there is no value in gb_user field.

<?php if( empty($user_log->gb_user) ): ?>
    <img class="img-circle" src="../assets/img/default.png" width="70" style="margin:20px 0 0 10px;" />
<?php else: ?>
    <img class="img-circle" src="../assets/img/<?php echo $user_log('gb_user');?>" width="70" style="margin:20px 0 0 10px;" />
<?php endif;?>
Dian
  • 470
  • 1
  • 6
  • 20
0

You can use Js/HTML approach like here HTML if image is not found:

<img id="currentPhoto" src="SomeImage.jpg" onerror="this.src='Default.jpg'" width="100" height="120">

But instead of setting default image at beginning, create another hidden image

<img src="<?=$myImage?>" onerror="getElementById("default-img").src" />

Even if $myImage is null, php renders it as empty string, DOM look on this as error and event "onError" will triggered

Community
  • 1
  • 1
edwardstock
  • 168
  • 2
  • 10