Created
March 3, 2017 14:17
-
-
Save phpfiddle/79868c591c1a01a30016cafaa148343c to your computer and use it in GitHub Desktop.
[ Posted by Telmo Frias ] Opta Stats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<script type="text/javascript"> | |
var _optaParams = { | |
custID: '', | |
language: 'en' | |
}; | |
</script> | |
<div> | |
<div> | |
<form action='team_player_stats.php' method='get'> | |
<select id="year" name="year"> | |
<option value="2017" <?=$_GET['year'] == 2017 ? ' selected="selected"' : '';?>>2017</option> | |
<option value="2016" <?=$_GET['year'] == 2016 ? ' selected="selected"' : '';?>>2016</option> | |
<option value="2015" <?=$_GET['year'] == 2015 ? ' selected="selected"' : '';?>>2015</option> | |
<option value="2014" <?=$_GET['year'] == 2014 ? ' selected="selected"' : '';?>>2014</option> | |
<option value="2013" <?=$_GET['year'] == 2013 ? ' selected="selected"' : '';?>>2013</option> | |
<option value="2012" <?=$_GET['year'] == 2012 ? ' selected="selected"' : '';?>>2012</option> | |
<option value="2011" <?=$_GET['year'] == 2011 ? ' selected="selected"' : '';?>>2011</option> | |
<option value="2010" <?=$_GET['year'] == 2010 ? ' selected="selected"' : '';?>>2010</option> | |
<option value="2009" <?=$_GET['year'] == 2009 ? ' selected="selected"' : '';?>>2009</option> | |
<option value="2008" <?=$_GET['year'] == 2008 ? ' selected="selected"' : '';?>>2008</option> | |
<option value="2007" <?=$_GET['year'] == 2007 ? ' selected="selected"' : '';?>>2007</option> | |
<option value="2006" <?=$_GET['year'] == 2006 ? ' selected="selected"' : '';?>>2006</option> | |
<option value="2005" <?=$_GET['year'] == 2005 ? ' selected="selected"' : '';?>>2005</option> | |
<option value="2004" <?=$_GET['year'] == 2004 ? ' selected="selected"' : '';?>>2004</option> | |
<option value="2003" <?=$_GET['year'] == 2003 ? ' selected="selected"' : '';?>>2003</option> | |
<option value="2002" <?=$_GET['year'] == 2002 ? ' selected="selected"' : '';?>>2002</option> | |
<option value="2001" <?=$_GET['year'] == 2001 ? ' selected="selected"' : '';?>>2001</option> | |
<option value="2000" <?=$_GET['year'] == 2000 ? ' selected="selected"' : '';?>>2000</option> | |
</select> | |
<input type="submit" value="Reload"> | |
</form> | |
</div> | |
<div><select id="comps" name="comps"></select></div> | |
<div><select id="teams" name="teams"></select></div> | |
<form action='updatedb.php' method='post' id='updForm'> | |
<input type="hidden" id="comp_id" name="comp_id" value="" > | |
<input type="hidden" id="comp_name" name="comp_name" value="" > | |
<input type="hidden" id="team_id" name="team_id" value="" > | |
<input type="hidden" id="team_name" name="team_name" value="" > | |
<input type="hidden" id="team_stats" name="team_stats" value="" > | |
<input type="hidden" id="year" name="year" value="<?php echo $_GET['year']; ?>" > | |
<input type="hidden" id="comp_index" name="comp_index" value="" > | |
<input type="hidden" id="team_index" name="team_index" value="" > | |
<!--<input type="submit" value="Update">--> | |
</form> | |
<input type="button" id='btnUpd' value="Data update"/> | |
<div id="content"></div> | |
<div id="result"><?php echo $_POST["res_id"]; ?></div> | |
</div> | |
<script src="widgets.opta.js"></script> | |
<script> | |
// -------------------- CONSTANTS -------------------- | |
const SEASON = <?php echo $_GET['year']; ?>; | |
const SPORT_ID = "1"; | |
const SPORT = "football"; | |
const COMP_INDEX = <?php if(isset($_POST['comp_index'])) { echo $_POST['comp_index'];} else {echo 0;};?>; | |
const TEAM_INDEX = <?php if(isset($_POST['team_index'])) { echo $_POST['team_index'];} else {echo 0;};?>; | |
// -------------------- HELPER -------------------- | |
function splitTrans(a) { | |
var out = [], a = a.split("¦"); | |
for (var b = 0; b < a.length; b++) { | |
var c = a[b].split("|"); | |
if (c[1] && c[1].length>0) { | |
out.push({id:c[0],full:c[1],short:c[2],abbr:c[3]}); | |
} | |
} | |
return out; | |
} | |
function addElement(values, delim, el, id) { | |
delim = delim || '', id = id || 'content', el = el || 'div'; | |
var elem = document.createElement(el); | |
elem.innerHTML = values.join(delim); | |
document.getElementById(id).appendChild(elem); | |
} | |
// -------------------- COMPETITIONS -------------------- | |
var competitions; | |
var compDef = new $jqOpta.Deferred; | |
compDef.resolveWith = function(a, b) { a.done(b[0].d); }; | |
compDef.done = function(a) { | |
var comps = splitTrans(a); | |
competitions = comps; | |
//var c = document.getElementById('comps'); | |
//var cid = c.options[c.selectedIndex].value; | |
ctParams.competition = String(competitions[0].id); | |
// var teamRequest = new $jqOpta.FeedRequest( | |
// $jqOpta.FeedRequest.FEED_TRANS_TEAM, | |
// ctParams, | |
// teamDef, | |
// 99999); | |
// $jqOpta.FeedMonitor.requestFeed(teamRequest); | |
comps.forEach(function(comp) { | |
var option = document.createElement("option"); | |
option.innerHTML = comp.full, option.value = comp.id; | |
document.getElementById('comps').appendChild(option); | |
}); | |
//document.getElementById("comps").selectedIndex = COMP_INDEX; | |
//document.getElementById("teams").selectedIndex = TEAM_INDEX; | |
}; | |
// -------------------- TEAMS -------------------- | |
var t; | |
var teamDef = new $jqOpta.Deferred; | |
teamDef.resolveWith = function(a, b) { a.done(b[0].d); }; | |
teamDef.done = function(a) { | |
var teams = splitTrans(a); | |
t = teams; | |
a = a.split("¦"); | |
var c = document.getElementById('teams'); | |
while (c.firstChild) { c.removeChild(c.firstChild); } | |
for (var k in t) { | |
var option = document.createElement("option"); | |
option.innerHTML = t[k].full, option.value = t[k].id; | |
document.getElementById('teams').appendChild(option); | |
} | |
}; | |
var i = 0; | |
document.getElementById('btnUpd').onclick = function (e) { | |
var dataDef = new $jqOpta.Deferred; | |
//dataDef.resolveWith = function(a, b) { a.done(b[0].d); }; | |
dataDef.done(function(a) { | |
var x = a; | |
document.getElementById("comp_id").value = a.SeasonStatistics['@attributes'].competition_id; | |
document.getElementById("comp_name").value = a.SeasonStatistics['@attributes'].competition_name; | |
document.getElementById("team_id").value = a.SeasonStatistics.Team['@attributes'].id; | |
document.getElementById("team_name").value = a.SeasonStatistics.Team['@attributes'].name; | |
var Team = a.SeasonStatistics.Team; | |
addElement([Team['@attributes'].name]); // add team name | |
var TeamStat = Team.Stat; | |
// remove all children | |
var c = document.getElementById('content'); | |
while (c.firstChild) { c.removeChild(c.firstChild); } | |
// output team stats | |
TeamStat.forEach(function(o) { | |
addElement([o['@attributes'].name, String(o['@value'])], ": "); | |
}); | |
document.getElementById("team_stats").value = JSON.stringify(TeamStat); | |
//sessionStorage.setItem('teamStat', TeamStat); | |
var TeamPlayer = Team.Player; | |
TeamPlayer.forEach(function(o) { | |
var a = o['@attributes']; | |
addElement(['<p></p><b>'+a.first_name+' '+a.last_name+'</b>', a.position], ', '); | |
o.Stat.forEach(function(p) { | |
addElement([p['@attributes'].name, String(p['@value'])], ': ', 'li'); | |
}); | |
}); | |
// remove all children | |
// var c = document.getElementById('teams'); | |
// while (c.firstChild) { c.removeChild(c.firstChild); } | |
// for (var k in t) { | |
// var option = document.createElement("option"); | |
// option.innerHTML = t[k].full, option.value = t[k].id; | |
// document.getElementById('teams').appendChild(option); | |
// } | |
document.getElementById("updForm").submit(); | |
}); | |
var c = document.getElementById('comps'); | |
var cid = c.options[c.selectedIndex].value; | |
ctParams.competition = String(cid); | |
var tid = t[i].id; | |
i = i + 1; | |
document.getElementById('comp_index').value = c.selectedIndex; | |
document.getElementById('team_index').value = document.getElementById('comps').selectedIndex; | |
var r = { | |
feedLife: 60, | |
feedParams: { | |
competition: cid, | |
season: SEASON, | |
sport: SPORT, | |
team: tid | |
}}; | |
var f = new $jqOpta.FeedRequest( | |
$jqOpta.FeedRequest.FEED_F30, | |
r.feedParams, // feed params like competition, season, etc. | |
dataDef, // result handler object | |
r.feedLife, // feed life ?? | |
r.trn, // trn, maybe translation ?? | |
r); // request object | |
// finally request feed | |
//$jqOpta.FeedMonitor.requestFeed(f); | |
$jqOpta.FeedMonitor.requestFeed(f); | |
}; | |
// -------------------- COMPS ONCHANGE -------------------- | |
document.getElementById('comps').onchange = function (e) { | |
var c = document.getElementById('comps'); | |
var cid = c.options[c.selectedIndex].value; | |
ctParams.competition = String(cid); | |
var teamRequest = new $jqOpta.FeedRequest( | |
$jqOpta.FeedRequest.FEED_TRANS_TEAM, | |
ctParams, | |
teamDef, | |
99999); | |
$jqOpta.FeedMonitor.requestFeed(teamRequest); | |
}; | |
// -------------------- TEAMS ONCHANGE -------------------- | |
// document.getElementById('teams').onchange = function (e) { | |
// var c = document.getElementById('comps'); | |
// var cid = c.options[c.selectedIndex].value; | |
// var t = document.getElementById('teams'); | |
// var tid = t.options[t.selectedIndex].value; | |
// var r = { | |
// feedLife: 60, | |
// feedParams: { | |
// competition: cid, | |
// season: SEASON, | |
// sport: SPORT, | |
// team: tid | |
// } | |
// }; | |
// var dataDef = new $jqOpta.Deferred; | |
// dataDef.done(function(a) { | |
// document.getElementById("comp_id").value = a.SeasonStatistics['@attributes'].competition_id; | |
// document.getElementById("comp_name").value = a.SeasonStatistics['@attributes'].competition_name; | |
// document.getElementById("team_id").value = a.SeasonStatistics.Team['@attributes'].id; | |
// document.getElementById("team_name").value = a.SeasonStatistics.Team['@attributes'].name; | |
// var Team = a.SeasonStatistics.Team; | |
// addElement([Team['@attributes'].name]); // add team name | |
// var TeamStat = Team.Stat; | |
// // remove all children | |
// var c = document.getElementById('content'); | |
// while (c.firstChild) { c.removeChild(c.firstChild); } | |
// // output team stats | |
// TeamStat.forEach(function(o) { | |
// addElement([o['@attributes'].name, String(o['@value'])], ": "); | |
// }); | |
// document.getElementById("team_stats").value = JSON.stringify(TeamStat); | |
// //sessionStorage.setItem('teamStat', TeamStat); | |
// var TeamPlayer = Team.Player; | |
// TeamPlayer.forEach(function(o) { | |
// var a = o['@attributes']; | |
// addElement(['<p></p><b>'+a.first_name+' '+a.last_name+'</b>', a.position], ', '); | |
// o.Stat.forEach(function(p) { | |
// addElement([p['@attributes'].name, String(p['@value'])], ': ', 'li'); | |
// }); | |
// }); | |
// }); | |
// var f = new $jqOpta.FeedRequest( | |
// $jqOpta.FeedRequest.FEED_F30, | |
// r.feedParams, // feed params like competition, season, etc. | |
// dataDef, // result handler object | |
// r.feedLife, // feed life ?? | |
// r.trn, // trn, maybe translation ?? | |
// r); // request object | |
// // finally request feed | |
// $jqOpta.FeedMonitor.requestFeed(f); | |
// }; | |
// -------------------- INITIALIZE -------------------- | |
var ctParams = { | |
competition: "", | |
cust_id: "default", | |
trans_id: $jqOpta.settings.translation_id || 1, | |
lang_id: "en_GB", | |
sport_id: SPORT_ID, | |
season: SEASON | |
}; | |
var compRequest = new $jqOpta.FeedRequest( | |
$jqOpta.FeedRequest.FEED_TRANS_COMP, | |
ctParams, | |
compDef, | |
99999); | |
$jqOpta.FeedMonitor.requestFeed(compRequest); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment