I want to declare a global variable with a value and access it in the functions.
I'm done below code but showing Undefined variable: msg1 in the login function.
Please help me to do this.
class loader extends CI_Controller {
public $msg1 = "login error";
public function __construct() {
parent::__construct();
}
public function login() {
$result = $this->login_model->login();
if (!$result) {
$msg = $this->$msg1;
$this->index($msg) ;
} else {
//something here
}
}
}
}
Try this,
class loader extends CI_Controller {
public $msg1 = "login error";
public function __construct() {
parent::__construct();
}
public function login() {
$result = $this->login_model->login();
if (!$result) {
$msg = $this->msg1;
$this->index($msg) ;
} else {
//something here
}
}
}
}
Remove $ from $msg1 in login function
$msg = $this->msg1;
Related
The value of variable $jwtoken in method addNew() is null. I want to pass auth() method value "some val" to addNew() method/function.
class Employees extends CI_Controller
{
public $jwtoken = null;
public function __construct()
{
parent::__construct();
}
public function auth() {
if(condition is true){
$this->jwtoken = 'val1';
}
else{
$this->jwtoken = 'val2';
}
}
public function addNew()
{
$this->auth();
echo $this->jwtoken;
}
// ..
}
Try this..
class Employees extends CI_Controller{
public function __construct()
{
parent::__construct();
}
public function auth() {
if(condition==true){
return "some val";
}else{
return "some other val";
}
}
public function addNew()
{
echo $this->auth();
}
}
Your code is working fine in codeigniter 4. try removing constructor.
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class checklist extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('checklist_model');
}
public function index()
{
$data['check_list'] = $this->checklist_model->get_all_details();
$this->load->view('show_checklist', $data);
}
public function form_dropdown()
{
$this->load->view('insert_checklist');
}
public function data_submitted()
{
$data = array('dropdown_single' => $this->input->post('Technology'));
$this->load->model('checklist_model');
$this->checklist_model->insert_in_db($data);
$this->load->model("checklist_model");
$result = $this->checklist_model->read_from_db($data);
$data['result'] = $result[0];
$this->load->view('insert_checklist', $data);
}
public function add_form()
{
$this->load->view('insert_checklist');
}
public function edit()
{
$id = $this->uri->segment(3);
$data['details'] = $this->checklist_model->getById($id);
$this->load->view('edit', $data);
}
public function delete($id)
{
$this->checklist_model->delete_a_detail($id);
$this->index();
}
public function insert_new_details()
{
$udata['technology_name'] = $this->input->post('technology_name');
$udata['questions'] = $this->input->post('questions');
$udata['status'] = $this->input->post('status');
$udata['comments'] = $this->input->post('comments');
$res = $this->checklist_model->insert_details_to_db($udata);
if($res)
{
header('location:'.base_url()."/index.php/checklist/".$this->index());
}
}
public function update()
{
$mdata['technology_name']=$_POST['technology_name'];
$mdata['questions']=$_POST['questions'];
$mdata['status']=$_POST['status'];
$mdata['comments']=$_POST['comments'];
$res=$this->checklist_model->update_info($mdata, $_POST['id']);
if($res)
{
header('location:'.base_url()."/index.php/checklist/".$this->index());
}
}
}
?>
it means you call for an array variable's index 'question', and that index does not exist in that array. Maybe in function update(), $_POST['question']. Not sure. You should provide the full error msg, like file and line.
Also: the view if that's the case. And make this sound more like a question.
I have more than 30 methods in this User Controller.I don't want to create another controller just for this single method.
class User extends CI_Controller {
public function __construct()
{
parent::__construct();
if(empty($_SESSION['userid'])){
$error['error'][] = "Please LogIn";
echo json_encode($error);
exit;
}
}
public function index(){
}
public function get_public_pages(){
}
}
I don't want the constructor function to run when accessing get_public_pages method.How can I do it?
Try this,
public function __construct(){
parent::__construct();
$method = $this->router->fetch_method();
if(empty($method ) && $method != 'get_public_pages'){
if(empty($_SESSION['userid'])){
$error['error'][] = "Please LogIn";
echo json_encode($error);
exit;
}
}
}
i have crated a custom library file for the login validation. if i call the custom library at before $this->load->library('form_validation');
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('loginuser');
$this->load->library('validate_login','','session_validation');// where validate_login is the custom library class inside applications/libraries/validate_login.php
}
function index()
{
//my code
$this->load->library('form_validation');
}
}
This is the error i got $this->load->library('form_validation'); it works perfectley.
i just want to know Why this code works? Am i overwriting the default libraries?
Message: Undefined property: VerifyLogin::$form_validation
if i load the library after
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('loginuser');
$this->load->library('form_validation');
$this->load->library('validate_login','','session_validation');// where validate_login is the custom library class inside applications/libraries/validate_login.php
}
function index()
{
//my code
}
}
It looks like you're loading your libraries wrong. Try one of these;
Multiple
$this->load->library(array('library1', 'library2'));
Single
$this->load->library('library1');
$this->load->library('library2');
#sobiaholic
this is validate_login
class Validate_login extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('loginuser');
$this->load->helper('url');
}
function is_logged()
{
if(isset($this->session->userdata['my_session_id']))
{
if(strlen($this->session->userdata['my_session_id']))
{
return TRUE;
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
function validate_login()
{
$session_id=$this->session->userdata['my_session_id'];
$this->db->select('last_activity,user_data');
$this -> db -> from('sessions');
$this->db->where('session_id',$session_id);
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1)
{
$results=$query->result();
$active_session=$this->session_alive($results[0]->last_activity,$session_id);
if($active_session==TRUE)
{
return TRUE;
}
else
{
$this->verifylogin->logout();
}
}
else
{
return false;
}
}
function session_alive($valid_till,$session_id)
{
$time_limit=$this->config->item('sess_expiration');
if (time() - $valid_till > $time_limit)
{
return FALSE;
}
else
{
if($this->update_session($session_id)==TRUE)
{
return TRUE;
}
else
{
return FALSE;
}
}
}
function update_session($session_id)
{
$new_time=time();
$data=array('last_activity'=>$new_time);
$this->db->where('session_id',$session_id);
$this->db->update('sessions',$data);
if($this->db->affected_rows())
{
return TRUE;
}
}
}
I am new to php, so sorry if this is lame.
I have a simple class that has two private functions. For some reason when I try to call _sendInvalidEmailNotice() I get an error stating that the function is undefined.
What am I doing wrong?
class Mail extends CI_Controller {
function __construct() {
parent::__construct();
$this - > load - > helper('email');
}
function _sendMessage($message) {
send_email('name#gmail.com', 'Test Email', $message);
$success = array('success' = > 'Mail Sent');
echo json_encode($success);
}
function _sendInvalidEmailNotice() {
$errorMessage = array('error' = > 'Invalid Email Address');
echo json_encode($errorMessage);
}
public
function sendMail($returnAddress, $message) {
if (valid_email($returnAddress)) {
_sendMessage($message);
} else {
_sendInvalidEmailNotice();
}
}
}
You need to add context.
$this->_sendInvalidEmailNotice();