Fairplay tabela – juniori

Fairplay tabela

posts p
inner join $wpdb->postmeta pm on (p.id = pm.post_id)
where p.post_type = ‘sp_event’
and p.post_date < date_add(current_timestamp(), interval 8 hour) and pm.meta_key = 'sp_results' and length(pm.meta_value) > 10
and p.id in
(
select object_id from $wpdb->term_relationships
where term_taxonomy_id in
(
select term_taxonomy_id from $wpdb->term_taxonomy tax
inner join $wpdb->terms t on (t.term_id = tax.term_id)
where t.slug in (‘juniori-grupa-a’, ‘juniori-grupa-b’, ‘juniori-grupa-c’, ‘juniori-grupa-d’, ‘polufinale_junior_cup’, ‘finale-junior-cup’,'utakmica-za-3-mjesto-junior-cup’)

)
);”;

$rez = $wpdb->get_results($wpdb->prepare ($sql_eventi ));

// ucitaj detalje utakmica i saberi faulove, zute kartone…
$tabela = array();
foreach($rez as $u){
$p = get_post_meta( (int)$u->id, ‘sp_players’);

foreach($p[0] as $timid => $igraci){
@$tabela[$timid][“timid”] = $timid;
@$tabela[$timid][“utakmica”]++;
foreach($igraci as $igrac){
@$tabela[$timid][“faulovi”] += $igrac[“faulovi”];
@$tabela[$timid][“zuti”] += $igrac[“yellowcards”];
@$tabela[$timid][“crveni”] += $igrac[“redcards”];
}
}
}

//izracunaj fair play score
foreach($tabela as $tim_id => $stat){
@$tabela[$tim_id][“score”] = round(($stat[“faulovi”] + $stat[“zuti”]*2 + $stat[“crveni”]*4 ) / $stat[“utakmica”],2);
}

// sortiraj tabelu
usort($tabela, function($a, $b) {
return ((float)$a[‘score’]) > ((float)$b[‘score’]);
});

// print tabelu
echo “

“;

foreach($tabela as $stat){
$tim_id = $stat[“timid”];
$naziv_tima = $wpdb->get_var($wpdb->prepare (“select post_title from $wpdb->posts
where id = $tim_id and post_type = ‘sp_team'”));

echo “

“;
}

echo “

Ekipa Broj faulova Žuti kartoni Crveni kartoni Broj odigranih utakmica Fair Play*
$naziv_tima $stat[faulovi] $stat[zuti] $stat[crveni] $stat[utakmica] $stat[score]

“;
?>

*Fair Play = (F + ŽK*2 + CK*4) / Broj odigranih utakmica