http://www.w3school.com.cn/php/index.asp
这里的函数查询很好
array相关函数str相关函数很有用~
PHP5.1以上时区需要设置:
ini_set('date.timezone','Asia/Shanghai');
<?php
include_once "../conn.php";
$cid = $_GET['cid'];
if (db_contest_exist($cid)) {
ini_set('date.timezone','Asia/Shanghai');
$nowtime=time();
list($locktu,$sttimeu,$fitimeu,$t) = @mysql_fetch_array(mysql_query("SELECT unix_timestamp(lock_board_time),unix_timestamp(start_time),unix_timestamp(end_time),unix_timestamp(board_make) FROM contest WHERE cid = '$cid'"));
$scr = "contest_standing_".$cid.".html";
$pastsec=$nowtime-$t;
if ($pastsec<$srefresh&&!($nowtime>$locktu&&$t<$locktu)&&!($nowtime>$fitimeu&&$t<$fitimeu))//60秒更新一次
{
Header("Location: contest_standing_".$cid.".html");
}
else{
$maketime=date("Y-m-d G:i:s",$nowtime);
$sql_update = "update contest set board_make='$maketime' where cid = '$cid'";
$que_update = mysql_query($sql_update);
ob_start(); //打开缓冲区
include("cheader.php");
include("cmenu.php");
function get_time ($unix_time) {
$first = floor($unix_time/3600);
$mid = floor( ($unix_time-$first*3600)/60 );
$last = $unix_time%60;
return $first.":".$mid.":".$last;
}
// $cid = $_GET['cid'];
$num_of_problem = 0; //题目个数
$sql = " SELECT * FROM `contest_problem` WHERE `cid` = ".$cid; //创建label和题目的对应
$res = mysql_query($sql);//执行mysql查询
$map2 = array();
while($row = mysql_fetch_array($res)){
$map[$row[pid]] =$row[lable];
$map2[$row[lable]] = $row[cpid];
$num_of_problem++;
}
//print_r($map);
$sql = "SELECT * FROM `contest` WHERE `cid` =".$cid;
$res = mysql_query($sql);//执行mysql查询
$info_of_contest = mysql_fetch_array($res);
/*
* 比赛信息
* [0] =>[cid]
* [1] =>[title]
* [2] =>[description]
* [3] =>[isprivate]
* [4] =>[start_time]
* [5] =>[end_time]
* [6] =>[lock_board_time]
* [7] =>[hide_others]
* [8] => [board_make]
* [9] =>[isvirtual]
* [10] => [owner]
* [11] => [report]
* */
$basetime = strtotime($info_of_contest[4]);
// 查询 并存入二维表 OK
// 联合查询,带出名字
$sql = " SELECT status.pid,status.result,status.time_submit,status.username,user.nickname FROM status,user WHERE `status`.`contest_belong` =".$cid." AND status.username=user.username" ;
$res = mysql_query($sql);//执行mysql查询
$ary = array();//初始化二维表
while($row = mysql_fetch_array($res) ){
$id = array_push($ary,$row); //$id 为行数
}//将查询结果存入
// 扫描一遍查询结果 生成名称序二维表
$Name_ary = array(); //初始化名称序二维表
for($i = 0 ; $i < $id ; $i++){
$k = 0;
$get = 0;
for($j = 0 ; $j < $iid ; $j++){
if( in_array($ary[$i][username],$Name_ary[$j],true) ){
$k = 1;
$get = $j;
break;
}
}
if($k == 1){
//echo "Yes<br/>";
if($ary[$i][result] == "Accepted" ){
if( $Name_ary[$get][$map[$ary[$i][pid]]]!=-1 ){
continue;
}
else{
$Name_ary[$get][$map[$ary[$i][pid]]] = strtotime($ary[$i][time_submit])-$basetime;
$Name_ary[$get][5] += strtotime($ary[$i][time_submit])-$basetime;
}
}
else{
if( $Name_ary[$get][$map[$ary[$i][pid]]] == -1 ){
$Name_ary[$get][$map[$ary[$i][pid]]._wci]--;
}
}
}
else{
//echo "No<br/>";
$iid = array_push($Name_ary,$ary[$i]);
if($ary[$i][result] == "Accepted" ){
array_push($Name_ary[$iid-1],strtotime($ary[$i][time_submit])-$basetime);
foreach ($map as $value){
$Name_ary[$iid-1][$value] = -1;
$Name_ary[$iid-1][$value._wci] = 0;
}
$Name_ary[$iid-1][$map[$ary[$i][pid]]] = strtotime($ary[$i][time_submit])-$basetime;
$Name_ary[$iid-1][$map[$ary[$i][pid]]._wci] = 0;
}
else{
array_push($Name_ary[$iid-1],0);
foreach ($map as $value){
$Name_ary[$iid-1][$value] = -1;
$Name_ary[$iid-1][$value._wci] = 0;
}
$Name_ary[$iid-1][$map[$ary[$i][pid]]] = -1;
$Name_ary[$iid-1][$map[$ary[$i][pid]]._wci] = -1;
}
}
}
// 扫描计算罚时与题数 然后排序
for($i = 0 ; $i < $iid ; $i++){
$fs = 0;
$Name_ary[$i][sum] = 0;
foreach ($map as $value){
if( $Name_ary[$i][$value]!=-1 ){
$Name_ary[$i][sum]++;
$fs -= 20*60*$Name_ary[$i][$value._wci];
}
}
$Name_ary[$i][5] += $fs;
}
for($i = 0 ; $i < $iid ; $i++ ){
for($j = 0 ; $j < $iid ; $j++ ){
if($Name_ary[$i][sum] > $Name_ary[$j][sum] ){
$temp = array();
$temp = $Name_ary[$i];
$Name_ary[$i] = $Name_ary[$j];
$Name_ary[$j] = $temp;
}
else if($Name_ary[$i][sum] == $Name_ary[$j][sum]){
if($Name_ary[$i][5]<$Name_ary[$j][5]){
$temp = array();
$temp = $Name_ary[$i];
$Name_ary[$i] = $Name_ary[$j];
$Name_ary[$j] = $temp;
}
}
}
}
// 显示
echo "<table width='98%'>";
echo "<tr>";
echo //"<td>pid</td>" . //pid
// "<td>result</td>" . //result
// "<td>time_submit</td>".//time_submit
"<th>Rank</th>".//username
"<th>Nickname</th>".//nickname
"<th>Accepts</th>";//nickname
foreach ($map as $value)
{
echo "<th><a href=../contest_problem_show.php?cpid=".$map2[$value].">".$value."</a></th>";
}
echo "<th>Penalty</th>";//Penalty
echo "</tr>";
for($i = 0 ; $i < $iid ; $i++){
// print_r($Name_ary[$i]);
$nick = strip_tags(change_out($Name_ary[$i][4]));
if($nick == '') {
$nick = "你太懒了";
}
else if(strlen($nick)>21 ){
$nick = substr($nick,0,21)."";
}
echo "<tr>";
echo //"<th>".$Name_ary[$i][0]."</th>" . //pid
// "<th>".$Name_ary[$i][1]."</th>" . //result
// "<th>".$Name_ary[$i][2]."</th>".//time_submit
"<th>".($i+1)."</th>".//username
"<th><a alt='".$Name_ary[$i][username]."' href=../userinfo.php?name=".$Name_ary[$i][username].">".$nick."</a></th>".//nickname
"<th>".$Name_ary[$i][sum]."</th>";//ac_num
foreach ($map as $value)
{
if($Name_ary[$i][$value] != -1 && array_key_exists($value,$Name_ary[$i]))
echo "<th>".get_time($Name_ary[$i][$value])."(".$Name_ary[$i][$value._wci].")</th>";
else if($Name_ary[$i][$value._wci])
echo "<th>(".$Name_ary[$i][$value._wci].")</th>";
else{
echo "<th></th>";
}
}
echo "<th>".get_time($Name_ary[$i][5])."</th>";//Penalty
echo "</tr>";
}
echo "</table>";
include("footer.php");
$content=ob_get_contents(); //得到缓冲区的内容
if (!function_exists("file_put_contents"))
{
function file_put_contents($fn,$fs)
{
$fp=fopen($fn,"w+");
fputs($fp,$fs);
fclose($fp);
}
}
file_put_contents("contest_standing_".$cid.".html",$content);
}
}
else {
include("cheader.php");
include("cmenu.php");
echo "<center><p class=warn>Invalid Contest!</p></center>";
include("footer.php");
}
?>