Welcome Guest, Not a member yet? Register   Sign In
After login load own user profile
#1

[eluser]Zonyk[/eluser]
hey guys,
im new at ci and i want to build a simple cms.
my login is just working and redirects to the user's dashboard. there the user have an anchor which he can load his user profile.

now i think i have to do this with a session but i dont know how.

thank you for your help Smile
#2

[eluser]mi6crazyheart[/eluser]
Just after login validation & before redirecting to "Dashboard" create all u'r session info. After this, create a logout page for destroy all the session data which u've created at login time.

Now, if u want to make any VIEW as private, just place a session check at the respective CONTROLLER. At here, u check user's session data for user has logged in or not. If user has already logged in allow him to see the VIEW page else user has not logged in then redirect him to the Sign Up page.

Now, if any user has already logged in & want to logout move him to the "logout" controller. At there Logout controller delete all his session data & redirect him to the login page.

Hope this logic will help...
#3

[eluser]Zonyk[/eluser]
Yeahr thank you Smile

Now i have another problem.
The session is working and i can get the user info with a model from the database.
How i can take the query out of my model in my view?

my controller:
Code:
<?php

class Site extends Controller {
    
    function index()
    {
        $data ['main_content'] = 'logout_successful';
        $this->load->view('includes/template', $data);
    }
    
    function __construct()
    {
        parent::Controller();
        $this->is_logged_in();
    }
    
    function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');
    
            if(!isset($is_logged_in) || $is_logged_in != true)
            {                
                print '[removed]';
                    print 'alert("Bitte logg dich ein!")';  
                    print '[removed]';
                echo '[removed][removed] = "/login"[removed]';                  
            }
    }
    
    function home()
    {
        $data ['main_content'] = 'home_view';
        $this->load->view('includes/template', $data);
    }
    
    function pcard()
    {
        $this->load->model('pcard_model');
        $data ['email_address'] = $this->pcard_model->get();    
         $data ['main_content'] = 'pcard_view';
        $this->load->view('includes/template', $data);
     }

    function logout()  
    {    
            $this->session->sess_destroy();
            redirect('main/logged_out', 'Logout');
    }  
}

/* End of file site.php */
/* Location: ./system/application/controllers/site.php */

my model:
Code:
class pcard_model extends Model {

    function get()
    {
        $email_address = $this->session->userdata('email_address');
           $sql = "SELECT * FROM membership WHERE email_address = ?";
        $query = $this->db->query($sql, $email_address);
        
    if ($query->num_rows() > 0)
    {
           $row = $query->row_array();

           echo $row['email_address'];                 <-----------
           echo $row['first_name'];
           echo $row['last_name'];
    }
    }
}

and my view is empty... but there i want to show the user info.

Thank you so much Tongue
#4

[eluser]mi6crazyheart[/eluser]
Dear, your MODEL function is perfect. You just need to add a return statement in it.
Ex:
Code:
function get()
    {
        $email_address = $this->session->userdata('email_address');
        $sql = "SELECT * FROM membership WHERE email_address = ?";
        $query = $this->db->query($sql, $email_address);
        return $query->result();
}
#5

[eluser]Zonyk[/eluser]
Ah, thank you again.
This is really my last question. After I added the return statement and put in my view:
Code:
&lt;?php
    echo "Your Email Adress: $email_address";
?&gt;

i get "Your Email Adress: Array" instead of the email address.
what am I doing wrong?

Smile
#6

[eluser]mi6crazyheart[/eluser]
Here is some clean way to do the stuff which u want. I've just written in my way. Just modify it by using the parameters which u've. Hope now u'll get the perfect idea...

CONTROLLER Code:
Code:
$data['userInfo'] = $this->pcard_model->getUserInfo($emailID);

Example of MODEL:
Code:
function getUserInfo($emailID)
{
    $this->db->select('FirstName,LastName, EmailID'); // DB table fields.
    $this->db->where('EmailID', $emailID); // $emailID is the value which u've already get & now compare to table field "EmailID"
    $query = $this->db->get('user'); // "user" is table name
    return $query->result();
}


VIEW code:
Code:
foreach($userInfo as $item):
echo $item->FirstName ; // Show FirstName
echo $item->LastName ; // Show LastName
echo $item->EmailID ; // Show EmailID
endforeach;
#7

[eluser]Zonyk[/eluser]
Okay, it's working, thank you for here Smile but now i get this error:
______________________________________________
A PHP Error was encountered
Severity: Notice

Message: Undefined variable: email_address

Filename: controllers/site.php

Line Number: 41
_______________________________________________

and i've this in my CONTROLLER SITE:

Code:
function pcard()
    {
        $this->load->model('pcard_model');
        $email_address = $this->pcard_model->getUserInfo($email_address);
        $data ['userInfo'] = $this->pcard_model->getUserInfo($email_address);    
         $data ['main_content'] = 'pcard_view';
        $this->load->view('includes/template', $data);
     }
#8

[eluser]Zonyk[/eluser]
Hey dear,
i get it. The variable $email_address have to be empty.

Thank for your help Smile

Close

Code:
function pcard()
    {
        $this->load->model('pcard_model');
        $email_address = "";
        $data ['userInfo'] = $this->pcard_model->getUserInfo($email_address);    
         $data ['main_content'] = 'pcard_view';
        $this->load->view('includes/template', $data);
     }
#9

[eluser]mi6crazyheart[/eluser]
Oh Buddy, u r not getting the point actually...

Look , what u'd written in MODEL function get() previously
Code:
function get()
    {
        $email_address = $this->session->userdata('email_address');
        $sql = "SELECT * FROM membership WHERE email_address = ?";
        $query = $this->db->query($sql, $email_address);
        return $query->result();
}

According to above code(2nd line) u'r trying to access all the data from table where email_address field of any record may be equal to any specific email id.

By keeping this thing in mind i'd showed u that CONTROLLER function example..
Code:
$data['userInfo'] = $this->pcard_model->getUserInfo($emailID);

What at here i'm trying to do in above line is, just sending that require mail id to MODEL function which needed to be compare with DB table emailID field through SQL query. So, now it's u'r duty to get that emailID any way(GET/POST/whatever) & assign that to the $emailID variable. SO that u'r MODEL can get that required emailID for comparison.

But, just make blank the value of mailID id by $emailID = ""; will not solve u'r problem. Because, according to u'r model function's SQL query it'll search for those record in DB table which has no value in their emailID field.

Hope, now u got the point...
#10

[eluser]sehummel[/eluser]
[quote author="Zonyk" date="1292474065"]Hey dear,
i get it. The variable $email_address have to be empty.

Thank for your help Smile

Close

Code:
function pcard()
    {
        $this->load->model('pcard_model');
        $email_address = "";
        $data ['userInfo'] = $this->pcard_model->getUserInfo($email_address);    
         $data ['main_content'] = 'pcard_view';
        $this->load->view('includes/template', $data);
     }
[/quote]

How do I load pcard_view if it is in a subfolder in my views folder?




Theme © iAndrew 2016 - Forum software by © MyBB