Welcome Guest, Not a member yet? Register   Sign In
Function Return
#1

[eluser]ibnclaudius[/eluser]
All queries run correctly, but its not echoing TRUE, why?

Code:
public function create_user($name, $email, $password, $sex, $type, $school_id)
{
  $create_user = $this->CI->auth_mod->create_user($name, $email, $password, $sex, $type, $school_id);
  
  if ($create_user)
  {
   echo 'TRUE';
  }
}

Code:
public function create_user($name, $email, $password, $sex, $type, $school_id)
{
  $query = $this->db->insert($this->users_table, array('name' => $name, 'email' => $email, 'password' => $password, 'sex' => $sex));
  
  if ($this->db->affected_rows() > 0)
  {
   $user_id = $this->db->insert_id();
  
   $this->create_user_type($type, $user_id, $school_id);
  }
  else
  {
   return FALSE;
  }
}

public function create_user_type($type, $user_id, $school_id)
{
  $query = $this->db->insert($this->users_types_table, array('type' => $type, 'user_id' => $user_id, 'school_id' => $school_id));
  
  if ($this->db->affected_rows() > 0)
  {
   return TRUE;
  }
  else
  {
   return FALSE;
  }
}
#2

[eluser]CroNiX[/eluser]
you never return TRUE in your create_user() method if affected_rows > 0. You only return FALSE if it isn't.
#3

[eluser]CroNiX[/eluser]
actually, this line:
Code:
$this->create_user_type($type, $user_id, $school_id);
should be
Code:
return $this->create_user_type($type, $user_id, $school_id);
so the result of create_user_type() gets returned. You are just executing it but not returning whether it passed/failed, yet the method is returning TRUE/FALSE.
#4

[eluser]ibnclaudius[/eluser]
For some reason the $this->session->userdata('school_id') is return nothing on Form controller, but I tested on some view files and returned well...

Controller:
Code:
<?php

class Form extends CI_Controller
{
public function __construct()
{
  parent::__construct();
  
  if ($this->input->server('REQUEST_METHOD') != 'POST')
  {
   exit('No direct script access allowed');
  }

  $this->load->library('auth_lib');
  $this->load->library('session');
  $this->load->helper('url');
}

public function create_user()
{
  $name = $this->input->post('name');
  $email = $this->input->post('email');
  $password = sha1($this->input->post('password'));
  $sex = $this->input->post('sex');
  $type = $this->input->post('type');
  $school_id = $this->session->userdata('school_id');
  
  $this->auth_lib->create_user($name, $email, $password, $sex, $type, $school_id);
}
}

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

class Auth_lib
{
private $CI;

public function __construct()
{
  $this->CI =& get_instance();

  $this->CI->load->model('auth_mod');
  $this->CI->load->library('session');
  $this->CI->load->helper('cookie');
  $this->CI->load->helper('url');
}

public function create_user($name, $email, $password, $sex, $type, $school_id)
{
  $create_user = $this->CI->auth_mod->create_user($name, $email, $password, $sex, $type, $school_id);
  
  if ($create_user)
  {
   echo 'TRUE';
  }
}

}

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

class Auth_mod extends CI_Model
{
private $users_table = 'users';
private $users_types_table = 'users_types';
private $users_schools_table = 'users_schools';

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

  $this->load->database();
}

public function create_user($name, $email, $password, $sex, $type, $school_id)
{
  $query = $this->db->insert($this->users_table, array('name' => $name, 'email' => $email, 'password' => $password, 'sex' => $sex));
  
  if ($this->db->affected_rows() > 0)
  {
   $user_id = $this->db->insert_id();
  
   $this->create_user_school($type, $user_id, $school_id);
  }
  else
  {
   return FALSE;
  }
}

public function create_user_school($type, $user_id, $school_id)
{
  $query = $this->db->insert($this->users_schools_table, array('user_id' => $user_id, 'school_id' => $school_id));

  if ($this->db->affected_rows() > 0)
  {
   $this->create_user_type($type, $user_id, $school_id);
  }
  else
  {
   return FALSE;
  }
}

public function create_user_type($type, $user_id, $school_id)
{
  $query = $this->db->insert($this->users_types_table, array('type' => $type, 'user_id' => $user_id, 'school_id' => $school_id));
  
  if ($this->db->affected_rows() > 0)
  {
   return TRUE;
  }
  else
  {
   return FALSE;
  }
}
}




Theme © iAndrew 2016 - Forum software by © MyBB