Val Do
Val Do

Reputation: 2695

How to use variable from model to controller codeigneter?

Hello I have this script in model

<?php

class Log_in extends CI_Model
{

    public function sign_in()
    {
        $Email = $this->input->post('Email');

        $this->db-select('Email');
        $this->db-from('users');
        $this->db->where('Email' , $Email);
        $this->db->limit(1);

        $query = $this->db->get();

        if($query->num_rows() == 1){
            return $query->result();
        }else{
            return false;
        }

    }
}

and this controller

<?php

class LogIn_Controller extends CI_Controller{

    function __construct()
    {
        parent::__construct();
    }   


    public function LogIn(){

        $this->load->model('Log_in');

        if($query){
            echo "login success";
        }else{
            echo "error";
        }

    }
}

when I check if(query) codeigneter show error page undefined variable $query ?

Upvotes: 0

Views: 103

Answers (5)

Tarang patel
Tarang patel

Reputation: 408

First the call the main model Log_in in your controller and after the create one variable and called only the model method... changes your controller as following

public function LogIn(){

    $this->load->model('Log_in'); //Load The Model In your construct at Once
    $query = $this->Log_in->sign_in(); //create var and load the model method
    if($query){
        echo "login success";
    }else{
        echo "error";
    }

}

Upvotes: 1

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

<?php

    class LogIn_Controller extends CI_Controller{

        function __construct()
        {
            parent::__construct();
            $this->load->model('Log_in');//this load your model once
        }


        public function LogIn(){

            $query = $this->Log_in->sign_in();//assign value to query

            if($query)
            {
                echo "login success";
            }else
            {
                echo "error";
            }
        }
    }

Read this

  1. CI Model
  2. CI Controller

Upvotes: 2

Subho Ghose
Subho Ghose

Reputation: 97

Undefined variable $query showing because you didn't declare the variable "$query". You can try the following code :

public function LogIn(){

    $this->load->model('Log_in');
    $query = $this->Log_in->sign_in(); // variable $query declared 
    if($query){
        echo "login success";
    }else{
        echo "error";
    }

}

Upvotes: 1

Sachin Gadagi
Sachin Gadagi

Reputation: 749

Either declare a variable (say $query )

$query = $this->load->model('Log_in');

    if($query){
        echo "login success";
    }else{
        echo "error";
    }


OR

Directly check in the if condition

    if($this->load->model('Log_in')){
        echo "login success";
    }else{
        echo "error";
    }

Upvotes: 1

Rejoanul Alam
Rejoanul Alam

Reputation: 5398

change your LogIn controller as following

public function LogIn(){

    $this->load->model('Log_in');
    $query = $this->Log_in->sign_in();
    if($query){
        echo "login success";
    }else{
        echo "error";
    }

}

Upvotes: 2

Related Questions