How can i pass variable from function a to b in codeigniter controller.
function a()
{
$id = $this->input->post('id');
}
I want $id from function a to be passing to b
function b()
{
if($id)
{
.....
}
}
How can i do that?
create $id
public $id = false;
then use
function a(){
$this->id = $this->input->post('id');
}
function b(){
if($this->id){.....}
}
in controls class
class TestApi extends MY_Controller {
public function __construct() {
parent::__construct();
}
public t1 = false;
public function a () {
$this->t1 = true;
}
public function b () {
a();
if($this->t1){
...
}
}
}
or try global var? but not a good idea in framework
$a1 = 5;
function Test()
{
$GLOBALS['a1'] = 1;
}
Test();
echo $a1;
In the Same Class? maybe should check var or what u get from input?
Class Test{
public $t = 1;
public function a(){
$this->t = 20;
}
public function b(){
echo $this->t;
}
}
$TestClass = new Test;
echo $TestClass->t;
echo "-";
echo $TestClass->a();
echo ">";
echo $TestClass->t;
function a(){
$id = $this->input->post('id');
$this->b($id);
}
function b($id){
if ($id) {
//....
} else {
//.....
}
}
i used $this->session->set_userdata('a', $a); to pass the variable and it can be accessed by all method within the controller. to access it i used $b = $this->session->userdata('a');
thanks for all the help :D
Related
For example I have code like this:
$this->users = $data['data'];
$this->month = $data['month'];
$this->year = $data['year'];
But I need to use DTO. For example I used this function in DTO class:
public function getUsers(): string
{
return $this->users;
}
And as I understand I need to add it to the first code. But I don't understand how to use DTO for my the first code. Can you explain me please?
upd
Now I have:
public function __construct($data, $jobWatcherId)
{
$this->jobWatcherId = $jobWatcherId;
$jobsDTO = new JobsDTO($data['data'], $data['month'], $data['year'],
$data['working_days'], $data['holiday_hours'],
$data['advance_payroll_date'], $data['main_payroll_date']);
}
public function handle()
{
$jobWatcher = JobWatcher::find($this->jobWatcherId);
try {
$startedAt = now();
$jobWatcher->update([
'status_id' => JobWatcherStatusEnum::PROCESSING,
'started_at' => $startedAt,
]);
$redmineService = new RedmineAPIService();
foreach ($jobsDTO->getUsers() as $user) {
}
And for line foreach ($jobsDTO->getUsers() as $user) I have Undefined variable '$jobsDTO'
Your question is a bit unclear, but as I understand it, you want to instantiate a DTO with the above data?
You could have a class like:
class UsersDTO
{
public array $users;
public int $month;
public int $year;
public function __construct(array $users, int $month, int $year)
{
$this->users = $users;
$this->month = $month;
$this->year = $year;
}
public function getUsers(): array
{
return $this->users;
}
public function getMonth(): int
{
return $this->month;
}
public function getYear(): int
{
return $this->year;
}
}
and then somewhere else call:
$usersDTO = new UsersDTO($data['data'], $data['month'], $data['year']);
// Do something with $usersDTO->getUsers();
How to use this keyword in codeigniter to store values. Beacuse i use same value multiple time in the same page.
So if the this keyword is help to solve this problem.
function1() {
$value = '123456';
$assign1 = $value;
.....
}
function2() {
$value = '123456';
$assign2 = $value;
.....
}
Yes. We store value using this keyword inside the _construct() function to over come this problem.
The sample code,
public function __construct() {
parent::__construct ();
$this->value = '123456';
}
function1() {
$assign1 = $this->value;
.....
}
function2() {
$assign2 = $this->value;
.....
}
You can declare it as a global variable and use with $this, you can do this in a constructor or above constructor, both will work nice
class MyClass extends CI_Controller {
var $value = '123456';//global variable
function __construct() {
parent::__construct();
//$this->value = '123456';
}
function1() {
$assign1 = $this->value;
.....
}
function2() {
$assign2 = $this->value;
.....
}
}
It create the session but does not go to index2 and index3 always redirect with else and go to index method but i want to go index2 and index3 to handle other panels also.
Session is created successfully for all just comming else condition all the time.
My form data and array is also showing when i using the print_r for my code to view if the data is comming or not.
Problem is it is showing no any error just redirect with file of index method.
My Controller
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Main_Model');
$this->load->helper('url');
$this->load->library('session');
$method = $this->router->fetch_method();
$methods = array('index','index2','index3');
if(in_array($method,$methods))
{
if(!$this->session->has_userdata('signup_email'))
{
redirect(base_url('Main/login'));
}
}
}
public function index()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('BKO/index');
}
}
public function index2()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('Admin/index');
}
}
public function index3()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('Owner/index');
}
}
public function login()
{
//$data['select'] = $this->Main_Model->get_select();
$this->load->view('login');
}
public function login_process()
{
//$roll = $this->input->post('select');
echo $email = $this->input->post('email');
echo $pass = $this->input->post('upass');
$query = $this->Main_Model->login_process($email,$pass);
if($query == TRUE)
{
$this->session->set_userdata('signup_email');
$session = array(
'signup_email' => $email
);
$this->session->set_userdata($session);
redirect(base_url('Main/check_login'));
}
else
{
$this->session->set_flashdata('error','Invalid Email or Password');
redirect(base_url('Main/login'));
}
}
public function check_login()
{
if($this->session->userdata() == 'admin#gmail.com')
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index2'));
}
elseif($this->session->userdata() == 'owner#gmail.com')
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index3'));
}
else
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index'));
}
}
public function logout()
{
$this->session->sess_destroy();
redirect(base_url());
}
My Model
public function login_process($email,$pass)
{
//$this->db->select('*');
//$this->db->where('roll_id',$roll);
$this->db->where('signup_email',$email);
$this->db->where('signup_password',$pass);
$query = $this->db->get('signup');
if($query->num_rows() > 0)
{
$this->session->set_flashdata('signup_email');
return true;
}
else
{
return false;
}
}
You missed the parameter here
if($this->session->userdata() == 'admin#gmail.com')
instead it should be
if($this->session->userdata('signup_email') == 'admin#gmail.com')
I am having problem with my code, as below
The error i am is as follow
( ! ) Fatal error: Call to undefined method Agentie_model::get_agentii_list() in C:\wamp\…controllers\fk_controller.php on line 65
Controller:fk_controller
public function __construct() {
parent::__construct();
$this->load->model('agentie_model');
$this->load->model('fk_model');
}
public function add2() {
$this->load->model('agentie_model');
$data['principal'] = $this->agentie_model->get_agentii_list(); //Here is the error(line 65)
$this->load->view('lista_agentii', $data);
}
Model:agentie_model
private $_table = "agentii";
public function get_agentii_list() {
$query = $this->db->get($this->_table);
return $query->result();
}
View:lista_agentii
echo "Lista agentii:</br>";
foreach($principal as $list) {
echo $list->nume_agentie;
}
public function __construct()
{
parent::__construct();
$this->db = $this->load->database('default', true);
}
public function add2()
{
$this->load->model('Agentie_model',true);
$data['principal'] = $this->Agentie_model->get_agentii_list(); //Here
$this->load->view('lista_agentii', $data);
}
in your controller set this below
function fk_controller() {
parent::__construct();
$this->load->model('agentie_model');
$this->load->model('fk_model');
}
public function add2() {
$this->load->model('agentie_model');
$data['principal'] = $this->agentie_model->get_agentii_list(); //Here is the error(line 65)
$this->load->view('lista_agentii', $data);
}
in your model view
class Agentie_model extends CI_Model
{
public function get_agentii_list() {
$query = $this->db->get($this->_table);
return $query->result();
}
}
Having real big problems with CodeIgniter sessions. I can't get any userdata, really unusual thing, who can help me? I don't know how to realize it, I'm not getting the session.
Here is code:
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('admin_model');
}
public function do_login($username,$password)
{
$right_login = $this->admin_model->get_username($username);
$right_password = $this->admin_model->get_password($password);
if( ! empty($right_login) && ! empty($right_password))
{
$session = array();
$session['admin_logined'] = 'yes';
$session['user_ip'] = $_SERVER['REMOTE_ADDR'];
$this->session->set_userdata($session);
redirect('admin/main');
}
else
{
redirect('admin');
}
}
public function do_logout()
{
$session = array();
$session['admin_logined'] = '';
$session['user_ip'] = '';
$this->session->unset_userdata($session);
redirect('admin');
}
public function check_admin()
{
if(($this->session->userdata('admin_logined') === "yes"))
{
return TRUE;
}
else
{
redirect('admin');
}
}
}
To get session working in codeigniter you have to loaded session library to your controller constructor .
$this->load->library('session'); for more detail Codeigniter session.