douchun5976 2016-02-20 18:43
浏览 27
已采纳

如果用户没有照片codeigniter,则显示默认图像

In displaying my image getting it from the database is fine but I want to display the default image if no user hasn't uploaded a photo yet. I saved a no-avatar.jpg to my /uploads folder



Here is my display user image.

view

<img height="180px" width="180px"class="ppborder" src="<?php echo base_url().'/uploads/'.$this->session->userdata('image'); ?>"> 

upload

public function updatephoto($id)
    {
    if ((int)$id < 1)//$id is not an integer
    {
    redirect('Memberlogincontroller/member_view', 'refresh');
    }
    else{
    $this->load->helper(array('form','file','url'));
    $this->load->library('form_validation');
    $config_image = array(
        'upload_path'   => './uploads/',
        'allowed_types' => 'gif|jpg|png',
        'max_size'      => '1024',
        'overwrite'     => true

    );
    $this->load->library('upload', $config_image);

    if($this->form_validation->run()==false and empty($_FILES['userfile']['name'][0]))
        {
            $id = $this->session->userdata('id');
            $memberinfo = array(
                'error_image' => ''
            );
        $this->session->set_flashdata('flashError', 'Oops no photo selected', $memberinfo);         
        redirect('index.php/Memberlogincontroller/editphoto/'.$id, 'refresh');
        }       
    else
        {           
            $this->upload->do_upload();
             $data = array('upload_data' => $this->upload->data());
             $this->image_resize($data['upload_data']['full_path'], $data['upload_data']['file_name']);
             $id = $this->session->userdata('id');
             $this->db->where('id', $id);
             $query = $this->db->get('member'); 
            $data = array(
                'image' => $data['upload_data']['file_name']    
        );
        $this->db->update('member',$data,array('id'=>$id));          
        $this->session->set_userdata($data); 
        $this->session->set_flashdata('flashSuccess', 'Your photo has been updated.');
        redirect('index.php/Memberlogincontroller/getMember/'.$id, 'refresh');       
    }       
    }
    }

How to display my default image?

  • 写回答

1条回答 默认 最新

  • dragonsun00000 2016-02-20 18:50
    关注

    Just add a condition to check if image is available. If yes show user's image else default image.

    <?php
    $user_img = !empty($this->session->userdata('image')) ? $this->session->userdata('image') : 'no-avatar.jpg';
    ?>
    <img height="180px" width="180px" class="ppborder" src="<?php echo base_url().'/uploads/'.$user_img; ?>"> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C