Location: PHPKode > projects > SportsPHool > sportsphool/leaderpicks.php
<?php
// leaderpicks.php: displays all player picks
//
// This file is part of the SportsPHool application
// Copyright (C) 2000, 2001 Barry Scott Will <hide@address.com>
// See /license and /gpl.txt for licensing information
//
$page="leaders";
$pagetitle="Leaderboard - Player Picks";
require("./shared.php");

// Make sure a valid player is accessing the page
if(!$HTTP_SESSION_VARS["userID"]) {
  header("Location: " . $SYS["base_href"] . "/signon.php?&message=4&" . SID);
  exit;
}
include($header);

// Output switch profile form
if(!switch_profile($SYS["base_href"] . "/leaderpicks.php", 0, $playerID, $profile)) {
  echo "<p><strong>You must create a profile before you can view the leaderboard.</strong></p>\n";
  include($footer);
  exit;
};

if(!$profile) {
  echo "<p><strong>You must first choose a profile from the list above, or create a new profile.</strong></p>\n";
  include($footer);
  exit;
}

if($stopdate>$SYS["gmt"]) {
  echo "<p><strong>You cannot view all player picks until the week closes. Please choose another week.</strong></p>\n";
  switch_week($SYS["base_href"] . "/leaderpicks.php", 0, $phool, $weekID);
  include($footer);
  exit;
}

echo "<h3>$profilename: Viewing player picks for Week $weekname</h3>\n";

$picktbl = $SYS["prefix"] . $phool . "Picks";
$gametbl = $SYS["prefix"] . $phool . "Games";
$teamtbl = $SYS["prefix"] . $phool . "Teams";

// Put all teams in an associative array
$qry = "SELECT teamID, teamabbr, teamlogo FROM $teamtbl";
$rs=&$conn->Execute($qry);
$teams=$rs->GetAssoc();

// Get all games for this week
$qry="SELECT gameID, visitor, home, winner FROM $gametbl WHERE weekID=$weekID ORDER BY gameID";
if($rs1=&$conn->Execute($qry)===FALSE) {
  echo "<p><strong>Could not access Games database. Please inform the site administrator of this error:<br/>\n" . $conn->ErrorNo() . ": " . $conn->ErrorMsg() . "</strong></p>\n";
  exit;
}
$games=$rs1->GetRows();

if(count($games)==0) {
  echo "<p><strong>No games have yet been entered for this week. Contact the site administrator if you believe there is a problem.</strong></p>\n";
  include($footer);
  exit;
}

// Get all user picks for this week
$qry="SELECT P.profileID, R.profilename, G.gameID, P.pick FROM $picktbl P, $gametbl G, $profiletbl R WHERE G.weekID=" . $weekID . " AND G.gameID=P.gameID AND R.profileID=P.profileID ORDER BY R.profilename, G.gameID";
if($rs2=&$conn->Execute($qry)===FALSE) {
  echo "<p><strong>Could not access Picks database. Please inform the site administrator of this error:<br/>\n" . $conn->ErrorNo() . ": " . $conn->ErrorMsg() . "</strong></p>\n";
  exit;
}

// Output TABLE tag and header cells
echo "<table>\n<tr><th>Player</th>\n";
foreach($games as $game) {
  echo "<th><small>";
  $class = getclass($teams[$game["visitor"]]["teamID"], $game["winner"]);
  echo "<span class=\"$class\">" . $teams[$game["visitor"]]["teamabbr"] . "</span>\n";
  echo "<br/>@<br/>\n";
  $class = getclass($teams[$game["home"]]["teamID"], $game["winner"]);
  echo "<span class=\"$class\">" . $teams[$game["home"]]["teamabbr"] . "</span></small></th>\n";
}
echo "</tr>\n";

// Output Rows
$rownum=1;
$colnum=0;
while(!$rs2->EOF) {
  // Start by checking to see if this is the first column
  // If so, output row tag and players profile name,
  if($colnum==0) {
    $class=!($rownum%2)?"alt":"";
    if($rs2->fields["profileID"]==$profile) $class="player";
    echo "<tr class=\"$class\"><td>";
    echo $rs2->fields["profilename"];
    echo "</td>\n";
  // If not, output a data cell
  } else {
    // Check to see if current gameID = this column's game
    // If so, we'll output a team and advance the recordset
    if($rs2->fields["gameID"]==$games[$colnum-1]["gameID"]) {
      echo "<td>";
      $pick = $rs2->fields["pick"];
      if($teams[$pick]["teamlogo"]) {
        echo "<img src=\"" . $SYS["assets"] . "/logos/" . $teams[$pick]["teamlogo"] . "\" width=\"" . $SYS["logowidth"] . "\" height=\"" . $SYS["logoheight"] . "\" alt=\"" . $teams[$pick]["teamabbr"] . "\"/>";
      } else {
        echo $teams[$pick]["teamabbr"];
      }
      echo "</td>\n";
      $rs2->MoveNext();
    // If not, output a blank and do not advance the recordset
    } else {
      echo "<td>&nbsp;</td>\n";
    }
  }
  $colnum++;
  // If we've run out of games, output end of row and reset column counter
  if($colnum>count($games)) {
    $rownum++;
    $colnum=0;
    echo "</tr>\n";
  }  
}

echo "</table>";

if($stopdate<$SYS["gmt"]) {
  echo "<p><a href=\"" . $SYS["base_href"] . "/leaders.php?gWeek=" . $weekID . "&" . SID . "\">View leaderboard</a></p>\n";
}

// Output the list of weeks for switching views
switch_week($SYS["base_href"] . "/leaderpicks.php", 0, $phool, $weekID);
include($footer);

// Function getclass(): gets winner/loser class to apply to table headers
// Requires:
//   $team: current team ID
//   $winner: team ID of current game's winner
function getclass($team, $winner) {
  global $games;
  $class = "";
  if ($winner<0 || $winner==$team) {
    $class = "winner";
  } elseif($winner>0 && $winner!=$team) {
    $class = "loser";
  }
  return $class;
}
?>
Return current item: SportsPHool