CodeIgniter invoke variable from another function in the same controller - codeigniter

I have a problem. I want to access to a variable from another function. let me explain.
Controller
public function obtener_oplataforma_usuarios() {
$this->load->helper('form');
$this->load->model('usuarios_model');
$plataforma = $this->input->post('id_plataforma');
$data['records'] = $this>usuarios_model>obtener_plataformas_usuarios($plataforma);
$this->load->view('vista_plataforma_usuarios', $data);
}
function grupos($grupo){
$this->load->helper('form');
$this->load->model('usuarios_model');
$plataforma = 'BaD';
$data['records'] = $this->usuarios_model->obtener_grupos($plataforma, $grupo);
$this->load->view('vista_plataforma_usuarios', $data);
}
Model
function obtener_grupos($plataforma, $grupo){
$this->db->select('*');
$this->db->from('usuarios');
$this->db->where('Plataforma', $plataforma);
$this->db->where('grupos', $grupo);
$query = $this->db->get();
return $query->result();
}
I want to access to the variable platforma in the function obtener_oplataforma_usuarios from the function obtener_grupos I tried different things and I still have problems.
Can you help me to solve this issue. and I want to do that because I want to make a query with two validations one is group and the other platforma. My problem is the second one. I recibe the information of plataforma fine from a from this is in the function obtener_oplataforma_usuarios and is fine but I do not how to read this variable from the other function.

you lose minuses (-) in pointers :)
$data['records'] = $this>usuarios_model>obtener_plataformas_usuarios($plataforma);
must be:
$data['records'] = $this->usuarios_model->obtener_plataformas_usuarios($plataforma)

You can use a private variable in your class.
Create the variable above your __construct function, private $plataforma
Set the default value in your __construct function
Set and get in your public function using $this->plataforma
Implemented in your code:
private $plataforma;
public function __construct() {
parent::__construct();
$this->plataforma = "";
}
public function obtener_oplataforma_usuarios() {
$this->load->helper('form');
$this->load->model('usuarios_model');
$this->plataforma = $this->input->post('id_plataforma');
$data['records'] = $this>usuarios_model>obtener_plataformas_usuarios($this->plataforma);
$this->load->view('vista_plataforma_usuarios', $data);
}
function grupos($grupo){
$this->load->helper('form');
$this->load->model('usuarios_model');
$data['records'] = $this->usuarios_model->obtener_grupos($this->plataforma, $grupo);
$this->load->view('vista_plataforma_usuarios', $data);
}

Related

Method behavior with codeigniter parameters

Needing help understanding codeigniter 3 behavior
I'm not understanding the controller behavior very well and I ask for help
class Simulado extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('funcoes_helper');
$this->load->helper('file');
$this->load->library('session');
$this->load->library('controle_acesso');
$this->load->model('resumoapp_model', 'resumo');
$this->load->model('homeapp_model', 'homeapp');
$this->load->model('simulado_model', 'simulado');
//permissão para acessar app
$this->controle_acesso->acesso();
$this->output->enable_profiler(TRUE);
}
public function index() {
$dados = '0'//todas as materias;
$bdinsert = $this->simulado->set_bd($dados);
$this->load->view('prova',$dados)
}
public function materia () {
$dados = 1; //idamateria
$bdinsert = $this->simulado->set_bd($dados);
$this->load->view('prova',$dados)
}
}
model
public function set_bd ($dados) {
$data = [
'id_mat' = $dados
];
$this->db->insert('simulados', $data);
}
routes.php
$route['default_controller'] = 'home';
$route['404_override'] = 'my404';
$route['translate_uri_dashes'] = FALSE;
$route['app'] = "/app/home";
$route['site'] = "/home";
when accessed through the link http://localhost/escola/app/simulado, the system behaves in the desired way, it writes the value zero once in the database, for example.
accessing the url we have the method of the class, simulated, http://localhost/escola/app/simulado/materia/1/exatas-matematica calls the method in the correct way, but when writing to the database it writes the code of the matter.
I did a test and put the same code of the article method in the index and called the url, and it worked perfectly, writing only once the code of the article in the database.
I need to know why it is recording 3 times in the database when accessing the url is a method of the class and has parameters.

Loading view inside a Library, issues with cached vars

