/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Problems in ranking script.

I’ve got a script that does team ranking for a fantasy basketball website.

The rules for ranking are:

  • 1. Within a division, the teams are ordered from most to fewest wins.

  • 2. If two teams have the same number of wins, then the team with fewer losses is listed above the team with more losses.

  • 3. If two teams have the same number of wins and losses, the team with the most wins against teams within the division is rated above the team with fewer wins within the division.

  • 4. If two teams have the same number of wins and losses, and the same number of wins within the division, the team with the fewest losses within the division is listed above the team with more losses within the division.

  • 5. If two teams have the same number of wins and losses and the same number of wins and losses within the division, the team that was ranked higher in the previous season is listed above the team that was ranked lower in the previous season.
  • So, here is the function in question that attempted to follow this:

    [code=php]function Ranking($tm_arr){
    $t_list = array();
    $t_rank = array();
    $l=0;
    foreach($tm_arr as $tm){
    array_push($t_list, $tm);
    $t_list[$l][‘Placed’] = false;
    $l++;
    }
    $loop_halt=0;
    while(count($t_rank) < count($t_list) && $loop_halt < count($t_list)){
    $l=0;
    while(($t_list[$l][‘Placed’])){
    $l++;
    }
    $top = $t_list[$l];
    $l2 = 0;
    $pl_num = 0;
    while($l2 < count($t_list)){
    if(!($t_list[$l2][‘Placed’])){
    if(
    ($t_list[$l2][‘Wins’] > $top[‘Wins’]) ||
    (
    ($t_list[$l2][‘Wins’] == $top[‘Wins’]) &&
    ($t_list[$l2][‘Losses’] < $top[‘Losses’])
    ) ||
    (
    ($t_list[$l2][‘Wins’] == $top[‘Wins’]) &&
    ($t_list[$l2][‘Losses’] == $top[‘Losses’]) &&
    ($t_list[$l2][‘D_Wins’] > $top[‘D_Wins’])
    ) ||
    (
    ($t_list[$l2][‘Wins’] == $top[‘Wins’]) &&
    ($t_list[$l2][‘Losses’] == $top[‘Losses’]) &&
    ($t_list[$l2][‘D_Wins’] == $top[‘D_Wins’]) &&
    ($t_list[$l2][‘D_Losses’] > $top[‘D_Losses’])
    ) ||
    (
    ($t_list[$l2][‘Wins’] == $top[‘Wins’]) &&
    ($t_list[$l2][‘Losses’] == $top[‘Losses’]) &&
    ($t_list[$l2][‘D_Wins’] == $top[‘D_Wins’]) &&
    ($t_list[$l2][‘D_Losses’] == $top[‘D_Losses’]) &&
    ($t_list[$l2][‘Last_Seas’] > $top[‘Last_Seas’])
    )
    ){
    $top = $t_list[$l2];
    $pl_num = $l2;
    }
    }//*/
    $l2++;
    }
    array_push($t_rank, $top);
    $t_list[$pl_num][‘Placed’] = true;
    $loop_halt++;
    }

    return $t_rank;
    }
    [/code]

    For the most part, this works, but I’m running into a glitch. The page is at [url]http://fba.furtopia.org/Games/ranking.php[/url]. Down the left side are the team according to rank. Down the right is the order in which each team in each division appears in the XML file.

    The glitch is this: two teams are being repeated, which should not happen.

    One is under Western Conference > Southwest, the other is under Eastern Conference > Northern.

    to post a comment
    PHP

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @Mr_Initial_ManauthorDec 10.2009 — Below is the full PHP page, including an include:

    rankings.php
    [code=php]<?php
    include('../DataFiles/Includes/common.php');

    //////////////////////////////////////////////////////////////
    //
    // Get XML


    $teamlist = GetXML('../DataFiles/XML/teams.xml', '..');
    $year =
    (empty($_GET['year']))?
    (array((GetYear($teamlist) - 1), (GetYear($teamlist)))):
    explode('-', $_GET['year']);
    ;
    $years = $year[0] . '-' . $year[1];
    $gamelist = GetXML('../DataFiles/XML/' . $years . '_games.xml', '..');

    //////////////////////////////////////////////////////////////
    //
    // Set Up Teams Array: Conference and Divisions


    $teams = array();

    foreach($teamlist -> getElementsByTagName('Divisions') -> item(0) -> getElementsByTagName('Conference') as $conf){
    $c_name = $conf -> getAttribute('c_name');
    $teams[$c_name] = array();
    foreach($conf -> getElementsByTagName('Division') as $div){
    if(
    ($div -> getAttribute('from') <= $year[0])&&
    ($div -> getAttribute('to') >= $year[1])
    ){
    $d_name = $div -> getAttribute('d_name');
    $teams[$c_name][$d_name] = array();
    }
    }
    }

    //////////////////////////////////////////////////////////////
    //
    // Assign Teams

    foreach($teamlist -> getElementsByTagName('Team') as $team){
    if(
    ($team -> getAttribute('began') <= $year[0])&&
    ($team -> getAttribute('ended') >= $year[1])
    ){
    $t_div = $teamlist -> getElementById($team -> getElementsByTagName('Div') -> item(0) -> getAttribute('d_id'));
    $teams[
    $t_div -> parentNode -> getAttribute('c_name')
    ][
    $t_div -> getAttribute('d_name')
    ][
    $team -> getAttribute('code')
    ] = array(
    'Code' => $team -> getAttribute('code'),
    'Wins' => 0,
    'Losses' => 0,
    'D_Wins' => 0,
    'D_Losses' => 0,
    'Last_Seas' => (
    ($team -> getElementsByTagName('Season') -> length)?
    ($team -> getElementsByTagName('Season') -> item(0) -> getAttribute('div_rank')):
    (
    ($team -> getAttribute('was'))?
    (
    $teamlist -> getElementById($team -> getAttribute('was')) ->
    getElementsByTagName('Season') -> item(0) -> getAttribute('div_rank')
    ):4
    )
    ),
    );
    }
    }

    //////////////////////////////////////////////////////////////
    //
    // Calculate Team Performance (Last_Seas field is already filled in)

    function GetDiv($c){
    global $teamlist;
    return $teamlist -> getElementById(
    $teamlist -> getElementById($c) ->
    getElementsByTagName('Div') -> item(0) ->
    getAttribute('d_id')
    );
    }

    $game_count = 0;
    while(is_numeric($gamelist -> getElementsByTagName('Game') -> item($game_count) -> getAttribute('h_score'))){
    $game = $gamelist -> getElementsByTagName('Game') -> item($game_count);
    $a_code = $game -> getAttribute('away');
    $h_code = $game -> getAttribute('home');
    $a_div = GetDiv($a_code);
    $h_div = GetDiv($h_code);
    $a_d_name = $a_div -> getAttribute('d_name');
    $h_d_name = $h_div -> getAttribute('d_name');
    $a_conf = $a_div -> parentNode -> getAttribute('c_name');
    $h_conf = $h_div -> parentNode -> getAttribute('c_name');
    if($game -> getAttribute('h_score') > $game -> getAttribute('a_score')){
    $teams[$h_conf][$h_d_name][$h_code]['Wins']++;
    $teams[$a_conf][$a_d_name][$a_code]['Losses']++;
    if($a_d_name == $h_d_name){
    $teams[$h_conf][$h_d_name][$h_code]['D_Wins']++;
    $teams[$a_conf][$a_d_name][$a_code]['D_Losses']++;
    }
    } else {
    $teams[$h_conf][$h_d_name][$h_code]['Losses']++;
    $teams[$a_conf][$a_d_name][$a_code]['Wins']++;
    if($a_d_name == $h_d_name){
    $teams[$h_conf][$h_d_name][$h_code]['D_Losses']++;
    $teams[$a_conf][$a_d_name][$a_code]['D_Wins']++;
    }
    }
    $game_count++;
    }

    //////////////////////////////////////////////////////////////
    //
    // Calculate Team Rank.
    //
    // 1) Most Wins
    // 2) Fewest Losses
    // 3) Most Division Wins
    // 4) Fewest Division Losses
    // 5) Highest Ranking Last Season

    function Ranking($tm_arr){
    $t_list = array();
    $t_rank = array();
    $l=0;
    foreach($tm_arr as $tm){
    array_push($t_list, $tm);
    $t_list[$l]['Placed'] = false;
    $l++;
    }
    $loop_halt=0;
    while(count($t_rank) < count($t_list) && $loop_halt < count($t_list)){
    $l=0;
    while(($t_list[$l]['Placed'])){
    $l++;
    }
    $top = $t_list[$l];
    $l2 = 0;
    $pl_num = 0;
    while($l2 < count($t_list)){
    if(!($t_list[$l2]['Placed'])){
    if(
    ($t_list[$l2]['Wins'] > $top['Wins']) ||
    (
    ($t_list[$l2]['Wins'] == $top['Wins']) &&
    ($t_list[$l2]['Losses'] < $top['Losses'])
    ) ||
    (
    ($t_list[$l2]['Wins'] == $top['Wins']) &&
    ($t_list[$l2]['Losses'] == $top['Losses']) &&
    ($t_list[$l2]['D_Wins'] > $top['D_Wins'])
    ) ||
    (
    ($t_list[$l2]['Wins'] == $top['Wins']) &&
    ($t_list[$l2]['Losses'] == $top['Losses']) &&
    ($t_list[$l2]['D_Wins'] == $top['D_Wins']) &&
    ($t_list[$l2]['D_Losses'] < $top['D_Losses'])
    ) ||
    (
    ($t_list[$l2]['Wins'] == $top['Wins']) &&
    ($t_list[$l2]['Losses'] == $top['Losses']) &&
    ($t_list[$l2]['D_Wins'] == $top['D_Wins']) &&
    ($t_list[$l2]['D_Losses'] == $top['D_Losses']) &&
    ($t_list[$l2]['Last_Seas'] > $top['Last_Seas'])
    )
    ){
    $top = $t_list[$l2];
    $pl_num = $l2;
    }
    }//*/
    $l2++;
    }
    array_push($t_rank, $top);
    $t_list[$pl_num]['Placed'] = true;
    $loop_halt++;
    }

    return $t_rank;
    }


    $ranking = array();
    foreach(array_keys($teams) as $conf){
    $ranking[$conf] = array();
    foreach(array_keys($teams[$conf]) as $d_name){
    $ranking[$conf][$d_name] = Ranking($teams[$conf][$d_name]);
    }
    }


    //////////////////////////////////////////////////////////////
    //
    // Display Page

    $seas = ''' . (substr($year[0],2)) . ' - '' . (substr(($year[1]),2));

    include('../DataFiles/Includes/head.htm');
    ?>
    <link type="text/css" rel="stylesheet" href="./Display/CSS/main.css">
    <link type="text/css" rel="stylesheet" href="./Display/CSS/light.css">
    <link type="text/css" rel="stylesheet" href="./Display/CSS/nav.css">
    <style type="text/css">
    #Content_1{float:left;}
    #Content_2{float:right;}
    </style>
    <title>Furry Basketball Association <?php echo $seas; ?> Divisional Rankings</title>
    </head>
    <body>
    <div id="Top">
    <h1>Furry Basketball Association</h1>
    <h2><?php echo $seas; ?> Divisional Rankings</h2>
    </div>
    <div id="Content">
    <div id="Content_1">
    <h3><em>What should be order of rank</em></h3>
    <?php
    // PHP Data Continent

    foreach(array_keys($ranking) as $conf){
    echo
    t3 . '<div id="' . $conf . '_Conf">' . n .
    t4 . '<h3>' . $conf . ' Conference</h3>' . n .
    t4 . '<ul>' . n .
    ''
    ;
    foreach(array_keys($ranking[$conf]) as $d_name){
    echo
    t5 . '<li>' . n .
    t6 . '<strong>' . $d_name . '</strong>' . n .
    t6 . '<ul>' . n .
    ''
    ;
    for($l=0; $l < count($ranking[$conf][$d_name]); $l++){
    $t_el = $teamlist -> getElementById($ranking[$conf][$d_name][$l]['Code']);
    echo
    t7 . '<li>' . n .
    t8 . $ranking[$conf][$d_name][$l]['Code'] .
    /* $t_el ->getAttribute('home') . ' ' . $t_el ->getAttribute('name') .
    ' <em>(' .
    $ranking[$conf][$d_name][$l]['Wins'] . '-' .
    $ranking[$conf][$d_name][$l]['Losses'] .
    ')</em>' . //*/
    n .
    t5 . '</li>' . n
    ;
    }//*/
    echo
    t6 . '</ul>' . n .
    t5 . '</li>' . n
    ;
    }
    echo
    t4 . '</ul>' . n .
    t3 . '</div>' . n
    ;
    }
    ?>
    </div>
    <div id="Content_2">
    <h3><em>Teams according to order in XML file</em></h3>
    <?php
    foreach(array_keys($teams) as $conf){
    echo
    t3 . '<div id="' . $conf . '_Conf">' . n .
    t4 . '<h3>' . $conf . ' Conference</h3>' . n .
    t4 . '<ul>' . n .
    ''
    ;
    foreach(array_keys($teams[$conf]) as $d_name){
    echo
    t5 . '<li>' . n .
    t6 . '<strong>' . $d_name . '</strong>' . n .
    t6 . '<ul>' . n .
    ''
    ;
    foreach(array_keys($teams[$conf][$d_name]) as $t_code){
    // $t_el = $teamlist -> getElementById($ranking[$conf][$d_name][$t_code]);
    echo
    t7 . '<li>' . $t_code . '</li>' . n
    ;
    }//*/
    echo
    t6 . '</ul>' . n .
    t5 . '</li>' . n
    ;
    }
    echo
    t4 . '</ul>' . n .
    t3 . '</div>' . n
    ;
    }
    ?>
    </div>
    <div id="Nav">
    <?php include '../DataFiles/Includes/nav.htm'; ?>
    </div>
    </body>
    </html>
    [/code]


    common.php:
    [code=php]<?php

    function GetXML($file, $path=false, $f1=false, $f2=false, $f3=false){

    $xml = new DomDocument();

    $xml -> LoadXML((
    ($path)?(
    str_replace(
    'SYSTEM "http://fba.furtopia.org',
    ('SYSTEM "' . $path),
    file_get_contents($file)
    )
    ):
    file_get_contents($file)
    ));
    $xml -> validate();
    if($f1 == 'ID'){
    $x = $xml -> getElementByID($f2);
    } else if($f1 == 'Tags'){
    $x2 = $xml -> getElementsByTagName($f2);
    $x = (($f3) || ($f3 === 0))?$x2 -> item(0):$x2;
    }
    return($f1)?$x:$xml;
    }
    function GetYear($xml){
    return $xml -> getElementByID('ROOT') -> getAttribute('curr_seas');
    }
    define('n', "rn");
    define('t1', ' ');
    define('t2', t1 . ' ');
    define('t3', t2 . ' ');
    define('t4', t3 . ' ');
    define('t5', t4 . ' ');
    define('t6', t5 . ' ');
    define('t7', t6 . ' ');
    define('t8', t7 . ' ');
    define('t9', t8 . ' ');
    define('tA', t9 . ' ');


    ?>[/code]
    Copy linkTweet thisAlerts:
    @Mr_Initial_ManauthorDec 10.2009 — I figured out a solution:

    [code=php] $l=0;
    while(($t_list[$l]['Placed'])){
    $l++;
    }
    $top = $t_list[$l];
    $l2 = 0;
    $pl_num = 0;
    [/code]


    Became
    [code=php] $l=0;
    while(($t_list[$l]['Placed'])){
    $l++;
    }
    $top = $t_list[$l];
    $l2 = 0;
    $pl_num = $l;
    [/code]


    This made sure that $pl_num would never refer to an array element already placed in $t_rank.
    ×

    Success!

    Help @Mr_Initial_Man spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...