0

I got this code:

 $totalcount = 0;

    function print_results($results) {
        foreach ($results as $id => $data) {
            print_table($data);
        }
    }
    function print_table($data) {

            $totalcount = $totalcount + $data['gq_maxplayers'];
        }


        }
        printf("</tr></tbody>");
    }

But if I use it, $totalcount is being reset every time I try set it. While what I need to do is to add the last $totalcount result with the current one, so if it's on the first one is 25, and the other one is 24, in the end it should be 49.

Ron Melkhior
  • 395
  • 2
  • 3
  • 13

3 Answers3

1

add this line inside your function

global $totalcount;

explanation: http://www.php.net/manual/en/language.variables.scope.php

Can't access global variable inside function

Community
  • 1
  • 1
gaurav5430
  • 9,899
  • 5
  • 32
  • 73
0

$totalcount is local to the function you use it in. You could either pass it as a parameter to the function (and return it to the parent context), make it global, or make it static to persist the value between calls.

Calimero
  • 4,050
  • 1
  • 19
  • 33
0

use global $totalcount; inside the function print_table($data)

Sabuj Hassan
  • 35,286
  • 11
  • 68
  • 78