Welcome Guest, Not a member yet? Register   Sign In
Model Issue
#11

[eluser]JayArias[/eluser]
Code:
<?PHP

class User extends Model{
    
    function User()
    {
        parent::Model();
    }
    
    function info($string)
    {
        $q = $this->db->query("SELECT * FROM `users` WHERE `username` = '".$this->session->userdata('username')."' AND `password` = '".$this->session->userdata('password')."'");
        $member = $q->row();
        return $member->$string;
    }
}

Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: models/user.php

Line Number: 14
#12

[eluser]Zack Kitzmiller[/eluser]
No. That makes perfect sense. Adding unnecessary information to the session is the wrong way to handle things.

CONTROLLER
Code:
....
// Call your Model
$this->load->model('User');
$user = $this->model->info($string);
// Now all of the information is stored in the $user variable

MODEL
Code:
...
    function info($string)
    {
        $user = array();
        $Q = $this->db->query("SELECT * FROM `users` WHERE `username` = '".$this->session->userdata('username')."' AND `password` = '".$this->session->userdata('password')."'");
        if ($Q->num_rows() > 0)
        {
            $user = $Q->row_array();
        }
        $Q->free_result();
        return $user;
    }
...

Obviously you could use row() in stead of row_array, but I prefer using the array notation for some reason. Furthermore, freeing the result in this situation isn't really necessary, but good practice so I always do it.
#13

[eluser]JayArias[/eluser]
so in sence I would just use
Code:
$user['first_name']
?
#14

[eluser]InsiteFX[/eluser]
Or you can return the whole query.

Code:
return $Q->row_array();

Also what jedd told you makes a lot of sence!

Enjoy
InsiteFX
#15

[eluser]JayArias[/eluser]
Wait! , so in template.php since im using template library

I can do the following
Code:
<html>
<head>
<title>#</title>
</head>
<body>
<?=$user['firstname'];?>
</body>
</html>
#16

[eluser]InsiteFX[/eluser]
Try this.

Model:
Code:
function info($string)
    {
        $q = $this->db->query("SELECT * FROM `users` WHERE `username` = '".$this->session->userdata('username')."' AND `password` = '".$this->session->userdata('password')."'");
        $row = $q->row_array();
        return $row;
    }

Controller:
Code:
$data = array();

$data['firstname'] = $this->modelname->row->['firstname']

$this->load->view('yourview', $data);

Something like that, change to fit you app.

Enjoy
InsiteFX
#17

[eluser]JayArias[/eluser]
Not working....

I don't want to keep adding things to the controller Sad

I just want the model to fetch the information for me so if I have the following in my controller.

$user = $this->member->info();

I want to be able to use the following in my view.

echo $user->username;

and have it echo the logged in user's username.
#18

[eluser]cahva[/eluser]
First. In your example you use username and password in the session. Instead, save the user_id in the session. If you have user_id in the session, the code would be something like this:

Model:
Code:
...
    function info()
    {
        if (!$this->session->userdata('logged_in_id'))
        {
            return FALSE;
        }

        $query = $this->db->get_where('users',array('user_id' => $this->session->userdata('logged_in_id')),1);
        if ($query->num_rows() > 0)
        {
            return $query->row();
        }
        else
        {
            return FALSE;
        }
    }

Controller:
Code:
...
        $this->load->model('user');
        $data['user'] = $this->user->info();
        $this->load->view('userinfo',$data);
    }

Now in the view you can use $user->username, $user->email etc.

I left out checks that you ofcourse have to do that user_id is in session etc.
#19

[eluser]JayArias[/eluser]
Code:
$this->template->write('content', "hey", $data);
works well with views but cant get it to work with template library.
#20

[eluser]Colin Williams[/eluser]
Try the write_view() method. You might want to look at the template library docs more closely.




Theme © iAndrew 2016 - Forum software by © MyBB