Welcome Guest, Not a member yet? Register   Sign In
controller class variables
#1

[eluser]dan.syme[/eluser]
I am having trouble declaring a variable that can be used throughout the functions in my controller.

snippet below I get Message: Undefined variable: errors for a my variables.

I wanted to clean up my code so I didn't have to declare all my variables for each function with in the controller.

Code:
<?php

class sim_audio_presets extends Controller {

    function sim_audio_presets()
    {
        parent::Controller();    
        
        //load libraries and helpers
        $this->load->library('user_agent');
        $this->load->helper('socket');
        $this->load->helper('url');
        
        //Set Database Table
        $table = 'sim_audio_presets';
        
        //Set callback server
        $server = '************';
        $port = '****';
        
    }

function get()
    {
        
        
        $id = $this->uri->segment(3);

        $this->db->where('room_id', $id);
        $query = $this->db->get($table);
        
        foreach ($query->result() as $row)
        {
            $data[] = array('id'        => $row->id,
                            'room_id'    => $row->room_id,
                            'preset'    => $row->preset,
                            'name'        => $row->name);
        }
        
        $json = '{"'. $table .'":' . json_encode($data) . '}';
        $data['json'] = $json;
        
        $this->load->view('responder', $data);
    }
#2

[eluser]dan.syme[/eluser]
nm, i'm a dumbzy head. should do $this->load->vars() in the controller.
#3

[eluser]WanWizard[/eluser]
You're defining local variables, not class variables:
Code:
<?php
class sim_audio_presets extends Controller {

    var $table; // add others here as needed

    function sim_audio_presets()
    {
        parent::Controller();    
        
        //load libraries and helpers
        $this->load->library('user_agent');
        $this->load->helper('socket');
        $this->load->helper('url');
        
        //Set Database Table
        $this->table = 'sim_audio_presets';
        
        //Set callback server
        $server = '************';
        $port = '****';
        
    }

function get()
    {
        
        
        $id = $this->uri->segment(3);

        $this->db->where('room_id', $id);
        $query = $this->db->get($this->table);
        
        foreach ($query->result() as $row)
        {
            $data[] = array('id'        => $row->id,
                            'room_id'    => $row->room_id,
                            'preset'    => $row->preset,
                            'name'        => $row->name);
        }
        
        $json = '{"'. $table .'":' . json_encode($data) . '}';
        $data['json'] = $json;
        
        $this->load->view('responder', $data);
    }
#4

[eluser]dan.syme[/eluser]
yea. brain fart.
#5

[eluser]steelaz[/eluser]
To use the same variable in all methods of your class (controller) you have to declare them as class properties. For example, I'm going to declare property $table:

Code:
class sim_audio_presets extends Controller
{
    private $table = '';

    function sim_audio_presets()
    {
        // Something here
    }

    function get()
    {
        // Something here
    }
}

To use class property you have to add $this-> before variable name:

Code:
class sim_audio_presets extends Controller
{
    private $table = '';

    function sim_audio_presets()
    {
        // Set table name
        $this->table = 'sim_audio_presets';
    }

    function get()
    {
        // Use table name in query
        $query = $this->db->get($this->table);
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB