$counthiss = mysql_query("SELECT * FROM history WHERE (`dir` = 1 OR `dir` = 3) $st $end LIMIT 500");
while($counthis = mysql_fetch_array($counthiss)){
if ($counthis[dir]==1){
$testid=0;
$costs = mysql_query("SELECT id, doc_cost FROM history WHERE `posid` = $counthis[posid] AND `dir`=3");
while ($cost = mysql_fetch_array($costs)){
if($testid<$cost[id]){
$testid = $cost[id];
}
}
$cost = mysql_query("SELECT doc_cost FROM history WHERE `id` = $testid");
$cost = mysql_fetch_array($cost);
$uh+=$counthis[count]*$cost[doc_cost];
}
if ($counthis[dir]==3){
$pr+=$counthis[count]*$counthis[doc_cost];
}
}
$counthiss = mysql_query("SELECT * FROM history WHERE (`dir` = 1 OR `dir` = 3) $st $end LIMIT 500");
while($counthis = mysql_fetch_array($counthiss)){
if ($counthis[dir]==1){
$cost = mysql_query("SELECT id, doc_cost FROM history WHERE `posid` = $counthis[posid] AND `dir`=3 ORDER BY id DESC LIMIT 1");
$cost = mysql_fetch_array($cost);
$uh+=$counthis[count]*$cost[doc_cost];
}
if ($counthis[dir]==3){
$pr+=$counthis[count]*$counthis[doc_cost];
}
}