Welcome Guest, Not a member yet? Register   Sign In
Can I change class properties/variables within a controller from one function to the next?
#1

[eluser]oldblueday[/eluser]
Sorry if this has been answered, but I've been searching the forums and cannot find a solution. I want to pass an array from one controller function to another, but it doesn't seem to work.

Code:
class Testing extends Controller
{
  private $grading_key = array();
  private $test = "HELLO?";

  function _construct() { parent::_construct(); }

  function show_quiz()
  {
    echo $this->test; // this outputs "HELLO?"
    $this->test = "GOODBYE!";
    $this->grading_key = array('a', 'c', 'd', 'e');
    $this->load->view('the_quiz'); // this will generate a form which will call grade_quiz()
  }

  function grade_quiz()
  {
    // this function is called by a form
    echo $this->test;       // this outputs "HELLO?" not "GOODBYE!"
    print_r($grading_key);  // this outputs a blank array, not the key from above
  }
}

Obviously the properties are being reset when the grade_quiz() function is called (from the form). Is there a way that $this->test can still be "GOODBYE?" and $this->grading_key be the answer array instead of an empty array.

Thanks-Rahul
#2

[eluser]Clooner[/eluser]
There are many ways of doing this!

Maybe simply using the form validator is an option for you!
http://ellislab.com/codeigniter/user-gui...ation.html

Or look into flashdata http://ellislab.com/codeigniter/user-gui...sions.html
or post the Test variable or and Id to that variable in the form! (will need some storage mechanism!)




When using flashdata make sure your settings are correct otherwise you will run into trouble sooner or later
http://ellislab.com/forums/viewthread/157468/
#3

[eluser]pkrstic[/eluser]
1. this line in grade_quiz() method
print_r($grading_key);

works fine Wink BUT it's not correct, this should be print_r($this->grading_key); then this will work correctly.

2. method grade_quiz() works fine again, when you call it as action in form, since variable parameters are not saved between to calls. This is how PHP works. To save value between two calls you could do that over SESSION variable (not CI session class - two different things), COOKIES (this is CI session class), data base, or send that value as hidden element in form (avoid this solution).

I assume that you need different message before and after form displays, depending on what user filled in form. That is on you, your application need to decide which message will be shown.
#4

[eluser]oldblueday[/eluser]
Thanks clooner and pkrstic for your advice. I think I did change the $grading_key to $this->grading_key but it still didn't work between calls, as you noted. I went with using PHP Sessions via the Native Sessions library.

The tests are randomly generated and so, then, is the answer key and this needs to be passed between the two methods. The final exam's was bigger than the 4 kb limit allowed by cookies, so I switched to the Native Sessions library. Why does CI use cookies for sessions instead of real sessions?




Theme © iAndrew 2016 - Forum software by © MyBB