Welcome Guest, Not a member yet? Register   Sign In
pass the id
#1

[eluser]Mainboard[/eluser]
hey everyone, really i need a little help xD, how i can to pass the $id in my foreach model to the array in my controll, this is my code
my model
Code:
function validate()
    {
    $this->default_db = $this->load->database('default', true);    
        
     $query = $this->default_db->select('*')->from('tb_membership')
    ->where('username', $this->input->post('username'))
    ->where('password', sha1($this->input->post('password')));
     $query = $this->default_db->get();
    
        //return $query;
        
        if($query->num_rows() == 1)
        {
           foreach ($query->result() as $row)
           {
              $id = $row->id;
           }
         return true;  
        }  

    }
my controll
Code:
function validate_credentials()
    {        
        $this->load->model('membership_model');
        $query = $this->membership_model->validate();
              
        if($query) // si las credenciales del usuario son validas...
        {    
  
            $data = array(
                'username' => $this->input->post('username'),
                //----> $id
               'is_logged_in' => true
                );
            //return $query->row('id');  
            $this->session->set_userdata($data);
            redirect('site/members_area');
          
        
        }
        
        else // incorrect username or password
        {
            $this->index();
        }
        
    }
i just only need to catch the $id in the session but first i need to put the id in the array, im new in CI, so y need a little help with this xD
#2

[eluser]skunkbad[/eluser]
Use return $row->id instead of return true;

You'd do something like this:

Code:
if( $query->num_rows() > 0 )
{
     $row = $query->row();

     return $row->id;
}

return FALSE;
#3

[eluser]Mainboard[/eluser]
thanks for your fast reply, but how i can to pass the id into the array in the control?
#4

[eluser]atw[/eluser]
Hi Mainboard,

What skunkbad is saying is that your current model is currently just returning TRUE if it gets anything from the database.

He is suggesting that you should change your code to:
Code:
function validate()
{
    $this->default_db = $this->load->database('default', true);

     $query = $this->default_db->select('*')->from('tb_membership')
    ->where('username', $this->input->post('username'))
    ->where('password', sha1($this->input->post('password')));
     $query = $this->default_db->get();

    //return $query;

    if($query->num_rows() > 0)
    {
        $row = $query->row();

        return $row->id;
    }

    return FALSE;
}
In your controller, you should then find that $query contains the ID (only the ID not an array of data) you want.

Hope this helps.
#5

[eluser]Mainboard[/eluser]
thanks, I resolve the problem, I had a little confused at the controll but i fixed, thanks




Theme © iAndrew 2016 - Forum software by © MyBB