Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Apologies for the long explanation. I have tried to not leave anything out. If this is simply too complicated just let me know and I can go pay someone to do it. I am hoping it is a simple fix.

I have php code that generates scores from fitness test results. The test results are pulled from a mysql database. Players can login to a drupal website and view their scores, which are based on their individual fitness test results. This script works great and gives each player their score compared against everyone at the camp they attended. Scores are generated by the code each time they login. Scores are not stored in the database with the fitness test results.

I want to have an account for the coach of the team as well so that he can view player scores.

I have managed to make the code for a coach to login and view the actual fitness test results (like speed in time, bench press weight lifted in lbs, etc.) in an html table using a php array. This works perfectly.

What I want is to take a modified version of the code that calculates the scores from the players login pages and displays those scores on the coach's page. The code uses functions and I keep getting errors on everything I try related to these functions not being able to work more than once per page.

This is the current code that works for a logged in player. Normally the code calculates a race score, battle score, and recovery score but I have removed everything for the battle and recover because if I can see how to do the race I can just duplicate it for the other two.

<?php
  global $user;
  $cllectNode=array();
  $collectPlayers=array();
  $newNidList=array();


  function GetNames($uid)
  {
    $getNodeid=mysql_query("select nid from node where uid='".$uid."'")or die(mysql_error());
    $getNodeIdRow=mysql_fetch_array($getNodeid);
    $nameQry=mysql_query("select field_last_name_value,field_first_name_value from content_type_player where nid='".$getNodeIdRow['nid']."'")or die(mysql_error());
    $nameRow=mysql_fetch_array($nameQry);
    return $nameRow;
  }

  function gettingUserData($uid)
  {
    $userTestQry=mysql_query("select * from content_type_test where field_player_uid='".$uid."' order by nid desc limit 1")or die(mysql_error());
    $userTestRow=mysql_fetch_array($userTestQry);
    return $userTestRow;
  }

  function getAllUids($camp,$nid)
  {
  /*$nodeQry=mysql_query("select title,created from node where type='test' and nid='".$nid."'")or die(mysql_error());
    $nodeRow=mysql_fetch_array($nodeQry);
    $allnodesQry=mysql_query("select nid from node where  created='".$nodeRow['created']."' and type='test'")or die(mysql_error());
    while($allnodesRow=mysql_fetch_array($allnodesQry))
    {*/
      $allnodeTestsQry=mysql_query("select field_player_uid from content_type_test where  field_training_camp_nid='".$camp."'")or die(mysql_error());
      if(mysql_num_rows($allnodeTestsQry) > 0)
      {
        while($allnodeTestRow=mysql_fetch_array($allnodeTestsQry))
        {
          $collectPlayers[]=$allnodeTestRow['field_player_uid'];
        }
      }
/*}*/
    return $collectPlayers;
  }

  function getAllNids($camp,$nid)
  {
  /*$nodeQry=mysql_query("select title,created from node where type='test' and nid='".$nid."'")or die(mysql_error());
    $nodeRow=mysql_fetch_array($nodeQry);
    $allnodesQry=mysql_query("select nid from node where  created='".$nodeRow['created']."' and type='test'")or die(mysql_error());
    while($allnodesRow=mysql_fetch_array($allnodesQry))
    {*/
      $allnodeTestsQry=mysql_query("select nid from content_type_test where field_training_camp_nid='".$camp."'")or die(mysql_error());
      if(mysql_num_rows($allnodeTestsQry) > 0)
      {
        while($allnodeTestRow=mysql_fetch_array($allnodeTestsQry))
        {
          $cllectNode[]=$allnodeTestRow['nid'];
        }
      }

    /*}*/
return $cllectNode;
  }

