Codeigniter在标题上显示图像

时间:2018-06-24 14:08:22

标签: php codeigniter session login codeigniter-3

我使用了会话数据。但是我想做sessiondata = DB上的东西。例如

<a data-toggle="dropdown" class="dropdown-toggle" href="#">
                    <img alt="" src="<?php
                    if(($this->session->userdata('people_id')) == 'cosId') { echo base_url().'upload/customer/'.$this->session->userdata('people_img');} else{ echo base_url().'upload/user/'.$this->session->userdata('people_img');} ?>">
                    <span class="username"><?php echo $this->session->userdata('people_name'); ?>  <?php echo $this->session->userdata('people_surname'); ?></span>
                    <b class="caret"></b>
                </a>

我该如何(($this->session->userdata('people_id')) == 'cosId')来显示有关会话数据的其他目录中的任何图像?

当客户登录可以看到自己的照片时,或者当用户登录可以看到来自其他图像目录/根目录的自己的照片时。

我的带有用户数据的控制器如下:

$username = $this->input->post('username');
            $password = $this->input->post('password');
            $user_data = $this->db->get_where('user', array('userUsername' =>$username,'userPass' => $password));
            $cus_data = $this->db->get_where('customer', array('cosUserName' =>$username,'cosPassword' => $password));
            if ($user_data->num_rows() > 0) {
                foreach ($user_data->result_array() as $row) {
                    $this->session->set_userdata('people_id',$row['userId']);
                    $this->session->set_userdata('people_name',$row['userName']);
                    $this->session->set_userdata('people_surname',$row['userSurname']);
                    $this->session->set_userdata('people_usertype',$row['userType']);
                    $this->session->set_userdata('people_username',$row['userUsername']);
                    $this->session->set_userdata('people_img',$row['userImg']);
                    $this->session->set_userdata('people_email',$row['userEmail']);
                    $this->session->set_userdata('people_status',$row['userStatus']);
                    $this->session->set_userdata('people_cdate',$row['userDate']);
                    $this->session->set_userdata('people_department',$row['userDepartment']);
                    $this->session->set_userdata('people_lang',$row['userLang']);
                    $this->session->set_userdata("logged_in", true);
                }
                // redirect to dashboard
                redirect("dashboard");
            }
            elseif ( $cus_data->num_rows() > 0) {
                foreach ($cus_data->result_array() as $row) {
                    $this->session->set_userdata('people_id',$row['cusId']);
                    $this->session->set_userdata('people_name',$row['cosName']);
                    $this->session->set_userdata('people_surname',$row['cosSurname']);
                    $this->session->set_userdata('people_username',$row['cosUserName']);
                    $this->session->set_userdata('people_img',$row['cosImg']);
                    $this->session->set_userdata('people_cname',$row['cosCName']);
                    $this->session->set_userdata('people_country',$row['cosCountry']);
                    $this->session->set_userdata('people_email',$row['cosEmail']);
                    $this->session->set_userdata('people_status',$row['cosStatus']);
                    $this->session->set_userdata('people_lang',$row['cosLang']);
                    $this->session->set_userdata("logged_in", true);

我可以为客户编辑吗?

<?php $cus_data = $this->db->get_where('customer');
                    foreach ($cus_data->result_array() as $row) {

                    if($this->session->set_userdata('people_id',$row['cusId']))
                    {
                        $src = base_url('upload/customer/'.$this->session->userdata('people_img'));
                    }
                    else
                    {
                        $src = base_url('upload/user/'.$this->session->userdata('people_img'));
                    }}
                    ?>

当前登录方法:

public function index(){
        $this->load->library('form_validation');
        $this->form_validation->set_rules("username", "Username", "trim|required");
        $this->form_validation->set_rules("password", "Password", "trim|required");
        if ($this->form_validation->run() == FALSE)
        {
            $this->session->set_flashdata("error", validation_errors());
            $this->load->view("login");
        }
        else{
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            $user_data = $this->db->get_where('user', array('userUsername' =>$username,'userPass' => $password));
            $cus_data = $this->db->get_where('customer', array('cosUserName' =>$username,'cosPassword' => $password));
            if ($user_data->num_rows() > 0) {
                foreach ($user_data->result_array() as $row) {
                    $this->session->set_userdata('people_id',$row['userId']);
                    $this->session->set_userdata('people_name',$row['userName']);
                    $this->session->set_userdata('people_surname',$row['userSurname']);
                    $this->session->set_userdata('people_usertype',$row['userType']);
                    $this->session->set_userdata('people_username',$row['userUsername']);
                    $this->session->set_userdata('people_img',$row['userImg']);
                    $this->session->set_userdata('people_email',$row['userEmail']);
                    $this->session->set_userdata('people_status',$row['userStatus']);
                    $this->session->set_userdata('people_cdate',$row['userDate']);
                    $this->session->set_userdata('people_department',$row['userDepartment']);
                    $this->session->set_userdata('people_lang',$row['userLang']);
                    $this->session->set_userdata("logged_in", true);
                }
                // redirect to dashboard
                redirect("dashboard");
            }
            elseif ( $cus_data->num_rows() > 0) {
                foreach ($cus_data->result_array() as $row) {
                    $this->session->set_userdata('people_id',$row['cusId']);
                    $this->session->set_userdata('people_name',$row['cosName']);
                    $this->session->set_userdata('people_surname',$row['cosSurname']);
                    $this->session->set_userdata('people_username',$row['cosUserName']);
                    $this->session->set_userdata('people_img',$row['cosImg']);
                    $this->session->set_userdata('people_cname',$row['cosCName']);
                    $this->session->set_userdata('people_country',$row['cosCountry']);
                    $this->session->set_userdata('people_email',$row['cosEmail']);
                    $this->session->set_userdata('people_status',$row['cosStatus']);
                    $this->session->set_userdata('people_lang',$row['cosLang']);
                    $this->session->set_userdata("logged_in", true);
                }
                // redirect to dashboard
                redirect("dashboard");
            }
            else{
                $this->session->set_flashdata("error", "Username or Password is invalid");
                $this->load->view("login");
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

执行以下操作:

正确的方法: 在您的登录方法中,还要为图像session设置一个url

对于客户:

$cust_src = base_url('upload/customer/'.$row['cusImg']);
$this->session->set_userdata('image_src',$cust_src);

对于用户:

$user_src = base_url('upload/customer/'.$row['userImg']);
$this->session->set_userdata('image_src',$user_src);

您的主播img应该是这样的:

<a data-toggle="dropdown" class="dropdown-toggle" href="#">
     <img alt="" src="<?=$this->session->userdata('image_src');;?>">
     <span class="username"><?php echo $this->session->userdata('people_name'); ?>  <?php echo $this->session->userdata('people_surname'); ?></span>
     <b class="caret"></b>
</a>

第二种方法是这样的:

<a data-toggle="dropdown" class="dropdown-toggle" href="#">
    <?php if($this->session->userdata('people_id') == 'cosId') 
    {
        $src = base_url('upload/customer/'.$this->session->userdata('people_img'));
    }
    else
    {
        $src = base_url('upload/user/'.$this->session->userdata('people_img'));
    }
    ?>

    <img alt="" src="<?php echo $src;?>" >
    <span class="username"><?php echo $this->session->userdata('people_name'); ?>  <?php echo $this->session->userdata('people_surname'); ?>

    </span>
    <b class="caret"></b>
</a>