Hello. Searching for help on getting total results from a foreach loop. From a search page, users select a State, date range, and multiple zip codes. For each zip code, the following query is run and the results are in $sql_resultstaxpun. The results are from just 1 column, taxpun, which contains only numbers. I wanted to get $sql_resultstaxpun to contain all the results from each time the query is run (a running total). The problem is that it only contains the results from the last time the query is run and I need it to contain all the results. What happens next is $sql_resultstaxpun is used in a function to calculate the median. Any suggestions would be appreciated. Thanks in advance.
PHP Code:
// Make array for median calculation results $medarrtaxpun = array ( );
// select and retreive Median Tax per Unit
foreach ($_POST['selectzip'] as $key => $value) {
if ($value != " ") {
$resultsrchtaxpun = "SELECT taxpun FROM wamuexp where date > '$date1' && date < '$date2' && state = '$selectstate' && zip
LIKE '$value%' ORDER BY taxpun"; $sql_resultstaxpun = mysql_query($resultsrchtaxpun,$dbc);
} }
functionmediantaxpun($sql_resultstaxpun); $medarrtaxpun[ ] = $mediantaxpun;
The following is the include file that calculates the median.
PHP Code:
<?php
// Function median for tax per unit function functionmediantaxpun($sql_resultstaxpun) { global $mediantaxpun; global $medarrtaxpun;
$i=0; while ($row = mysql_fetch_array($sql_resultstaxpun)) { // load array with data from table $test_data[] = $row[taxpun]; $i = $i + 1;
}
// sort the array rsort($test_data);
for ( $count = 0; $count<$i; $count++) //mysql_free_result($sql_resultstaxpun);
$oe_value = count($test_data);
// Total number //echo "$oe_value <br>";
if ($oe_value % 2 == 0 ) { $position = 1; } else { $position = 2; } if ($position == 2 ) { $mediantaxpun = $test_data[$oe_value/2]; } else { $index1 = $oe_value / 2; $index2 = $index1-1; $mediantaxpun = ($test_data[$index1] + $test_data[$index2])/2;
} $mediantaxpun = number_format($mediantaxpun);
}
?>
|