function rankByCriteria($criteria,$uid,$userCriteria,$allnid,$order)
{
  $i=1;
  $rank=0;
  if($userCriteria=='0.00' || $userCriteria=='NULL')
  {
    $rank=0;
  } else {
    $allTestNids=implode(',',$allnid);
    $criteriaRankQry=mysql_query("select * from content_type_test where nid in($allTestNids) and $criteria!='NULL' and $criteria!='0.00' order by $criteria $order")or die('rank by criteria issue');
    while($criteriaRankRow=mysql_fetch_array($criteriaRankQry))
    {
      if($criteriaRankRow['field_player_uid']==$uid and $criteriaRankRow[$criteria]==$userCriteria)
      {
        $rank=$i;  
      }

      $i++;
    }
  }
  return $rank;
}

function rankByCriteriaAbsent($criteria,$uid,$userCriteria,$allnid,$order)
{
  $allTestNids=implode(',',$allnid);

  $criteriaRankQry=mysql_query("select * from content_type_test where nid in($allTestNids) and $criteria!='NULL' and $criteria!='0.00' order by $criteria $order")or die('rank by criteria issue');
  $countwithoutAbsent=mysql_num_rows($criteriaRankQry);
  return $countwithoutAbsent;
}

/******************************************************************/
if(in_array('player',$user->roles))
{
  $loginUserData=gettingUserData($user->uid);
  $allRecentNodes=getAllNids($loginUserData['field_training_camp_nid'],$loginUserData['nid']);
  $allUidsOfPlayers=getAllUids($loginUserData['field_training_camp_nid'],$loginUserData['nid']);
  $allPlayers=count($allRecentNodes);

  //////////////
  $speedArr=array();
  $AgilityArr=array();
  $TRxArr=array();
  $avgRaceArr=array();

  for($m=0; $m < count($allUidsOfPlayers); $m++)
  {
    $userParameters=gettingUserData($allUidsOfPlayers[$m]);
    $speedD=1;
    $speed1=rankByCriteria('field_speed_value',$allUidsOfPlayers[$m],$userParameters['field_speed_value'],$allRecentNodes,'ASC');
    $withoutAbsentPlayer=rankByCriteriaAbsent('field_speed_value',$allUidsOfPlayers[$m],$userParameters['field_speed_value'],$allRecentNodes,'ASC');
    ////
    $speedPercentile = 1-($speed1/($withoutAbsentPlayer+1));
    //echo '<br>';
    $speedArr[$allUidsOfPlayers[$m]] = ceil(5*$speedPercentile);
    if($speed1=='0')
    {
      $speedD=0;
      $speedArr[$allUidsOfPlayers[$m]]=0;
    }
    /////////////////////////////////////
    $AgilityD=1;
    $Agility1=rankByCriteria('field_agility_value',$allUidsOfPlayers[$m],$userParameters['field_agility_value'],$allRecentNodes,'ASC');
    $withoutAbsentPlayer=rankByCriteriaAbsent('field_agility_value',$allUidsOfPlayers[$m],$userParameters['field_agility_value'],    $allRecentNodes,'ASC');
    ////
    $Agility = 1-($Agility1/($withoutAbsentPlayer+1));
    //echo '<br>';
    $AgilityArr[$allUidsOfPlayers[$m]] = ceil(5*$Agility);
    if($Agility1=='0')
    {
      $AgilityD=0;
      $AgilityArr[$allUidsOfPlayers[$m]]=0;
    }

    //////////////////////////////
    $TRxD=1;
    $TRx1=rankByCriteria('field_trx_value',$allUidsOfPlayers[$m],$userParameters['field_trx_value'],$allRecentNodes,'ASC');
    $withoutAbsentPlayer=rankByCriteriaAbsent('field_trx_value',$allUidsOfPlayers[$m],$userParameters['field_trx_value'],$allRecentNodes,'ASC');
     ////
     $TRx = 1-($TRx1/($withoutAbsentPlayer+1));
     $TRxArr[$allUidsOfPlayers[$m]] = ceil(5*$TRx);
     if($TRx1=='0')
     {
       $TRxD=0;
       $TRxArr[$allUidsOfPlayers[$m]]=0;
     }
     ////


     /********************************************/
     if($speedD!=0 || $AgilityD!=0 || $TRxD!=0)
     {
       $avgRaceArr[$allUidsOfPlayers[$m]]=($speed1+$Agility1+$TRx1)/($speedD+$AgilityD+$TRxD);
     }
     /*******************************************/
   }
   /////////////////////////////rank of average for race///////////////////////////////

   asort($avgRaceArr); 
   $prevRank='00';
   $prevValue='00';
   $currentUserRace='0';
   $i=1;
   $rankOfAvgRace=array();
   foreach ($avgRaceArr as $key => $value)
   {
     if($value==$prevValue)
     {
       $currentRank=$prevRank;
     } else {
       $currentRank=$i;
     }
     $prevRank=$currentRank;
     $prevValue=$value;
     $rankOfAvgRace[$key]=$currentRank;
     if($user->uid==$key)
     {
       $currentUserRace=$currentRank;
     }
     $i++;
   }

   $totalRace=count($rankOfAvgRace);

   $speed=$speedArr[$user->uid];
   $Agility=$AgilityArr[$user->uid];
   $TRx=$TRxArr[$user->uid];


   ///////////////////end of rank of avg/////////////////////////////
   //echo count($allRecentNodes);
   //echo '</pre>';
   //print_r($allUidsOfPlayers);
   //echo '</pre>';

   $netRace=1-($currentUserRace/($totalRace+1));
   $netRace=ceil(5*$netRace);
   if($currentUserRace=='0')
   {
     $netRace=0;
   }

   /*****************************************************************************************/
   }

 ?>

