<?php

  include("html_functions.php");



  ################## PAGE INITIALIZATION #########################

  $lgid=$_SESSION['lgid'];

  $lgyear=$_SESSION['lgyear'];



  $pid=$_GET['player_id'];

  if ($pid!="")

  {

	  include($_SESSION['adminpath']."/dbopen.php");

	  $pid=mysqli_real_escape_string($db,$pid);



	  if (is_numeric($pid)) 

	  {

		  $query="SELECT player_id FROM players WHERE player_id=$pid";

		  $result=mysqli_query($db,$query);

		  if (mysqli_num_rows($result)) {header("Location: ./player.php?player_id=".$pid);}

		  else {$pidError=1;}

	  }

	  elseif (strlen($pid)==1)

	  {

		  $letter=strtoupper($pid);

		  unset($pid);

	  }

	  else

	  {

		  $query="SELECT position,role,player_id,first_name,last_name,nick_name,retired,hall_of_fame,MIN(year1) as minYr,MAX(year2) as maxYr FROM (SELECT p.position,p.role,p.player_id,first_name,last_name,nick_name,retired,hall_of_fame,MIN(year) as year1,MAX(year) as year2 FROM players as p,players_career_batting_stats as pcb WHERE p.player_id=pcb.player_id AND pcb.league_id=$lgid";

		  $query.=" AND (first_name LIKE '%".strtolower($pid)."%' OR first_name LIKE '".ucfirst($pid)."%' OR last_name LIKE '%".strtolower($pid)."%' OR last_name LIKE '".ucfirst($pid)."%' OR CONCAT(first_name,' ',last_name) LIKE '%".$pid."%' OR nick_name LIKE '%".strtolower($pid)."%' OR nick_name LIKE '".ucfirst($pid)."%')";

	       	  $query.=" GROUP BY player_id";

		  $query.=" UNION";

	       	  $query.=" SELECT p.position,p.role,p.player_id,first_name,last_name,nick_name,retired,hall_of_fame,MIN(year) as year1,MAX(year) as year2 FROM players as p,players_career_pitching_stats as pcp WHERE p.player_id=pcp.player_id AND pcp.league_id=$lgid";

		  $query.=" AND (first_name LIKE '%".strtolower($pid)."%' OR first_name LIKE '".ucfirst($pid)."%' OR last_name LIKE '%".strtolower($pid)."%' OR last_name LIKE '".ucfirst($pid)."%' OR CONCAT(first_name,' ',last_name) LIKE '%".$pid."%' OR nick_name LIKE '%".strtolower($pid)."%' OR nick_name LIKE '".ucfirst($pid)."%')";

		  $query.=" GROUP BY player_id) as t";

	       	  $query.=" GROUP BY player_id ORDER BY last_name,first_name;";

		  if ($_GET['showq']==1) {echo "$query<br/>\n";}

		  $presult=mysqli_query($db,$query);

	       	  if (mysqli_num_rows($presult)==1)

		  {

		    	  $row=mysqli_fetch_array($presult);

			  $pid=$row['player_id'];

			  header("Location: ./player.php?player_id=".$pid);

		  }	  

		  elseif (mysqli_num_rows($presult)) {$psearch=1;}

		  else {$psearchError=1;}

	  }



	  mysqli_close($db);

  }

  else

  {

	  $letter=$_GET['letter'];

	  $view=$_GET['view'];

  }

  $page=$_GET['page'];

  $pageLimit=200;





  $title='Players';

  page_head($title,$_SESSION['lgpath']);

  echo " <div id='topbar'>\n";

  main_titlebar($lgid,$_SESSION['lgpath'],$title);

  nav_toolbar("history","History");

  nav_toolbar("players");

  echo " </div>   <!-- END topbar DIV -->\n";

  echo " <div id='contentpane'>\n";



  ################## START OF PAGE CONTENT #########################

  include($_SESSION['adminpath']."/dbopen.php");



  if ($pidError==1) {echo "<div class='textbox' style='border:0;'>Player ID $pid does not exist</div>\n";}

  if ($psearchError==1) {echo "<div class='textbox' style='border:0;'>No players found with \"$pid\" in their name</div>\n";}





  # Display Results

  if ($psearch==1)

  {

	  echo "<div class='textbox' style='float:none;margin-left:auto;margin-right:auto;width:600px;'>   <!-- open player listing -->\n";

	  echo " <table border=0 cellpadding=4 cellspacing=0 width='100%' style='margin-left:auto;margin-right:auto;'>   <!-- open player listing -->\n";

	  echo "  <tr class='title2'><td>Players With \"".stripslashes($pid)."\" In Their Name</td></tr>\n";

	  echo "  <tr><td colspan=2 style='padding:0;margin:0;'>\n";

	  echo "   <table cellpadding=2 cellspacing=0 border=0 class='sortable' style='width:100%;'>\n";

	  echo "    <thead><tr class='headline'>";

	  echo "<td class='hsc2_l'>Player</td>";

	  echo "<td class='hsc2_l'>Nickname</td>";

	  echo "<td class='hsc2'>Pos</td>";

	  echo "<td class='hsc2'>From</td>";

	  echo "<td class='hsc2'>To</td>";

	  echo "<td class='hsc2'>OOTP Link</td>";

	  echo "</tr></thead>\n";



	  $rowCnt=0;

	  while ($row=mysqli_fetch_array($presult))

	  {

		  $pid=$row['player_id'];



		  $cls='s'.($rowCnt%2+1);

		  echo "    <tr class='$cls'>";

		  echo "<td class='".$cls."_l'>";

		  echo "<a href='./player.php?player_id=$pid'";

		  if ($row['hall_of_fame']==1) {echo " style='font-weight:bold;'";}

		  echo ">".$row['first_name']." ".$row['last_name']."</a>";

		  if ($row['retired']!=1) {echo "<sup>+</sup>";}

		  echo "</td>";

		  echo "<td class='".$cls."_l'>".$row['nick_name']."</td>";

		  $pos=$row['position'];

		  if ($pos==1) {$pos=$row['role'];}

		  echo "<td sorttable_customkey='$pos'>".get_pos($pos,1)."</td>";

		  echo "<td>".$row['minYr']."</td>";

		  echo "<td>".$row['maxYr']."</td>";

		  echo "<td><a href='".$_SESSION['lgpath']."/players/player_".$pid.".html'>OOTP Page</a></td>";

		  echo "</tr>\n";

		  $rowCnt++;

	  }



	  echo "   </table>\n";

	  echo "  </td></tr>\n";

	  echo " </table>   <!-- close player listing -->\n";

	  echo "</div>   <!-- close player listing -->\n";

	  echo "<div class='textbox' style='float:none;margin-left:auto;margin-right:auto;width:600px;border:0;'><sup>+</sup> = active player<br/><b>Bold</b> = Hall of Famer</div>\n";



  }

  elseif ($letter=="")

  {

	  echo "<div class='textbox' style='width:100%;'>   <!-- open player listing -->\n";

	  echo " <table border=0 cellpadding=4 cellspacing=0 width='100%' style='margin-left:auto;margin-right:auto;'>   <!-- open player listing -->\n";

	  echo "  <tr class='title2'><td colspan=3>Players</td></tr>\n";

	  $rowCnt=0;

	  for ($i=65;$i<=90;$i++) 

	  {

		  $x=chr($i);

	

		  $cls='s'.($rowCnt%2+1)."_l";



		  echo "  <tr class='$cls'>";

		  echo "<td rowspan=2><a href='./player_index.php?letter=$x' style='font-size:30px;'>$x</a></td>";

		  echo "<td style='border-bottom:1px dotted #CCCCCC;'>Batters</td>";

		  echo "<td style='border-bottom:1px dotted #CCCCCC;'>";

		  $query="SELECT p.player_id,first_name,last_name,retired,hall_of_fame,SUM(war) as war FROM players as p,players_career_batting_stats as pcs WHERE p.player_id=pcs.player_id AND pcs.league_id=$lgid AND split_id=1 AND last_name LIKE '".$x."%' GROUP BY player_id ORDER BY war DESC LIMIT 5;";

		  if ($_GET['showq']==1) {echo "$query<br/>\n";}

		  $result=mysqli_query($db,$query);

		  if (mysqli_num_rows($result))

		  {

			  $pcnt=0;

			  while ($row=mysqli_fetch_array($result))

			  {

				  $pid=$row['player_id'];

				  if ($pcnt>0) {echo ", ";}

				  echo "<a href='./player.php?player_id=$pid'";

				  if ($row['hall_of_fame']==1) {echo " style='font-weight:bold;'";}

				  echo ">".$row['first_name']." ".$row['last_name']."</a>";

				  if ($row['retired']!=1) {echo "<sup>+</sup>";}

				  $pcnt++;

			  }

		  }

		  else {echo "<i> - none -</i>";}

		  echo "</td>";

		  echo "</tr>\n";

		  echo "  <tr class='$cls'>";

		  echo "<td>Pitchers</td>";

		  echo "<td>";

		  $query="SELECT p.player_id,first_name,last_name,retired,hall_of_fame,SUM(war) as war FROM players as p,players_career_pitching_stats as pcs WHERE p.player_id=pcs.player_id AND pcs.league_id=$lgid AND split_id=1 AND last_name LIKE '".$x."%' GROUP BY player_id ORDER BY war DESC LIMIT 5;";

		  if ($_GET['showq']==1) {echo "$query<br/>\n";}

		  $result=mysqli_query($db,$query);

		  if (mysqli_num_rows($result))

		  {

			  $pcnt=0;

			  while ($row=mysqli_fetch_array($result))

			  {

				  $pid=$row['player_id'];

				  if ($pcnt>0) {echo ", ";}

				  echo "<a href='./player.php?player_id=$pid'";

				  if ($row['hall_of_fame']==1) {echo " style='font-weight:bold;'";}

				  echo ">".$row['first_name']." ".$row['last_name']."</a>";

				  if ($row['retired']!=1) {echo "<sup>+</sup>";}

				  $pcnt++;

			  }

		  }

		  else {echo "<i> - none -</i>";}

		  echo "</td>";

		  echo "</tr>\n";



		  $rowCnt++;



	  }



	  echo "   </table>\n";

	  echo "  </td></tr>\n";

	  echo " </table>   <!-- close player listing -->\n";

	  echo "</div>   <!-- close player listing -->\n";

	  echo "<div class='textbox' style='border:0;'><sup>+</sup> = active player<br/><b>Bold</b> = Hall of Famer</div>\n";

  }

  elseif ($view=="")

  {

	  $query="SELECT player_id,first_name,last_name,nick_name,position,role,retired,hall_of_fame,MIN(minYr) as minYr,MAX(maxYr) as maxYr FROM ((SELECT p.player_id,first_name,last_name,nick_name,p.position,role,retired,hall_of_fame,MIN(year) as minYr,MAX(year) as maxYr FROM players as p,players_career_batting_stats as pcs WHERE p.player_id=pcs.player_id AND pcs.league_id=$lgid AND split_id=1 AND last_name LIKE '".$letter."%' GROUP BY player_id) UNION (SELECT p.player_id,first_name,last_name,nick_name,p.position,role,retired,hall_of_fame,MIN(year) as minYr,MAX(year) as maxYr FROM players as p,players_career_pitching_stats as pcs WHERE p.player_id=pcs.player_id AND pcs.league_id=$lgid AND split_id=1 AND last_name LIKE '".$letter."%' GROUP BY player_id) ) as t1 GROUP BY player_id ORDER BY last_name,first_name";



	  $pgQuery="SELECT COUNT(*) as cnt FROM ($query) as x";

	  if ($_GET['showq']==1) {echo "$pgQuery<br/>\n";}

	  $result=mysqli_query($db,$pgQuery);

	  if (mysqli_num_rows($result)) 

	  {

		  $row=mysqli_fetch_array($result);

		  $totCnt=$row['cnt'];

		  $pgCnt=ceil($totCnt/$pageLimit);

	  }

	  else {$pgCnt=1;}





	  echo "<div class='textbox' style='float:none;margin-left:auto;margin-right:auto;width:600px;'>   <!-- open player listing -->\n";

	  echo " <table border=0 cellpadding=4 cellspacing=0 width='100%' style='margin-left:auto;margin-right:auto;'>   <!-- open player listing -->\n";

	  echo "  <tr class='title2'><td>\"$letter\" Player Index</td>\n";

	  echo "   <td style='margin-bottom:0px;padding-bottom:0px;'>\n";

	  if ($pgCnt<2) {echo "&nbsp;";}

	  else

	  {

		  echo "    <table border=0 cellpadding=0 cellspacing=0 style='float:right;'>\n";

		  echo "     <tr class='title2'><td>Page</td>\n";

		  echo "     <td>\n";

		  echo "      <form action='./player_index.php' method='GET'>\n";

		  echo "       <input type='hidden' name='letter' value='$letter' >\n";

		  echo "       <select name='page' onchange='this.form.submit()' style='float:right;clear:none;'>\n";

		  for ($i=0;$i<$pgCnt;$i++)

		  {

			  echo "        <option value='$i'";

			  if ($page==$i) {echo " selected='selected'";}

			  echo ">".($i+1)."</option>\n";

		  }

		  echo "       </select>\n";

		  echo "      </form>\n";

		  echo "     </td>\n";

		  echo "     <td style='text-align:left;'>of $pgCnt</td></tr>\n";

		  echo "    </table>\n";

	  }

	  echo "   </td>\n";

	  echo "  </tr>\n";

	  echo "  <tr><td colspan=2 style='padding:0;margin:0;'>\n";

	  echo "   <table cellpadding=2 cellspacing=0 border=0 class='sortable' style='width:100%;'>\n";

	  echo "    <thead><tr class='headline'>";

	  echo "<td class='hsc2_l'>Player</td>";

	  echo "<td class='hsc2_l'>Nickname</td>";

	  echo "<td class='hsc2'>Pos</td>";

	  echo "<td class='hsc2'>From</td>";

	  echo "<td class='hsc2'>To</td>";

	  echo "<td class='hsc2'>OOTP Link</td>";

	  echo "</tr></thead>\n";



	  $query.=" LIMIT ".($page*$pageLimit).",$pageLimit";

	  if ($_GET['showq']==1) {echo "$query<br/>\n";}

	  $result=mysqli_query($db,$query);

	  if (mysqli_num_rows($result))

	  {

		  $rowCnt=0;

		  while ($row=mysqli_fetch_array($result))

		  {

			  $pid=$row['player_id'];



			  $cls='s'.($rowCnt%2+1);

			  echo "    <tr class='$cls'>";

			  echo "<td class='".$cls."_l'>";

			  echo "<a href='./player.php?player_id=$pid'";

			  if ($row['hall_of_fame']==1) {echo " style='font-weight:bold;'";}

			  echo ">".$row['first_name']." ".$row['last_name']."</a>";

			  if ($row['retired']!=1) {echo "<sup>+</sup>";}

			  echo "</td>";

			  echo "<td class='".$cls."_l'>".$row['nick_name']."</td>";

			  $pos=$row['position'];

			  if ($pos==1) {$pos=$row['role'];}

			  echo "<td sorttable_customkey='$pos'>".get_pos($pos,1)."</td>";

			  echo "<td>".$row['minYr']."</td>";

			  echo "<td>".$row['maxYr']."</td>";

			  echo "<td><a href='".$_SESSION['lgpath']."/players/player_".$pid.".html'>OOTP Page</a></td>";

			  echo "</tr>\n";

			  $rowCnt++;

		  }

	  }



	  echo "   </table>\n";

	  echo "  </td></tr>\n";

	  echo " </table>   <!-- close player listing -->\n";

	  echo "</div>   <!-- close player listing -->\n";

	  echo "<div class='textbox' style='float:none;margin-left:auto;margin-right:auto;width:600px;border:0;'><sup>+</sup> = active player<br/><b>Bold</b> = Hall of Famer</div>\n";

  }



  mysqli_close($db);



  ################## END OF PAGE #########################

  echo " </div>   <!-- END contentpane DIV -->\n";



  page_foot($_SESSION['lgpath']);





?>

