Welcome Guest, Not a member yet? Register   Sign In
What is the best way for data in user section
#1

[eluser]Michal1[/eluser]
Hello guys,

I am thinking about what is the best way for showing data in user section. Lets say I have a login form and when an user logins he is redirect to membership area. And then I want to display only data which are assigned to certain user.

So for example I have a table data and in this table I have fields like "id" "article name", "article_content" etc. And every logged in user has chance to add an article into this table. And when he is logged in I want to display him only articles which he added by myself. ( I hope this make sense).

So what would I do is that I would create a new field in my data table called for example "user_id" and then when user ads an article to data table I would insert his id into user_id in that table.

So for example I will have user called "Michal" with id "10"

So when he adds and article I would insert that value 10 into user_id in data table.

And then when displaying articles in membership area I would probaby need to a lot of if statemens, something like:

if user_id in table data is equal to id in users table then display data.

But this sounds kinda complicated and after a while it will produces a huge code. So is there any better and simple way how to handle this?

Thank you very much
#2

[eluser]steelaz[/eluser]
Everything sounds correct up to the last part, you only need to add <i>where</i> statement to your database query. Your code in the model would look something like this:

Code:
public function get_articles_by_user($user_id)
{
    $this->db->where('user_id', $user_id);
    $query = $this->db->get('articles');
    
    return $query->result();
}

And in controller, assuming you have logged in user's id in the session:

Code:
$articles = $this->articles_model->get_articles_by_user($this->session->userdata('user_id'));
#3

[eluser]Michal1[/eluser]
Thank you for your help.

I tried to do code that and still cannot get it work as I do not know how to do some things. Do you think you could help me?

This is me "site controller" which controlls displaying articles. Inside of it I have:

Code:
class Site extends Controller {
    
    function index()
    
    {
        
        $data=array();
        if ($query=$this->site_model->get_data()
        
        {
            $data['records']=$query;
            
        }
$this->load->view('site_view', $data);

}

Then I have "site" model which has function get_data

Code:
function get_data()
        {
    
    $this->db->order_by("id","desc");
    $query=$this->db->get('data');
    
    return $query->result();
}

So this all takes and display every article from database.


Then I have a controller "login" and inside of that is

Code:
class Login extends Controller {
    
    function index()
    
    {
        
        
        
        $this->load->view('login_view');   //this loads just a view with forms for username, password and submit button
    }
    
    function validate()
    
    {
        if ($query=$this->login_model->validate_login())
        
        {
        $data = array (
            
            'logged_in'=> true
            
                )    ;
                
            $this->session->set_userdata($data);    
            redirect('site');   //if succes redirect to site controller
        }
        
        else
            {
                $this->index();  // if not reload login form
            }
        
        }
        
        
    
    
    
}

And then the last one is login_model

Code:
function validate_login()
    
    {
    
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', $this->input->post('password'));
        $query= $this->db->get('users');
        
        if ($query->num_rows==1)
        
        {
            
            return true;
        }
        
        
        
    }

And now I want to do that when certain user is logged in I will store his uniquate field from its database called "id_users"

and then I would just probably need to reedit my function in site_model

from
Code:
function get_data()
        {
    
    $this->db->order_by("id","desc");
    $query=$this->db->get('data');
    
    return $query->result();
}

to something different, probably using where. So something like

Code:
$this->db->where('user_id, $id_users);
$this->db->order_by("id","desc");
    $query=$this->db->get('data');

So the main problem for is how to insert certain user "id" which is field in database to $id_users and then use it in site_model for this $this->db->where('user_id, $id_users);

Thank you very much and I hope this makes sense
#4

[eluser]steelaz[/eluser]
You can change your validate_login() function to:

Code:
function validate_login()
{
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', $this->input->post('password'));
    $query= $this->db->get('users');
        
    if ($query->num_rows==1)
    {
        return true;
    }
    else
    {
        $query->row();
    }
}

and in login controller:

Code:
if ($query = $this->login_model->validate_login())
{
    $data['logged_in'] = TRUE;
    $data['user_id'] = $query->id; // assuming your users table primary index is "id"

    $this->session->set_userdata($data);    
    redirect('site');   //if succes redirect to site controller
}
else
{
    $this->index();  // if not reload login form
}

Then your site model get_data() function can be modified to filter by user when user is set:

Code:
function get_data($user_id = NULL)
{
    if (!is_null())
    {
        $this->db->where('user_id', $id_users);
    }

    $this->db->order_by('id', 'desc');
    $query = $this->db->get('data');
    
    return $query->result();
}

And in your site controller:

Code:
function index()
{
    $data['records'] = $this->site_model->get_data($this->session->user_data('user_id'));
    
    $this->load->view('site_view', $data);
}
#5

[eluser]InsiteFX[/eluser]
Also if your using Codeigniter 2.0.0 then you need CI_Controller and CI_Model
not Controller and Model.

Code:
class Site extends CI_Controller {
}
// instead of this:
class Site extends Controller {
}

class Model_name extends CI_Model {
}
// instead of this:
class Model_name extends Model {
}

InsiteFX
#6

[eluser]Michal1[/eluser]
Hey, thanks for the help. I reedit your code little bit as there were some mistakes and it almost works.

The only problem is with:

Code:
function validate()
    
    {
        if ($query=$this->login_model->validate_login())
        
        {
        $data = array (
            
            'logged_in'=> true,
                'user_ids'=>$query->id
            
                )    ;
                
            $this->session->set_userdata($data);    
            redirect('site');
        }


in user_ids part where I am inserting $query->id (which as you said should insert a id value from database. But it does not insert it Sad and I dont know why.

If I insert some value manually for example user_ids'=>'2' then the whole works perfectly then. So there must be some probably with $query->id
#7

[eluser]steelaz[/eluser]
In your validate() function try

Code:
$query = $this->login_model->validate_login();

echo "<pre>"; var_dump($query); echo "</pre>"; exit;

and see what you get.
#8

[eluser]Michal1[/eluser]
I get: bool(true)
#9

[eluser]steelaz[/eluser]
Ah, my bad, you need to update your validate_login() function:

Code:
function validate_login()
{
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', $this->input->post('password'));
    $query= $this->db->get('users');
        
    if ($query->num_rows==1)
    {
        $query->row();
    }
    else
    {
        return false;
    }
}
#10

[eluser]Michal1[/eluser]
[quote author="steelaz" date="1299508596"]Ah, my bad, you need to update your validate_login() function:

Code:
function validate_login()
{
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', $this->input->post('password'));
    $query= $this->db->get('users');
        
    if ($query->num_rows==1)
    {
        $query->row();
    }
    else
    {
        return false;
    }
}
[/quote]

Yeah it finally works. Thank you so much. I just need to add return true; after $query->row() because otherwise validation would not work correct




Theme © iAndrew 2016 - Forum software by © MyBB