If anyone needs more explanation of what the code is supposed to be doing I can add that.

When a coach is logged in he currently has an html table which shows him each player that attended his camp (based on the campid in the database) and what their fitness test results were

//get the camp id for the coach logged in
$getcampid = "SELECT value FROM profile_values WHERE uid = '$user->uid'";   
$campid = mysql_query ($getcampid);
$campidrow = mysql_fetch_array($campid);
$coachescamp = $campidrow[0];

$getplayerdata = mysql_query("SELECT * FROM content_type_test 

INNER JOIN content_type_player ON content_type_test.field_player_uid=

content_type_player.field_uid

WHERE content_type_test.field_training_camp_nid='$coachescamp' 

ORDER BY content_type_player.field_position_value") or die(mysql_error());

$left = true;
while($playerdata=mysql_fetch_array($getplayerdata))
{

echo "<td>", $playerdata['field_position_value'], "</td>";
echo "<td>", $playerdata['field_last_name_value'],", ",$playerdata['field_first_name_value'], "</td>";
echo "<td>", $playerdata['field_speed_value'], "</td>";
echo "<td>", $playerdata['field_agility_value'], "</td>";
echo "<td>", $playerdata['field_trx_value'], "</td>";
echo"</tr>";
}

I want to have a similar table that would be like this showing each players race score using the calculation in the first code

echo "<td>", $playerdata['field_position_value'], "</td>";
echo "<td>", $playerdata['field_last_name_value'],", ",$playerdata['field_first_name_value'], "</td>";
echo "<td>", *race score here using calculation in first code sample*, "</td>";
echo"</tr>";

If my thinking is correct I need to

  1. Run the first script once for each player in the coaches page array

  2. Anywhere in that script where $user->uid appears I need to replace it with the user id of the player who is in that row.

share|improve this question
5  
oh. wow........ – relentless Jun 5 '12 at 0:41
@Griff: Its not about the long post, its about the long code .. – Thrustmaster Jun 5 '12 at 0:42
3  
Short, Self Contained, Correct (Compilable), Example. Learn about SQL joins. Also, please stop writing new code with the ancient MySQL extension: it is no longer maintained and the community has begun the deprecation process. Instead you should use either the improved MySQLi extension or the PDO abstraction layer. – eggyal Jun 5 '12 at 0:43
Please note how much code you have commented out in your code samples. Note also that they're in main logic loops. One thing you might try and do is go through and comment what each section is supposed to do, and see if it's actually doing that. eggyal also has some excellent suggestions. – Nathaniel Ford Jun 5 '12 at 0:59
add comment (requires an account with 50 reputation)

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.