Welcome Guest, Not a member yet? Register   Sign In
Passing a variable from a Controller to a Model
#1

[eluser]Konvurt[/eluser]
Hi,

I have variables that are set in a controller that I need to access for a query found in my model.

Controller:

Code:
$data['main_content'] = 'login_form';
        $this->load->view('includes/template', $data);
                $this->load->model('membership_model');


     //session_start();
  require_once 'cas.php';
  $MYUIN=getCAS();
  //$MYUIN = $_SESSION['MYUIN'];

  $MYNETID = $_SESSION['MYNETID'];

$netid = $MYNETID;

//die("Your NETID  is  $netid");

        $query = $this->membership_model->validate();

                      

    }

Model

Code:
<?php

class Membership_model extends Model {

    function validate()
    {
        $this->db->where('netid',$this->netid); //this->input->post('netid')
        //$this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('users');
        echo $this->db->last_query(); exit();
        if($query->num_rows == 1)
        {
                    //die("Your netid was found.");
            return true;
        }
        
    }

Whenever I try this I receive an error a message stating:
Quote:Undefined property: Membership_model::$netid
The query from the model outputs as : SELECT * FROM (`users`) WHERE `netid` IS NULL

If I update my model to read " $this->db->where('netid',$netid);
The query from the model outputs as : SELECT * FROM (`users`) WHERE `netid`= 0

I am sure this probably something simple. I have poked around on this and have gotten anything but more frustrated.

Thanks.
#2

[eluser]OES[/eluser]
This is no where near perfect but hopefully well help you get on the right track.

Code:
## Your Controller Example

require_once 'cas.php';

class My_Controller extends Controller
{

    function __construct()
    {
        parent::__construct();
        // Load Required Helpers/Libraries etc...
        $this->load->model('membership_model');
    }
    
    public function my_function()
    {
        $data['main_content'] = 'login_form';

        $MYUIN = getCAS();
        $MYNETID = $_SESSION['MYNETID'];
    
        if($this->membership_model->validate($MYNETID)):
            $data['validated'] = true;  // Or whatever you want assigned
        else:
            $data['validated'] = false;  // Same as above
        endif;

        $this->load->view('includes/template', $data);
    }

}


## Your Model Example

class Membership_model extends Model
{
    function validate($net_id = '')
    {
        $this->db->where('netid', $netid);
        $query = $this->db->get('users');
        if($query->num_rows > 0):
            return true;
        endif;
        
        return false;
    }
}

Lee




Theme © iAndrew 2016 - Forum software by © MyBB