Welcome Guest, Not a member yet? Register   Sign In
Passing arrays back from own class library to Controller
#1

[eluser]SniffTheGlove[/eluser]
New to CI and OOP

I have created a class library, the file is in the library directory and is loaded via the Controller construct.

One of the functions in the class returns an array but I am having trouble getting access to the array back in the controller to pass onto the view as a data variable.

For simplicity here this is my shortened function from my Class Library for example, there are many more variables to be returned in the array that I have not shown here.

function Get_Trans_TS() {
date_default_timezone_set('UTC');
$Trans_TS_Array = array();
$Trans_TS_Array['Timestamp_Now'] = gmmktime(gmdate("H"), gmdate("i"), gmdate("s"), gmdate("n"), gmdate("j"), gmdate("Y"));
$Trans_TS_Array['Timestamp_Full'] = gmdate('Y-m-d H:iConfused', $Trans_TS_Array['Timestamp_Now']);
$this->Trans_TS = $Trans_TS_Array;
}

Now I was hoping to access the array from a controller function by assigning the array but I can not find out how to do this and would like a little help.

I have tried
$data['Timestamp_Now'] = $this->TransClass->Get_Trans_TS['Timestamp_Now'];
$data['Timestamp_Full'] = $this->TransClass->Get_Trans_TS['Timestamp_Now'];

I keep getting the error...
Message: Trying to get property of non-object
Filename: controllers/controller_home.php

I have also tried
$data['Timestamp_Now'] = $this->TransClass->Trans_TS['Timestamp_Now'];
$data['Timestamp_Full'] = $this->TransClass->Trans_TS['Timestamp_Now'];

and

$data['Timestamp_Now'] = $this->TransClass->Trans_TS->Timestamp_Now;
$data['Timestamp_Full'] = $this->TransClass->Trans_TS->Timestamp_Now;


Thank you
#2

[eluser]Eric Barnes[/eluser]
I think your library you just need to return your array:
Code:
public function Get_Trans_TS() {
  date_default_timezone_set(‘UTC’);
  $Trans_TS_Array = array();
  $Trans_TS_Array[‘Timestamp_Now’] = gmmktime(gmdate(“H”), gmdate(“i”), gmdate(“s”), gmdate(“n”), gmdate(“j”), gmdate(“Y”));
  $Trans_TS_Array[‘Timestamp_Full’] = gmdate(‘Y-m-d H:i:s’, $Trans_TS_Array[‘Timestamp_Now’]);
  $this->Trans_TS = $Trans_TS_Array;
  return $Trans_TS_Array;
}
#3

[eluser]SniffTheGlove[/eluser]
Thanks for the reply, but really struggling with this OOP approach to things and also CI (Only been 3 days of learning on my own with a simple CI tutorial that does not cover much and the CI user guide)

I have now returned the array in the class function as suggested but still struggling on the syntax to retrieve data from the class array in the controller and assign one element to a new variable for passing to the view etc.

eg what is the syntax to assign the variable $data[‘Timestamp_Now’] with the class function Get_Trans_TS to get the array value of [‘Timestamp_Now’]

Confused and need help seeing. Thanks
#4

[eluser]HardcoreNachos[/eluser]
In library:
class myClass {

var $myVar = array("value1", "value2", "value3");

function __construct(){

}

}

In controller:
class myController extends CI_Controller {

public function index(){

$this->load->library('myClass'); //remove if you use auto load
$myNewVar = $this->myClass->myVar; //this will be set to $myVar from myClass

}

}

Edit:

So then using the array is done like this:
class myController extends CI_Controller {

public function index(){

$this->load->library('myClass');
$myNewVar = $this->myClass->myVar;
$anotherVar = $myNewVar[0]; //$anotherVar will be set to the string 'value1'

}

}
#5

[eluser]SniffTheGlove[/eluser]
I am afriad I can not get this to work.

I have had to change all the single and double quotes in the pasted code to just a single ', this then solved theose errors

Now I am left with

A PHP Error was encountered
Severity: Notice
Message: Undefined property: MyClass::$MyClass
Filename: controllers/controller_home.php
Line Number: 18

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/controller_home.php
Line Number: 18

Line 18 is
$myNewVar = $this->MyClass->myVar;

I have tried putting in
$cl = new MyClass;
just before line 18 but I still get an error.
#6

[eluser]HardcoreNachos[/eluser]
In CI, after loading a library ( $this->load->library() ) you are able to call functions and get/set the library's variables easily via the controller.
Take a look at these few lines from my previous post (in the controller):

public function index(){

$this->load->library(‘myClass’);
$myNewVar = $this->myClass->myVar;

}


Within this function I simply load the the library 'myClass'. Now because my library has been loaded, I can call it by '$this->myClass' in my controller (only AFTER the library has been loaded, such as in this case). See on the second line of the same function, the variable 'myVar' from the myClass library is called by '$this->myClass->myVar;' this is then set to a variable called 'myNewVar'.

It seems you have not grasped OOP and maybe this is the problem. Using CI as a base for learning OOP may not be your best place to start. I would suggest gaining a little more knowledge on the concepts and functionality of OOP in PHP; also if you don't know much about MVC (Model View Controller) then that is also something you should really consider getting a solid understanding of... Hope I could help =D




Theme © iAndrew 2016 - Forum software by © MyBB