在codeigniter中查看用户数据以及个人资料照片?

时间:2015-08-11 14:05:27

标签: php image codeigniter user-data

我想在其配置文件页面上显示用户数据以及个人资料图片我已成功显示个人资料图片但在我的脑海中有一些疑问我怎么能在同一页面上显示用户数据我应该做其他这个或单个控制器和模型的控制器和模型就足以执行任务。如果我们为此创建另一个模型和控制器,则可以从profile_pic控制器和其他user_data控制器向同一视图返回不同的数据。 / p>

//My profile_pic controller
 public function index() {
            if($this->session->userdata('is_login')) {


        $session_data = $this->session->userdata('sessiondata');
        $id = $session_data['user_id'];
        $this->load->model('Display_profilepicture');
        $data = $this->Display_profilepicture->getImage();
        //var_dump($data);
        print_r($data);
        //echo '<br>';
        $img = base_url().'upload/thumbs/'.$data;
        //echo $img.'<br>';
        $data = array('img' => $img);
        //print_r($data);
        //$this->load->view('header');
        //$this->load->view("my_profile", $data);
    //  $data=array('profile_picture'=>$img);
        //print_r( $data['profile_picture']);
        //echo '<br>';
        //header("Content-type: image/jpg");
        //$this->load->view('header');
        //$this->load->view('my_profile',array('data'=>$data));



        }

//my model 
function get_user_data(){
$session_data = $this->session->userdata('sessiondata');
$id = $session_data['user_id'];
$this->db->select("*");
$this->db->from('tbl_usrs');
$this->db->where('user_id', $id);
$query = $this->db->get();
if($query->num_rows()==0)
echo("Picture not found!");
else
$data = $query->result();
return $data=$query->result();
//print_r($data['profile_picture']);
}
function getImage(){
$session_data = $this->session->userdata('sessiondata');
$id = $session_data['user_id'];
$this->db->select("*");
$this->db->from('tbl_usrs');
$this->db->where('user_id', $id);
$query = $this->db->get();
if($query->num_rows()==0)
echo("Picture not found!");
else
$data = $query->row_array();
return $data['profile_picture'];
//print_r($data['profile_picture']);
} 

这是从model_r($ data);

的模型和结果返回的数据
Array ( [user_id] => 26 [first_name] => aman [last_name] => [username] => aman123 [sex] => [dob] => 0000-00-00 [phone] => 0 [email] => aman@gmail.com [visited_country] => 0 [about_me] => [help_others] => [fav_activity] => [bed_offer] => 0 [couch_country] => 0 [couch_state] => 0 [couch_city] => 0 [couch_addline1] => [couch_addline2] => [profile_picture] => db421e40f1c1852d8cc0acc93d7bb963.jpg [picture1] => [picture2] => [picture3] => [picture4] => [picture5] => [country] => [state] => [city] => [addline1] => [addline2] => [zip] => 0 [created_on] => 0000-00-00 [password] => 123123 [modified_on] => 0000-00-00 [active] => [user_type] => 0 [ip] => ::1 ) 

如果我的模型集中的更改返回$ data而不是$ data [&#39; profile_picture&#39;] 然后返回数据数组,但我得到错误数组到字符串转换,因为控制器中的行#34; $ img = base_url()。&#39; upload / thumbs /&#39;。$ data;&#34;所以我只是想知道什么是执行任务的最佳方式以及如何?一些代码帮助也被接受了?

1 个答案:

答案 0 :(得分:0)

在控制器中

function index()
{
    if ( $this->session->userdata('is_login') )
    {

        $session_data = $this->session->userdata('sessiondata');
        $id = $session_data['user_id'];
        $this->load->model('Display_profilepicture');
        $result = $this->Model_name->get_user_data($id);

        if($result==0)
        {
            echo 'No user Found';
        }
        else
        {
            $data['user']=$result;
            $this->load->view('header');
            $this->load->view("my_profile", $data);
        }
    }
}

在模型中

function get_user_data($id)
{
    $query = $this->db->query("SELECT * FROM tbl_usrs WHERE user_id='$id'");
    $result = $query->result_array();
    $count = count($result);

    if(empty($count) || $count > 1)
    {
        $log = 0;
        return $log ;
    }
    else
    {
        return $result;
    }
}

在视图中

foreach ( $user as $new_user )
{
    ?>
    <h4>Your name: <?php echo $new_user['first_name'] ?> </h4>
    <img src="<?php echo base_url()?>upload/thumbs/<?php echo $new_user['profile_picture'] ?>" alt="<?php echo $new_user['first_name'] ?>">
    <p>E-Mail: <?php echo $new_user['aman@gmail.com'] ?></p>
<?php
}