<?php
/**
 * Make table functions
 *
 * Contains globally available tool functions. It is included in every
 * page and sets up some defaults like error reporting, environment
 * setups and config loading
 *
 *
 * @package Core
 * @author  Eric GALLENE <okrome@free.fr>
 * @version $Id: tables.php,v 1.00 2006/10/23 18:15:53
 */

/**
 * Return UserId to a given UserName
 *
 * @author Chinamann <chinamann@users.sourceforge.net>
 * @param   string  $userName user name
 * @result  integer user id
 */
function maketable($mycompet, $myday, $myseason)
{

$SELECT = 'SELECT * from '.TBL_PARAMS.' WHERE id_season='.$myseason.' AND id_compet='.$mycompet;
$params   = runSQL($SELECT);

$SELECT = 'SELECT id_club, penality from '.TBL_CANDIDATES.' WHERE id_season='.$myseason.' AND id_compet='.$mycompet;
$penalitys   = runSQL($SELECT);
foreach ($penalitys as $pen) {
	$penality[$pen['id_club']]['id'] = $pen['id_club'];
	$penality[$pen['id_club']]['penality'] = $pen['penality'];
}

// Domicile victory
$SELECT = 'SELECT '.TBL_CLUBS.'.id, count('.TBL_MATCHS.'.id_candidate_dom) as countmatch, sum('.TBL_MATCHS.'.buts_dom) as BP, sum('.TBL_MATCHS.'.buts_ext) as BC
FROM '.TBL_MATCHS.'
JOIN '.TBL_CLUBS.' ON '.TBL_CLUBS.'.id = '.TBL_MATCHS.'.id_candidate_dom AND '.TBL_MATCHS.'.validation > 0
JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
JOIN '.TBL_SEASONS.' ON '.TBL_DAYS.'.id_season = '.TBL_SEASONS.'.id
WHERE '.TBL_MATCHS.'.buts_dom > '.TBL_MATCHS.'.buts_ext AND '.TBL_MATCHS.'.extendtime = 0 AND '.TBL_DAYS.'.id_season='.$myseason.' AND '.TBL_DAYS.'.id_compet = '.$mycompet.' AND '.TBL_DAYS.'.day BETWEEN 1 AND '.$myday.' AND '.TBL_MATCHS.'.date_day <= NOW()
GROUP BY '.TBL_MATCHS.'.id_candidate_dom'; 
$domvictory   = runSQL($SELECT);

// Domicile nul
$SELECT = 'SELECT '.TBL_CLUBS.'.id, count('.TBL_MATCHS.'.id_candidate_dom) as countmatch, sum('.TBL_MATCHS.'.buts_dom) as BP, sum('.TBL_MATCHS.'.buts_ext) as BC
FROM '.TBL_MATCHS.'
JOIN '.TBL_CLUBS.' ON '.TBL_CLUBS.'.id = '.TBL_MATCHS.'.id_candidate_dom AND '.TBL_MATCHS.'.validation > 0
JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
JOIN '.TBL_SEASONS.' ON '.TBL_DAYS.'.id_season = '.TBL_SEASONS.'.id
WHERE '.TBL_MATCHS.'.buts_dom = '.TBL_MATCHS.'.buts_ext AND '.TBL_MATCHS.'.extendtime = 0 AND '.TBL_DAYS.'.id_season='.$myseason.' AND '.TBL_DAYS.'.id_compet = '.$mycompet.' AND '.TBL_DAYS.'.day BETWEEN 1 AND '.$myday.' AND '.TBL_MATCHS.'.date_day <= NOW()
GROUP BY '.TBL_MATCHS.'.id_candidate_dom';  
$domnul   = runSQL($SELECT);    

// Domicile defeat
$SELECT = 'SELECT '.TBL_CLUBS.'.id, count('.TBL_MATCHS.'.id_candidate_dom) as countmatch, sum('.TBL_MATCHS.'.buts_dom) as BP, sum('.TBL_MATCHS.'.buts_ext) as BC
FROM '.TBL_MATCHS.'
JOIN '.TBL_CLUBS.' ON '.TBL_CLUBS.'.id = '.TBL_MATCHS.'.id_candidate_dom AND '.TBL_MATCHS.'.validation > 0
JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
JOIN '.TBL_SEASONS.' ON '.TBL_DAYS.'.id_season = '.TBL_SEASONS.'.id
WHERE '.TBL_MATCHS.'.buts_dom < '.TBL_MATCHS.'.buts_ext AND '.TBL_MATCHS.'.extendtime = 0 AND '.TBL_DAYS.'.id_season='.$myseason.' AND '.TBL_DAYS.'.id_compet = '.$mycompet.' AND '.TBL_DAYS.'.day BETWEEN 1 AND '.$myday.' AND '.TBL_MATCHS.'.date_day <= NOW()
GROUP BY '.TBL_MATCHS.'.id_candidate_dom'; 
$domdefeat   = runSQL($SELECT); 


// Exterior victory
$SELECT = 'SELECT '.TBL_CLUBS.'.id, count('.TBL_MATCHS.'.id_candidate_ext) as countmatch, sum('.TBL_MATCHS.'.buts_ext) as BP, sum('.TBL_MATCHS.'.buts_dom) as BC
FROM '.TBL_MATCHS.'
JOIN '.TBL_CLUBS.' ON '.TBL_CLUBS.'.id = '.TBL_MATCHS.'.id_candidate_ext AND '.TBL_MATCHS.'.validation > 0
JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
JOIN '.TBL_SEASONS.' ON '.TBL_DAYS.'.id_season = '.TBL_SEASONS.'.id
WHERE '.TBL_MATCHS.'.buts_ext > '.TBL_MATCHS.'.buts_dom AND '.TBL_MATCHS.'.extendtime = 0 AND '.TBL_DAYS.'.id_season='.$myseason.' AND '.TBL_DAYS.'.id_compet = '.$mycompet.' AND '.TBL_DAYS.'.day BETWEEN 1 AND '.$myday.' AND '.TBL_MATCHS.'.date_day <= NOW()
GROUP BY '.TBL_MATCHS.'.id_candidate_ext'; 
$extvictory   = runSQL($SELECT);

// Exterior nul
$SELECT = 'SELECT '.TBL_CLUBS.'.id, count('.TBL_MATCHS.'.id_candidate_ext) as countmatch, sum('.TBL_MATCHS.'.buts_ext) as BP, sum('.TBL_MATCHS.'.buts_dom) as BC
FROM '.TBL_MATCHS.'
JOIN '.TBL_CLUBS.' ON '.TBL_CLUBS.'.id = '.TBL_MATCHS.'.id_candidate_ext AND '.TBL_MATCHS.'.validation > 0
JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
JOIN '.TBL_SEASONS.' ON '.TBL_DAYS.'.id_season = '.TBL_SEASONS.'.id
WHERE '.TBL_MATCHS.'.buts_ext = '.TBL_MATCHS.'.buts_dom AND '.TBL_MATCHS.'.extendtime = 0 AND '.TBL_DAYS.'.id_season='.$myseason.' AND '.TBL_DAYS.'.id_compet = '.$mycompet.' AND '.TBL_DAYS.'.day BETWEEN 1 AND '.$myday.' AND '.TBL_MATCHS.'.date_day <= NOW()
GROUP BY '.TBL_MATCHS.'.id_candidate_ext'; 
$extnul   = runSQL($SELECT);    

// Exterior defeat
$SELECT = 'SELECT '.TBL_CLUBS.'.id, count('.TBL_MATCHS.'.id_candidate_ext) as countmatch, sum('.TBL_MATCHS.'.buts_ext) as BP, sum('.TBL_MATCHS.'.buts_dom) as BC
FROM '.TBL_MATCHS.'
JOIN '.TBL_CLUBS.' ON '.TBL_CLUBS.'.id = '.TBL_MATCHS.'.id_candidate_ext AND '.TBL_MATCHS.'.validation > 0
JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
JOIN '.TBL_SEASONS.' ON '.TBL_DAYS.'.id_season = '.TBL_SEASONS.'.id
WHERE '.TBL_MATCHS.'.buts_ext < '.TBL_MATCHS.'.buts_dom AND '.TBL_MATCHS.'.extendtime = 0 AND '.TBL_DAYS.'.id_season='.$myseason.' AND '.TBL_DAYS.'.id_compet = '.$mycompet.' AND '.TBL_DAYS.'.day BETWEEN 1 AND '.$myday.' AND '.TBL_MATCHS.'.date_day <= NOW()
GROUP BY '.TBL_MATCHS.'.id_candidate_ext'; 
$extdefeat   = runSQL($SELECT); 


// Domicile victory in extra time
$SELECT = 'SELECT '.TBL_CLUBS.'.id, count('.TBL_MATCHS.'.id_candidate_dom) as countmatch, sum('.TBL_MATCHS.'.buts_dom) as BP, sum('.TBL_MATCHS.'.buts_ext) as BC
FROM '.TBL_MATCHS.'
JOIN '.TBL_CLUBS.' ON '.TBL_CLUBS.'.id = '.TBL_MATCHS.'.id_candidate_dom AND '.TBL_MATCHS.'.validation > 0
JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
JOIN '.TBL_SEASONS.' ON '.TBL_DAYS.'.id_season = '.TBL_SEASONS.'.id
WHERE '.TBL_MATCHS.'.buts_dom > '.TBL_MATCHS.'.buts_ext AND '.TBL_MATCHS.'.extendtime = 1 AND '.TBL_DAYS.'.id_season='.$myseason.' AND '.TBL_DAYS.'.id_compet = '.$mycompet.' AND '.TBL_DAYS.'.day BETWEEN 1 AND '.$myday.' AND '.TBL_MATCHS.'.date_day <= NOW()
GROUP BY '.TBL_MATCHS.'.id_candidate_dom'; 
$dominextvictory   = runSQL($SELECT);

// Domicile nul in extra time
$SELECT = 'SELECT '.TBL_CLUBS.'.id, count('.TBL_MATCHS.'.id_candidate_dom) as countmatch, sum('.TBL_MATCHS.'.buts_dom) as BP, sum('.TBL_MATCHS.'.buts_ext) as BC
FROM '.TBL_MATCHS.'
JOIN '.TBL_CLUBS.' ON '.TBL_CLUBS.'.id = '.TBL_MATCHS.'.id_candidate_dom AND '.TBL_MATCHS.'.validation > 0
JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
JOIN '.TBL_SEASONS.' ON '.TBL_DAYS.'.id_season = '.TBL_SEASONS.'.id
WHERE '.TBL_MATCHS.'.buts_dom = '.TBL_MATCHS.'.buts_ext AND '.TBL_MATCHS.'.extendtime = 1 AND '.TBL_DAYS.'.id_season='.$myseason.' AND '.TBL_DAYS.'.id_compet = '.$mycompet.' AND '.TBL_DAYS.'.day BETWEEN 1 AND '.$myday.' AND '.TBL_MATCHS.'.date_day <= NOW()
GROUP BY '.TBL_MATCHS.'.id_candidate_dom';  
$dominextnul   = runSQL($SELECT);    

// Domicile defeat in extra time
$SELECT = 'SELECT '.TBL_CLUBS.'.id, count('.TBL_MATCHS.'.id_candidate_dom) as countmatch, sum('.TBL_MATCHS.'.buts_dom) as BP, sum('.TBL_MATCHS.'.buts_ext) as BC
FROM '.TBL_MATCHS.'
JOIN '.TBL_CLUBS.' ON '.TBL_CLUBS.'.id = '.TBL_MATCHS.'.id_candidate_dom AND '.TBL_MATCHS.'.validation > 0
JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
JOIN '.TBL_SEASONS.' ON '.TBL_DAYS.'.id_season = '.TBL_SEASONS.'.id
WHERE '.TBL_MATCHS.'.buts_dom < '.TBL_MATCHS.'.buts_ext AND '.TBL_MATCHS.'.extendtime = 1 AND '.TBL_DAYS.'.id_season='.$myseason.' AND '.TBL_DAYS.'.id_compet = '.$mycompet.' AND '.TBL_DAYS.'.day BETWEEN 1 AND '.$myday.' AND '.TBL_MATCHS.'.date_day <= NOW()
GROUP BY '.TBL_MATCHS.'.id_candidate_dom'; 
$dominextdefeat   = runSQL($SELECT); 


// Exterior victory in extra time
$SELECT = 'SELECT '.TBL_CLUBS.'.id, count('.TBL_MATCHS.'.id_candidate_ext) as countmatch, sum('.TBL_MATCHS.'.buts_ext) as BP, sum('.TBL_MATCHS.'.buts_dom) as BC
FROM '.TBL_MATCHS.'
JOIN '.TBL_CLUBS.' ON '.TBL_CLUBS.'.id = '.TBL_MATCHS.'.id_candidate_ext AND '.TBL_MATCHS.'.validation > 0
JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
JOIN '.TBL_SEASONS.' ON '.TBL_DAYS.'.id_season = '.TBL_SEASONS.'.id
WHERE '.TBL_MATCHS.'.buts_ext > '.TBL_MATCHS.'.buts_dom AND '.TBL_MATCHS.'.extendtime = 1 AND '.TBL_DAYS.'.id_season='.$myseason.' AND '.TBL_DAYS.'.id_compet = '.$mycompet.' AND '.TBL_DAYS.'.day BETWEEN 1 AND '.$myday.' AND '.TBL_MATCHS.'.date_day <= NOW()
GROUP BY '.TBL_MATCHS.'.id_candidate_ext'; 
$extinextvictory   = runSQL($SELECT);

// Exterior nul in extra time
$SELECT = 'SELECT '.TBL_CLUBS.'.id, count('.TBL_MATCHS.'.id_candidate_ext) as countmatch, sum('.TBL_MATCHS.'.buts_ext) as BP, sum('.TBL_MATCHS.'.buts_dom) as BC
FROM '.TBL_MATCHS.'
JOIN '.TBL_CLUBS.' ON '.TBL_CLUBS.'.id = '.TBL_MATCHS.'.id_candidate_ext AND '.TBL_MATCHS.'.validation > 0
JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
JOIN '.TBL_SEASONS.' ON '.TBL_DAYS.'.id_season = '.TBL_SEASONS.'.id
WHERE '.TBL_MATCHS.'.buts_ext = '.TBL_MATCHS.'.buts_dom AND '.TBL_MATCHS.'.extendtime = 1 AND '.TBL_DAYS.'.id_season='.$myseason.' AND '.TBL_DAYS.'.id_compet = '.$mycompet.' AND '.TBL_DAYS.'.day BETWEEN 1 AND '.$myday.' AND '.TBL_MATCHS.'.date_day <= NOW()
GROUP BY '.TBL_MATCHS.'.id_candidate_ext'; 
$extinextnul   = runSQL($SELECT);    

// Exterior defeat in extra time
$SELECT = 'SELECT '.TBL_CLUBS.'.id, count('.TBL_MATCHS.'.id_candidate_ext) as countmatch, sum('.TBL_MATCHS.'.buts_ext) as BP, sum('.TBL_MATCHS.'.buts_dom) as BC
FROM '.TBL_MATCHS.'
JOIN '.TBL_CLUBS.' ON '.TBL_CLUBS.'.id = '.TBL_MATCHS.'.id_candidate_ext AND '.TBL_MATCHS.'.validation > 0
JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
JOIN '.TBL_SEASONS.' ON '.TBL_DAYS.'.id_season = '.TBL_SEASONS.'.id
WHERE '.TBL_MATCHS.'.buts_ext < '.TBL_MATCHS.'.buts_dom AND '.TBL_MATCHS.'.extendtime = 1 AND '.TBL_DAYS.'.id_season='.$myseason.' AND '.TBL_DAYS.'.id_compet = '.$mycompet.' AND '.TBL_DAYS.'.day BETWEEN 1 AND '.$myday.' AND '.TBL_MATCHS.'.date_day <= NOW()
GROUP BY '.TBL_MATCHS.'.id_candidate_ext'; 
$extinextdefeat   = runSQL($SELECT); 





	foreach ($domvictory as $result) {
		//if ($result['validation'] > 0) {
		$clubstats[$result['id']]['id'] = $result['id'];
		$clubstats[$result['id']]['points'] = ($result['countmatch'] * $params[0]['dom_vic']);
		$clubstats[$result['id']]['nummatch'] = $result['countmatch'];
		$clubstats[$result['id']]['victory']= $result['countmatch'];
		$clubstats[$result['id']]['bp']= $result['BP'];
		$clubstats[$result['id']]['bc']= $result['BC'];
		$clubstats[$result['id']]['validation']= $result['validation'];
		$clubstats[$result['id']]['dompoints'] = ($result['countmatch'] * $params[0]['dom_vic']);
		$clubstats[$result['id']]['domnummatch'] = $result['countmatch'];
		$clubstats[$result['id']]['domvictory']= $result['countmatch'];
		$clubstats[$result['id']]['dombp']= $result['BP'];
		$clubstats[$result['id']]['dombc']= $result['BC'];
		//}
	}
	
	foreach ($domnul as $result) {
		//if ($result['validation'] > 0) {
		$clubstats[$result['id']]['id'] = $result['id'];
		$clubstats[$result['id']]['points'] += ($result['countmatch'] * $params[0]['dom_nul']);
		$clubstats[$result['id']]['nummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['nul']= $result['countmatch'];
		$clubstats[$result['id']]['bp'] += $result['BP'];
		$clubstats[$result['id']]['bc'] += $result['BC'];
		$clubstats[$result['id']]['validation']= $result['validation'];
		$clubstats[$result['id']]['dompoints'] += ($result['countmatch'] * $params[0]['dom_nul']);
		$clubstats[$result['id']]['domnummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['domnul']= $result['countmatch'];
		$clubstats[$result['id']]['dombp'] += $result['BP'];
		$clubstats[$result['id']]['dombc'] += $result['BC'];
		//}
	}
	
	foreach ($domdefeat as $result) {
		//if ($result['validation'] > 0) {
		$clubstats[$result['id']]['id'] = $result['id'];
		$clubstats[$result['id']]['points'] += ($result['countmatch'] * $params[0]['dom_def']);
		$clubstats[$result['id']]['nummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['lost']= $result['countmatch'];
		$clubstats[$result['id']]['bp'] += $result['BP'];
		$clubstats[$result['id']]['bc'] += $result['BC'];
		$clubstats[$result['id']]['validation']= $result['validation'];
		$clubstats[$result['id']]['dompoints'] += ($result['countmatch'] * $params[0]['dom_def']);
		$clubstats[$result['id']]['domnummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['domlost']= $result['countmatch'];
		$clubstats[$result['id']]['dombp'] += $result['BP'];
		$clubstats[$result['id']]['dombc'] += $result['BC'];
		//}
	}
	
	foreach ($extvictory as $result) {
		//if ($result['validation'] > 0) {
		$clubstats[$result['id']]['id'] = $result['id'];
		$clubstats[$result['id']]['points'] += ($result['countmatch'] * $params[0]['ext_vic']);
		$clubstats[$result['id']]['nummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['victory']+= $result['countmatch'];
		$clubstats[$result['id']]['bp'] += $result['BP'];
		$clubstats[$result['id']]['bc'] += $result['BC'];
		$clubstats[$result['id']]['validation']= $result['validation'];
		$clubstats[$result['id']]['extpoints'] = ($result['countmatch'] * $params[0]['ext_vic']);
		$clubstats[$result['id']]['extnummatch'] = $result['countmatch'];
		$clubstats[$result['id']]['extvictory'] = $result['countmatch'];
		$clubstats[$result['id']]['extbp'] = $result['BP'];
		$clubstats[$result['id']]['extbc'] = $result['BC'];
		//}
	}
	
	
	foreach ($extnul as $result) {
		//if ($result['validation'] > 0) {
		$clubstats[$result['id']]['id'] = $result['id'];
		$clubstats[$result['id']]['points'] += ($result['countmatch'] * $params[0]['ext_nul']);
		$clubstats[$result['id']]['nummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['nul'] += $result['countmatch'];
		$clubstats[$result['id']]['bp'] += $result['BP'];
		$clubstats[$result['id']]['bc'] += $result['BC'];
		$clubstats[$result['id']]['validation']= $result['validation'];
		$clubstats[$result['id']]['extpoints'] += ($result['countmatch'] * $params[0]['ext_nul']);
		$clubstats[$result['id']]['extnummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['extnul'] = $result['countmatch'];
		$clubstats[$result['id']]['extbp'] += $result['BP'];
		$clubstats[$result['id']]['extbc'] += $result['BC'];
		//}
	}
	
	foreach ($extdefeat as $result) {
		//if ($result['validation'] > 0) {
		$clubstats[$result['id']]['id'] = $result['id'];
		$clubstats[$result['id']]['points'] += ($result['countmatch'] * $params[0]['ext_def']);
		$clubstats[$result['id']]['nummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['lost']+= $result['countmatch'];
		$clubstats[$result['id']]['bp'] += $result['BP'];
		$clubstats[$result['id']]['bc'] += $result['BC'];
		$clubstats[$result['id']]['validation']= $result['validation'];
		$clubstats[$result['id']]['extpoints'] += ($result['countmatch'] * $params[0]['ext_def']);
		$clubstats[$result['id']]['extnummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['extlost'] = $result['countmatch'];
		$clubstats[$result['id']]['extbp'] += $result['BP'];
		$clubstats[$result['id']]['extbc'] += $result['BC'];
		//}
	}


//Extend time

	foreach ($dominextvictory as $result) {
		//if ($result['validation'] > 0) {
		$clubstats[$result['id']]['id'] = $result['id'];
		$clubstats[$result['id']]['points'] += ($result['countmatch'] * $params[0]['dom_vic_ext']);
		$clubstats[$result['id']]['nummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['victory']+= $result['countmatch'];
		$clubstats[$result['id']]['bp']+= $result['BP'];
		$clubstats[$result['id']]['bc']+= $result['BC'];
		$clubstats[$result['id']]['validation']= $result['validation'];
		$clubstats[$result['id']]['dompoints'] += ($result['countmatch'] * $params[0]['dom_vic_ext']);
		$clubstats[$result['id']]['domnummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['domvictory']+= $result['countmatch'];
		$clubstats[$result['id']]['dombp']+= $result['BP'];
		$clubstats[$result['id']]['dombc']+= $result['BC'];
		//}
	}
	
	foreach ($dominextnul as $result) {
		//if ($result['validation'] > 0) {
		$clubstats[$result['id']]['id'] = $result['id'];
		$clubstats[$result['id']]['points'] += ($result['countmatch'] * $params[0]['dom_nul_ext']);
		$clubstats[$result['id']]['nummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['nul']+= $result['countmatch'];
		$clubstats[$result['id']]['bp'] += $result['BP'];
		$clubstats[$result['id']]['bc'] += $result['BC'];
		$clubstats[$result['id']]['validation']= $result['validation'];
		$clubstats[$result['id']]['dompoints'] += ($result['countmatch'] * $params[0]['dom_nul_ext']);
		$clubstats[$result['id']]['domnummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['domnul']+= $result['countmatch'];
		$clubstats[$result['id']]['dombp'] += $result['BP'];
		$clubstats[$result['id']]['dombc'] += $result['BC'];
		//}
	}
	
	foreach ($dominextdefeat as $result) {
		//if ($result['validation'] > 0) {
		$clubstats[$result['id']]['id'] = $result['id'];
		$clubstats[$result['id']]['points'] += ($result['countmatch'] * $params[0]['dom_def_ext']);
		$clubstats[$result['id']]['nummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['lost']+= $result['countmatch'];
		$clubstats[$result['id']]['bp'] += $result['BP'];
		$clubstats[$result['id']]['bc'] += $result['BC'];
		$clubstats[$result['id']]['validation']= $result['validation'];
		$clubstats[$result['id']]['dompoints'] += ($result['countmatch'] * $params[0]['dom_def_ext']);
		$clubstats[$result['id']]['domnummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['domlost']+= $result['countmatch'];
		$clubstats[$result['id']]['dombp'] += $result['BP'];
		$clubstats[$result['id']]['dombc'] += $result['BC'];
		//}
	}
	
	foreach ($extinextvictory as $result) {
		//if ($result['validation'] > 0) {
		$clubstats[$result['id']]['id'] = $result['id'];
		$clubstats[$result['id']]['points'] += ($result['countmatch'] * $params[0]['ext_vic_ext']);
		$clubstats[$result['id']]['nummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['victory']+= $result['countmatch'];
		$clubstats[$result['id']]['bp'] += $result['BP'];
		$clubstats[$result['id']]['bc'] += $result['BC'];
		$clubstats[$result['id']]['validation']= $result['validation'];
		$clubstats[$result['id']]['extpoints'] += ($result['countmatch'] * $params[0]['ext_vic_ext']);
		$clubstats[$result['id']]['extnummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['extvictory'] += $result['countmatch'];
		$clubstats[$result['id']]['extbp'] += $result['BP'];
		$clubstats[$result['id']]['extbc'] += $result['BC'];
		//}
	}
	
	
	foreach ($extinextnul as $result) {
		//if ($result['validation'] > 0) {
		$clubstats[$result['id']]['id'] = $result['id'];
		$clubstats[$result['id']]['points'] += ($result['countmatch'] * $params[0]['ext_nul_ext']);
		$clubstats[$result['id']]['nummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['nul'] += $result['countmatch'];
		$clubstats[$result['id']]['bp'] += $result['BP'];
		$clubstats[$result['id']]['bc'] += $result['BC'];
		$clubstats[$result['id']]['validation']= $result['validation'];
		$clubstats[$result['id']]['extpoints'] += ($result['countmatch'] * $params[0]['ext_nul_ext']);
		$clubstats[$result['id']]['extnummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['extnul'] += $result['countmatch'];
		$clubstats[$result['id']]['extbp'] += $result['BP'];
		$clubstats[$result['id']]['extbc'] += $result['BC'];
		//}
	}
	
	foreach ($extinextdefeat as $result) {
		//if ($result['validation'] > 0) {
		$clubstats[$result['id']]['id'] = $result['id'];
		$clubstats[$result['id']]['points'] += ($result['countmatch'] * $params[0]['ext_def_ext']);
		$clubstats[$result['id']]['nummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['lost']+= $result['countmatch'];
		$clubstats[$result['id']]['bp'] += $result['BP'];
		$clubstats[$result['id']]['bc'] += $result['BC'];
		$clubstats[$result['id']]['validation']= $result['validation'];
		$clubstats[$result['id']]['extpoints'] += ($result['countmatch'] * $params[0]['ext_def_ext']);
		$clubstats[$result['id']]['extnummatch'] += $result['countmatch'];
		$clubstats[$result['id']]['extlost'] += $result['countmatch'];
		$clubstats[$result['id']]['extbp'] += $result['BP'];
		$clubstats[$result['id']]['extbc'] += $result['BC'];
		//}
	}

	


	$DELETE = "DELETE FROM ".TBL_CLMNT." WHERE id_compet=".$mycompet." AND id_season=".$myseason." AND id_day=".$myday;
	$id = runSQL($DELETE);
	
	foreach ($clubstats as $clubdata) {
			$clubdata['points'] = $clubdata['points'] + $penality[$clubdata['id']]['penality'];
			$INSERT = "INSERT INTO ".TBL_CLMNT." SET id_compet=".$mycompet.", id_season=".$myseason.", id_day=".$myday.", id_team=".$clubdata['id'].",
			pts='".$clubdata['points']."', played='".$clubdata['nummatch']."', win='".$clubdata['victory']."', nul='".$clubdata['nul']."', lost='".$clubdata['lost']."', sb='".$clubdata['bp']."', sa='".$clubdata['bc']."',
			dompts='".$clubdata['dompoints']."', domplayed='".$clubdata['domnummatch']."', domwin='".$clubdata['domvictory']."', domnul='".$clubdata['domnul']."', domlost='".$clubdata['domlost']."', domsb='".$clubdata['dombp']."', domsa='".$clubdata['dombc']."',
			extpts='".$clubdata['extpoints']."', extplayed='".$clubdata['extnummatch']."', extwin='".$clubdata['extvictory']."', extnul='".$clubdata['extnul']."', extlost='".$clubdata['extlost']."', extsb='".$clubdata['extbp']."', extsa='".$clubdata['extbc']."'";
			$id = runSQL($INSERT);
	}



}



?>