<?
require ('../config.php');
if($_GET["kon"]==1){

  $kon_res = mysql_query("SELECT * FROM still_konkurs_ref WHERE status='0' and time_start+(time_end*86400) < '".time()."'");
  while ($kr=mysql_fetch_assoc($kon_res)){
    $mest = explode(",",$kr["mesto"]);
    $kon_count = count($mest);

    $top_kon=mysql_query("SELECT idu
    FROM still_top_kon WHERE idk='".$kr["id"]."' ORDER BY danie DESC LIMIT ".$kon_count."");
    $i_top = -1;

    if(mysql_num_rows($top_kon) > 0){
      while ($tk=mysql_fetch_assoc($top_kon)){$i_top++;

        mysql_query("UPDATE `tb_users`
        SET  `money` = `money`+'".$mest[$i_top]."'
        WHERE `id` = '".$tk["idu"]."'");

      }

    }

    mysql_query("UPDATE still_konkurs_ref
    SET status = '1'
    WHERE id='".$kr["id"]."'");

  }

  $kon_del = mysql_query("SELECT id FROM still_konkurs_ref WHERE status = '1' and time_start+((time_end+7)*86400) < '".time()."'");
  while ($kd=mysql_fetch_assoc($kon_del)){

    mysql_query("DELETE
    FROM `still_konkurs_ref`
    WHERE status = '1' and time_start+((time_end+7)*86400) < '".time()."' and id='".$kd["id"]."'");

    mysql_query("DELETE
    FROM `still_top_kon`
    WHERE idk='".$kd["id"]."'");

  }

}


?>