header('Content-Type: text/html; charset=tis-620');
echo "";
include('db_config.php');
mysql_query("TRUNCATE TABLE tmpkap4");
mysql_query("insert into tmpkap4 (SELECT * FROM $_data
where g+s+b > 0 group by kd_sch,kd_code,g,s,b,kd_sp)");
$sql="SELECT *,sum(g) AS go,sum(s) AS sv,sum(b) AS bn
FROM tmpkap4,$_reg where kd_school != '0' and kd_sch=scode
GROUP BY kd_school ORDER BY go desc,sv desc,bn desc";
$result=mysql_query($sql);
echo "ผลการแข่งขัน$_title เรียงตามจำนวนเหรียญรางวัล
";
echo "
ลำดับ
โรงเรียน
1
2
3
ตัวแทน
สพป.
";
$row=1;
while ($db=mysql_fetch_array($result)) {
// $total=$db[go]+$db[sv]+$db[bn];
$sql2="select * from $_reg where scode='$db[kd_sch]' ";
$result2=mysql_query($sql2);
$db2=mysql_fetch_array($result2);
$scm=$db2[scn];
$scd=$db[kd_school];
$sql3="SELECT kd_sch,count(kd_rank) as rk1
FROM $_data WHERE kd_school='$scd' and kd_rank=1
GROUP BY kd_school";
$result3=mysql_query($sql3);
$db3=mysql_fetch_array($result3);
$rnk1=$db3[rk1];
$sql3="SELECT kd_sch,count(kd_rank) as rk2
FROM $_data WHERE kd_school='$scd' and kd_rank=2
GROUP BY kd_school";
$result3=mysql_query($sql3);
$db3=mysql_fetch_array($result3);
$rnk2=$db3[rk2];
$sql3="SELECT kd_sch,count(kd_rank) as rk3
FROM $_data WHERE kd_school='$scd' and kd_rank=3
GROUP BY kd_school";
$result3=mysql_query($sql3);
$db3=mysql_fetch_array($result3);
$rnk3=$db3[rk3];
if ($rnk1+$rnk2+$rnk3>0) {
$trp=$rnk1+$rnk2+$rnk3;
} else {
$trp='-';
}
echo "