CodeIgniter Forums
getting information of logged in user - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: getting information of logged in user (/thread-67788.html)



getting information of logged in user - anmol - 04-09-2017

i want to get the information of logged in user from the database and display it in the profile view that i have made for them.for example username email etc.but i am not able to do so.my code is this is my user profile controller caleed main.php
"<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main extends CI_Controller {
public function __construct() {
    parent::__construct();

    // Load form helper library
    $this->load->helper('form');

    // Load session library
    $this->load->library('session');

    // Load database
    $this->load->model('Login_Database');
    }

public function index(){
        
    
    $result['data'] = $this->Login_Database->login($data);
        if ($result != false) {
            $this->load->view('main',$result);

        }else{
            // Sorry No feed to load.
        }
    }
}

public function save(){
$url=$this->do_upload();

$this->Login_Databse->save($url);

}
private function do_upload(){
$type=explode(".",$_FILES["pic"]["name"]);
$type=$type(count($type)-1);
$url="./images/".uniqid(rand()).','.$type;
if(in_array($type,array("jpg","jpeg","gif","png")))
    if(is_uploaded_file($_FILES["pic"]["tmp_name"]))

        if(move_uploaded_file($_FILES["pic"]["tmp_name"],$url))
return $url;
return "";
}


}"



this is my user login model caleed login_database
"<?php

Class Login_Database extends CI_Model {

// Insert registration data in database
public function registration_insert($data) {

// Query to check whether username already exist or not
$condition = "username =" . "'" . $data['username'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 0) {

// Query to insert data in database
$this->db->insert('user_login', $data);
if ($this->db->affected_rows() > 0) {
return true;
}
} else {
return false;
}
}

// Read data using username and password
public function login($data) {

$condition = "username =" . "'" . $data['username'] . "'"." AND " . "password =" . "'" . $data['password'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();

if ($query->num_rows() == 1) {
return true;
} else {
return false;
}
}

// Read all the posts from the database ordered by time - Latest First
public function posts($id)
{
    if ($id ==0) {
        $condition = "post_type =" . 1;
        $limit = 10;
        $anslimit = 1;
    }else{
        $condition = "post_type =" . 1 . " AND id = " . $id ;
        $limit = 1;
        $anslimit = 10;
    }
$this->db->select('*');
$this->db->from('posts');
$this->db->where($condition);
$this->db->order_by('date','desc');
$this->db->limit($limit);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$data = array();
$index = 0;
foreach ($query->result_array() as $row) {
    $info = $row;
    if ($row['ans_count'] != 0) {
        $info['ans'] = $this->answer($row['id'],$anslimit);
        // foreach ($info['ans'] as $ans) {
        //     $ans['upvote'] = $this->findVotes($ans['id']);
        // }
        $info['ans'][0]['upvote'] = $this->findVotes($info['ans'][0]['id']);

    }  
    $data[$index] = $info;
    $index++;
}
return $data;
} else {
return false;
}
}
// Returns the answers if exist
public function answer($id,$limit){
    $condition = "post_type =" . 2 . "  AND parent = " . $id ;
$this->db->select('*');
$this->db->from('posts');
$this->db->where($condition);
$this->db->limit($limit);
$query = $this->db->get();
return $query->result_array();

}
// Read data from database to show data in admin page, $use = check: => To verify username already exists or not
public function read_user_information($username,$use = 'details') {

$condition = "username =" . "'" . $username . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($use == 'check'){
    if($query->num_rows() == 0) {
        return true; //Username Does not exist
    }return false;}
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}

public function ansCount($data)    
{   
    $this->db->set('ans_count', 'ans_count+1', FALSE);
    $this->db->where('id', $data);
    $this->db->update('posts');

}

public function insert($table,$data)
{
    $this->db->insert($table, $data);
    if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
}

public function findVotes($id)
{
    $condition = "postid =" . intval($id);
$this->db->select('Count(id) as total');
$this->db->from('votes');
$this->db->where($condition);
$query = $this->db->get()->result_array();
return $query[0]['total'];
}

public function findComments($value='')
{
    # code...
}
public function save($url)
{
    $this->db->set('image',$url);
    $this->db->insert('user_login');
}

}

?>"
this iss my user profile view
"<!DOCTYPE html>
<html>
<head>
    <title>profile</title>
</head>
<body>


<img src="images/home-user-icon.png" style="width:150px"; height:150px; float:left; border-radius:50% ; margin-right:25px;">
<?php echo form_open_multipart('main/save');?>
<table class="table">

<tr>
<td>image</td>
<td><?php echo form_upload('pic');?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit','save','class="btn btn-primary"');?></td>
</tr>

</body>
</html>


"
if i try using $username varibale in this view it shows undefined error,plz help i am a complete beginner and want to learn.


RE: getting information of logged in user - ciadmin - 04-09-2017

TL;DR see https://forum.codeigniter.com/misc.php?action=help&hid=7


RE: getting information of logged in user - anmol - 04-09-2017

(04-09-2017, 11:10 AM)ciadmin Wrote: TL;DR see https://forum.codeigniter.com/misc.php?action=help&hid=7

what does that mean??


RE: getting information of logged in user - ciadmin - 04-09-2017

(04-09-2017, 11:25 AM)anmol Wrote:
(04-09-2017, 11:10 AM)ciadmin Wrote: TL;DR see https://forum.codeigniter.com/misc.php?action=help&hid=7

what does that mean??

TL;DR = too long, didn't read. Most readers' eyes will gloss over with the length of your post, and they will pass on reading it to the end.
The link I provided explains MyCode - tags you can add to help with post readability, specifically the [code] tag.
The link is from the "help" page, linked to on the otp navbar of the forum.