Mehur Mehur - 1 month ago 7
MySQL Question

How to get model value to controller?

I am using codeigniter 3.1 .

I want to pass posted data through model, but to pass login i need to get model value to controller ?

How to get model value to controller?

Controller

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("user_login");
}

public function index()
{

$post = $this->input->post("post", true);

if ($this->user_login->check($post)) {
if ($this->user_login->logins == "pass") {
echo "valid";
} else {
echo "Invalid";
}
}
}
}
?>


Model

<?php

class User_Login extends CI_Model
{
var $logins=false;

public function check($post)
{
$results = $this->db->where('username', $post)
->get('users');

if($results->num_rows() > 0){
$this->logins = "pass";
} else {
$this->logins = "fail";
}
}
}
?>

Answer

1) At first, if you want to use any public variable, no need to use var with it. Just write $logins=false;

2) Next you should something from your model for checking this type of thing. Like return "pass" or return "fail"

3) After that when you check it from controller, you can keep this return data to a variable like this $return_value = $this->user_login->check($post). At last, it can be like below code -

 if ($return_value) {
    if ($return_value == "pass") {   
    echo "valid";
   } else {
    echo "Invalid";
   }
}
Comments