-2

I want to know if exists a solution to do that:

I need to to use the arithmetic operations inside all cases and at the final to show the last result.

Functions.php

<?php
static $overall;

$overall = 0;

$action = $_GET['action'];

switch ($action) {

case 'result1':

$overall = $overall + 5;

break;

case 'result2':

$overall = $overall + 15;

break;

case 'result3':

$overall = $overall + 2;

break;

case 'finalresult':

echo $overall;

break;

}
?>

I am calling all cases via AJAX and at final I want to call the 'finalresult' case.

George George
  • 187
  • 2
  • 18
  • 2
    So what exactly is the problem? – Mureinik Aug 28 '15 at 22:40
  • are you saying the above code does not work? –  Aug 28 '15 at 22:41
  • Well it doesn't exactly do very much..... are you perhaps confusing `static` with `persistent`? – Mark Baker Aug 28 '15 at 22:42
  • Yes the above code is not working... when I am calling the 'finalresult' case returns 0, because I have on top $overall = 0; – George George Aug 28 '15 at 22:42
  • well yes that's what you tell it to do? so um what do you want? –  Aug 28 '15 at 22:43
  • I need to make the code working, but I cannot understand where is the problem. – George George Aug 28 '15 at 22:44
  • Well that's the expected behavior, static doesn't make the variable persist, it just makes it available without instantiation of a class. The script is being run every time and every time $overall is being reset to 0. You could do this a number of different ways, including a database for persistent data or $_SESSION to store the information. – John Halbert Aug 28 '15 at 22:45
  • Can you provide me a simple example or any related link? – George George Aug 28 '15 at 22:46

2 Answers2

0

When you mark a variable a static, what you're doing is making it available from outside the class without instantiation of the class. For example:

class Test {

static $someVariable = 0;

}

Now you can call that variable like so:

echo Test::$someVariable;

If you made an instance of the class and incremented the variable, then called the variable on the original class, it would remain 0. Like this:

$instance = new Test();
$instance->someVariable++;

// This is now 1
echo $instance->someVariable;

// This remains 0
echo Test::$someVariable;

If you had another variable that wasn't marked static, you couldn't call it from outside the class without instantiation. For example:

class Test {

public $differentVariable = 3;

}

echo Test::$differentVariable;

This will not run and give an error.

The confusion here is in how to use static, and how to make the data in your program persist.

Since you're using this on a website where you're loading this script from another page, every time it's called it's run and $overall is reset to 0. If you want that number to be incremented and persist (retain its value even after the user returns to another page), you have a couple of good options. You can learn about and use $_SESSION. (place session_start(); on each PHP page you wish to have the $_SESSION variable available and then store information in it as you would any other associative array)

You can also learn how to use mysqli or some other database interface. This all depends on your overall program design, as the database information will persist permanently or until manually reset, and the $_SESSION data will live only as long as the cookie exists in the user's browser.

Hope that helps!

John Halbert
  • 1,320
  • 2
  • 12
  • 23
  • Thank you very much for your time, I will try to see if I can found a solution with $_SESSION. I hope I make it works. – George George Aug 28 '15 at 23:10
  • Try this, at the top of that file, put: `session_start();` and then instead of `$overall` use `$_SESSION['overall'];` and increment that value. There are other design concerns here though. Like when do you want that to be reset to 0, if ever? So look further into $_SESSION and think about how you need your program to function. – John Halbert Aug 28 '15 at 23:12
  • I do not want to reset to 0, just the increment on each case. – George George Aug 28 '15 at 23:20
  • The increment is working but need to refresh the page every time to add the increment. – George George Aug 28 '15 at 23:27
  • If you're using AJAX you shouldn't have to. Are you using AJAX provided by jQuery? If so you should be able to take what's returned and update the DOM directly. You can do this without jQuery as well. – John Halbert Aug 28 '15 at 23:31
  • Yes I am using AJAX provided by jQuery. I do not know why it does that... Every time I updated page it's adding increment. – George George Aug 28 '15 at 23:35
  • It's somewhere in your logic. You're calling the ajax request when you're loading the page and you probably want it triggered when the use performs some action. Consider placing it in its own function. – John Halbert Aug 28 '15 at 23:36
-1

what about this

     <?php

      static $overall; 

     $overall = 0; 

    $action = overallResult($_GET['action']);

    echo $action;

    public function overallResult($action)

    { 

   switch ($action) 

   {

   $overall = 0;

   case 'result1':

   $overall = $overall + 5;

   break; 

  case 'result2':

 $overall = $overall + 15;

 break;

 case 'result3':

 $overall = $overall + 2;

 break;

 case 'finalresult': 
 $overall = $overall;
 break;

 }
return $overall ;
    }

  ?>