I am trying to implement a widgets library using load->view. I know I can use include to call directly the file and avoid the vars cache issues but just wondering why it does not work.
Here is how I have structured my code:
My Controller:
class Page extends MY_Controller {
public $data = array();
public function __construct() {
parent::__construct();
...
$this->load->library('widgetmanager');
}
public function index($slug = '') {
echo $this->widgetmanager->show(2);
echo $this->widgetmanager->show(1);
}
}
My Library
class WidgetManager
{
private $CI;
public function __construct()
{
$this->CI = & get_instance();
}
public function show($widget_id) {
$data = array();
$widget_id = (int)$widget_id;
$this->CI->db->select('*');
$this->CI->db->from('widget');
$this->CI->db->where('id', $widget_id);
$query = $this->CI->db->get();
$item = $query->row_array();
$data['widget_title'] = $item['title'];
$data['widget_content'] = $item['content'];
$widget = $this->CI->load->view('widget/'.$item['source'], $data, TRUE);
$data['widget_title'] = '';
$data['widget_content'] = '';
$this->CI->load->view('widget/'.$item['source'], $data);
return $widget;
}
}
widget 1: Calls widget/content
widget 2: Calls widget/banner
What is happening is, the vars set on the first widget call (they are same name as second widget call), get cached, meaning values from the first call are passed to same call. It is weird because are different views.
I have tried:
Using clear_vars(): $this->CI->load->clear_vars(), before and after doing load->view on the library.
Calling load->view with empty array, null, etc
Tried to add a prefix with the widget slug to the vars (that works, but I have to send in some way the prefix to the view, so it is not possible due cache issue)
Any ideas?
Here is what should work.
(I took the liberty of simplifying your database call making it require much less processing.)
public function show($widget_id)
{
$data = array();
$widget_id = (int) $widget_id;
$item = $this->CI->db
->get_where('widget', array('id' => $widget_id))
->row_array();
$data['widget_title'] = $item['title'];
$data['widget_content'] = $item['content'];
$widget = $this->CI->load->view('widget/'.$item['source'], $data, TRUE);
//clear the cached variables so the next call to 'show()' is clean
$this->CI->load->clear_vars();
return $widget;
}
On further consideration The call $this->CI->load->clear_vars(); is probably pointless because each time WidgetManager::show() is called the $data var is recreated with exactly the same keys. When the $data var is passed to load->view the new values of $data['widget_title'] and $data['widget_content'] will replace the values in the cached vars using those keys.

Error when calling procedures in code igniter

I am calling a procedure in code igniter and when I try to call another procedure I get the following error:
Error Number: 2014
Commands out of sync; you can't run this command now
call get_post_info($id)
Filename: C:/Apache24/htdocs/application/models/Posts_model.php
Line Number: 18
I think I need to clear the result set... not sure.
Here is my model:
<?php class Posts_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_post_ids($acc_id){
$query = $this->db->query('call get_post_ids('.$this->db->escape($acc_id).')');
return $query->row_array();
}
public function get_post($id)
{
$query = $this->db->query('call get_post_info($id)');
return $query->row_array();
}
}
In my controller:
$post_ids = $this->posts_model->get_post_ids(1);
foreach ($post_ids as $id){
array_push($data, $this->posts_model->get_post($id));
}
I didn't actually run these solutions, but I found some info that might be what you look for. First of all, check this post about the cause of the problem.
I suppose $query->free_result() should solve the problem:
public function get_post_ids($acc_id){
$query = $this->db->query('call get_post_ids('.$this->db->escape($acc_id).')');
$result = $query->row_array();
$query->free_result();
return $result;
}
And in case that doesn't work, this this may be a good solution for CodeIgniter.

How to use session_data in codeigniter throughout Website without including in particular view

Is there any other best way to use session_data in website.
How i set session in my project:
$sess_array = array('id' => $row->user_id,'name'=>$row->user_name,'email'=>$row->email,'condition'=>'','balance'=>$row->balance,'did_alloted'=>$row->did_alloted,'create_date'=>$row->create_date);
$this->session->set_userdata('logged_in', $sess_array);
when it comes to controller:
$data['id'] = $session_data['id'];
$data['name'] = $session_data['name'];
$data['email'] = $session_data['email'];
$data['balance']=$session_data['balance'];
$data['did_alloted']=$session_data['did_alloted'];
$data['create_date']=$session_data['create_date'];
$this->load->view('san-reception', $data);
and in my view. I use <?php echo $name ?> to get session_data.
So is there any method by which i can directly access session_data in view without including as $data.
$this->session->userdata('logged_in'); will be available directly in views and hence you just need to assigned this to a variable and then use that as array like bellow.
$userdata=$this->session->userdata('logged_in');
now variable $userdata contain all array field that you set in controller and hence you can use it as $userdata['id'], $userdata['name'] etc
public function __construct()
{
parent::__construct();
$data['session_data']=$this->session->user_data('logged_in');
}
public function some_function(){
$data['dummy']="";
$this->load->view('someview3',$data)
}
public function some_function1(){
$data['dummy']="";
$this->load->view('someview1',$data)
}
public function some_function2(){
$data['dummy']="";
$this->load->view('someview2',$data)
}
If you assign that into the constructor then it will call by automatically whenever a function calls in the controller.
you can view it in you view page by,
print_r($session_data);

CodeIgniter encoding and decoding arguments in url

Ok i wanna encode arguments which i send to controler and function in this case number 1 i wanna use url_decode and encode function is it possible to do?
view page
Create
controler
public function page($selfi){
$this->db->select('*');
$this->db->from('photos');
$this->db->join('bridge', 'bridge.photos_id = photos.id');
$this->db->where('bridge.tags_id',$selfi);
$data['query'] = $this->db->get();
$this->load->view('gallery_view',$data);
}
I'm not sure why you're trying to do it this way, but here's how:
Create
And on the controller side:
public function page($selfi){
$selfi = url_decode($selfi);
$this->db->select('*');
$this->db->from('photos');
$this->db->join('bridge', 'bridge.photos_id = photos.id');
$this->db->where('bridge.tags_id',$selfi);
$data['query'] = $this->db->get();
$this->load->view('gallery_view',$data);
}

Resources