Reply
Need total results from foreach loop
Old 11-02-2009, 10:50 PM Need total results from foreach loop
Average Talker

Posts: 17
Name: Gary
Trades: 0
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 == 
    { 
    
$position 1
    } 
    else 
    { 
    
$position 2
    } 
if (
$position == 
    { 
    
$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);

}

?>
gdaniels is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 11-04-2009, 12:53 PM Re: Need total results from foreach loop
Skilled Talker

Posts: 61
Name: John
Trades: 0
You could use a single QUERY to retrieve all of your data:

SELECT * FROM table WHERE (date BETWEEN start AND end) AND (State = 'SelectState') AND (zip IN (12345,23456,34567));
Envision_frodo is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need total results from foreach loop
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.10784 seconds with 13 queries