开发者

php counter is being reset each loop

I've got a rather complicated set of loops that pulls data out of mysql and compares it to values in an array and increments a counter. When I echo a flag when the counter is incremented, I get a bijilion flags (there're like 2600 records returned from the mysql query). But each time it prints, the counters are always 1 and when I print the counter's value at the end, it shows up as zero. It seems like something is re-setting the counter…

code

# ARRAY
$demographics=array(
    "region"=>array(
        "Northeast"=>array('total'=>0,'consented'=>0,'completed'=>0),
         //more...
        "West"=>array('total'=>0,'consented'=>0,'completed'=>0)
    ),"societal envirn"=>array(
        "Urban"=>array('total'=>0,'consented'=>0,'completed'=>0)
    ),"age"=>array(
        '18-19'=>array('total'=>0,'consented'=>0,'completed'=>0),
        '20-24'=>array('total'=>0,'consented'=>0,'completed'=>0),
         //more...
        '55-59'=>array('total'=>0,'consented'=>0,'completed'=>0)
    ),
    //more...
);

# LOOPS
while ($dbrecord = mysql_fetch_assoc($surveydata)) {
    foreach ( $dbrecord as $dbfield=>$dbcellval ) {
        foreach ( $demographics as $demographic=>$options ) {
           开发者_Go百科 foreach ( $options as $option=>&$counter ) {
                if($demographic==="age"){
                    list($min,$max) = explode('-', $option);
                    if ($dbcellval >= $min && $dbcellval <= $max){
                        $counter['total']++;
                        echo '$' . $option . "['total'] = " . $counter['total'] . "<br />";
                        if ($dbrecord['consent']==="1"){
                            $counter['consented']++;
                            echo '$' . $option . "['consented'] = " . $counter['consented'] . "<br />";
                            if ($dbrecord['completion status']==="complete") {
                                $counter['completed']++;
                                echo '$' . $option . "['completed'] = " . $counter['completed'] . "<br />";
                                break 3;
                            } // if
                        } // if
                        break 2;
                    }
                } // if age
                else if ($option===$dbcellval){
                    $counter['total']++;
                    echo '$' . $option . "['total'] = " . $counter['total'] . "<br />";
                    if ($dbrecord['consent']==="1"){
                        $counter['consented']++;
                        echo '$' . $option . "['consented'] = " . $counter['consented'] . "<br />";
                        if ($dbrecord['completion status']==="complete") {
                            $counter['completed']++;
                            echo '$' . $option . "['completed'] = " . $counter['completed'] . "<br />";
                            break 3;
                        } // if
                    } // if
                    break 2;
                } // else if $option==$dbcellval
            } // foreach $options
        } // foreach $demographics
    } // foreach $dbrecord
} // while

sample output

$40-44['total'] = 1
$White['total'] = 1
$35-39['total'] = 1
$Northeast['total'] = 1     // the 'total' counter is 1
$Northeast['consented'] = 1
$Northeast['completed'] = 1
$South['total'] = 1
$Northeast['total'] = 1     // notice the 'total' counter is 1 again :(
$Northeast['consented'] = 1
$Northeast['completed'] = 1


You're defining counter from a foreach instruction, as $value in foreach($foo as $key=>$value), when using the foreach you only have a local copy of $counter.

You need to use either foreach($foo as $key=>&$value) or to refer to the full array path of your counter from $demographics.


You need to reference your array at each level, otherwise you are working on a copy of the data:

foreach ( $dbrecord as $dbfield=>$dbcellval ) {
    foreach ( $demographics as $demographic => &$options ) {
        foreach ( $options as $option => &$counter ) {
            if($demographic==="age"){
                list($min,$max) = explode('-', $option);
                if ($dbcellval >= $min && $dbcellval <= $max){
                    $counter['total']++;


What if you simply stick with the $demographics variable.

i.e.

...
foreach ( $options as $option ) {
   ...
   $demographics[$option]['total']++;

...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