Welcome Guest, Not a member yet? Register   Sign In
Quick Question
#1

[eluser]Wondering Coder[/eluser]
Code:
if($query)
        {
            $data = array(
                'id' => $this->input->post('id'),//returns me null value or blank
                'username' => $this->input->post('username'),
                'classification' => $this->input->post('classification'),
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
            
                if($data['classification'] == 'company')
                    {                    
                        redirect('com_site');
                        
                    }
                else
                    {
                        redirect('stud_site');
                    }
        }
        else
        {
            $this->index();
        }

My problem is i'm trying to get the primary key or id in my table but don't have any luck. I know im missing something here and I know that this is very simple. Can anyone show me the light here.

When echoed it, this is the result:
Code:
Array
( [mod] => com_site
[classification] => company
[username] => admin
[id] => )
#2

[eluser]Cristian Gilè[/eluser]
We need the query and the view code.


Cristian Gilè
#3

[eluser]Kindari[/eluser]
What is the form you are submitting? Check the genereated form (view source) and make sure id is defined.
#4

[eluser]Wondering Coder[/eluser]
this is my controller function
Code:
function validate_credentials()
    {        
        $data['username'] = $this->input->post('username');
        $data['password'] = md5($this->input->post('password'));
        $data['classification'] = $this->input->post('classification');
        //echo $data['classification'];
        $query = $this->membership_model->validate($data);
        //print_r($data);
    
        if($query) // if the user's credentials validated...
        {
            $data = array(
                'id' => $this->input->post('id'),// return id => blank
                'username' => $this->input->post('username'),
                'classification' => $this->input->post('classification'),//used in views
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
            //$this->output->enable_profiler(TRUE);
                if($data['classification'] == 'company')
                    {                    
                        redirect('com_site');
                        
                    }
                else
                    {
                        redirect('stud_site');
                    }
        }
        else // incorrect username or password
        {
            $this->index();
        }
    }
my com_site controller
Code:
function my_profile() // just for sample
    {
        $data['mod'] = strtolower(get_class($this));
        $data['classification'] = $this->session->userdata('classification');
        $data['content'] = $this->load->view('panes/profile',$data, true);
        
        $this->load->vars($data);
        $this->load->view('template');        
    }

my membership_model
Code:
function validate($data)
    {        
        $this->db->where($data);
        $query = $this->db->get('membership');

        return (bool) $query->num_rows;
        
    }
my view
Code:
you are logged in
<br />
&lt;? echo $classification;
    echo $username;
    echo $id;
?&gt;

what im trying to do is having a profile page but to do this i would need first the primary key or id of the user.
#5

[eluser]Kindari[/eluser]
Well, the id is stored in the database, correct? The id is not known when the user logs in. So user logs in, your controller grabs their username and password, then validates it.

But what you need to do is instead of returning the number of rows (which in a user system should either be 1 or 0) just return the row itself, or FALSE. if False, user failed to log in. if the user did login, you return the row.

so change the membership model to:
Code:
function validate($data)
    {        
        $this->db->where($data);
        $query = $this->db->get('membership');

        return $query->row();
        
    }

Then your if($query) should still work. Following that, id is NOT in the post, instead retrieve from row.

Code:
'id' => $row->id,

That should fix your problem unless I missed something else.
#6

[eluser]Wondering Coder[/eluser]
@Kindari

first of all i want to thank you for helping me on this one ^_^.

i followed what you said kindari but now it gave me this error:
Undefined Variable: row and Trying to get property of non-object.
#7

[eluser]Kindari[/eluser]
Sorry friend I confused myself because of my own naming conventions Smile

replace $row with $query and it should work just fine.
#8

[eluser]Wondering Coder[/eluser]
its working now Kindari. again thanks ^Smile^

last question for tonight, if i want to pass the "'id' => $query->id" to my another controller how will i be able to do that? Then to display it to my view?
#9

[eluser]stuffradio[/eluser]
[quote author="Wondering Coder" date="1296178653"]its working now Kindari. again thanks ^Smile^

last question for tonight, if i want to pass the "'id' => $query->id" to my another controller how will i be able to do that? Then to display it to my view?[/quote]

When you have a variable referencing your model, do it like this:

Code:
$data['results'] = $this->model->validate();

$this->load->view('myview',$data);
Now anything you defined with the $data['']; stuff, you can access those variables on the view.
#10

[eluser]Cristian Gilè[/eluser]
Wondering Coder, do a redirect to another controller and pass the id as parameter:

Code:
redirect('controller_name/method_name/'.$query->id);

and then:

Code:
class Controller_name extends Controller
{
   function method_name()
   {
      $data['id'] = $this->uri->segment(3);
      $this->load->view('view_name',$data);
   }
  
   ....

alternatively, you can set a session:

Code:
$this->session->set_userdata('id',$query->id); // or set a flashdata if you need the id only for the next request
redirect('controller_name/method_name/');

You can then retrieve the id in the controller and in the view with

Code:
$this->session->userdata('id');


Cheers



Cristian Gilè




Theme © iAndrew 2016 - Forum software by © MyBB