Welcome Guest, Not a member yet? Register   Sign In
Controller variable lost after change
#1

[eluser]Keylocker[/eluser]
I have a simple jquery progress bar that should get a variable from Codeigniter controller and print it on screen. The problem is that when a function changes that variable, the result is lost and so the progress bar shows 0.

Here's my code:

Code:
class Emails extends CI_Controller {

public $p;
...

public function test()
{
    for ($i=0; $i < 1000000; $i++)
    {
        $this->setprogress($i);
    }
}

public function setprogress($value)
    {
        $this->p = intval($value);
}

public function progress()
{
    print $this->p;
}

In jquery

Code:
function getProgress(){
    $.get('&lt;?=site_url("/emails/progress"); ?&gt;', function(data) {
        percent = (parseInt(data));
    });
}

$(document).ready(function(){
    $.get('&lt;?=site_url("/emails/test"); ?&gt;', function(parsing) {});
    interval = setInterval(getProgress, 100);
});

The progress variable always comes back empty (NaN). Any suggestions? Thanks a lot guys.
#2

[eluser]InsiteFX[/eluser]
Code:
public static $p;
#3

[eluser]Keylocker[/eluser]
I'm sorry mate, didn't work.
When I print the output from AJAX
Code:
$.get('&lt;?=site_url("/emails/progress"); ?&gt;', function(data) {
  console.log(data);
}

I get this

Code:
<div  solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined property: Emails::$progress</p>
<p>Filename: controllers/emails.php</p>
<p>Line Number: 659</p>

</div>

The line 659 of course is the line that outputs the data in the progress() function
#4

[eluser]Keylocker[/eluser]
Solved using session variable.

Here's the correct code
Code:
public function __construct()
{
    parent::__construct();
    $this->load->library('session');
}

public function test()
{
    for ($i=0; $i < 100; $i++) {
        $this->setprogress($i);
    }
}

function setprogress($value)
{
    $this->session->set_userdata('progress', intval($value));
}

public function progress()
{
    print $this->session->userdata('progress');
}
#5

[eluser]InsiteFX[/eluser]
PHP Static Variables
#6

[eluser]Keylocker[/eluser]
[quote author="InsiteFX" date="1389223104"]PHP Static Variables[/quote]

The function didn't set nor return the variable, actually don't know why.
My workaround might not be beautiful, I admit, but it got the job done Smile
Client happy = everybody happy Wink

Thanks anyway for your hints mate




Theme © iAndrew 2016 - Forum software by © MyBB