仅显示数组PHP的最后一条记录

时间:2011-06-09 00:18:19

标签: php mysql record

我有这个功能:

function getLow() {
global $db_prefix, $min;

   //get latest week with all entered scores
   $lastCompletedWeek = getLastCompletedWeek();

   $sql = "select u.userID, u.teamName ";
   $sql .= "from " . $db_prefix . "users u ";
   $query = mysql_query($sql);
   while ($result = mysql_fetch_array($query)) {

      for($i = 1; $i <= $lastCompletedWeek; $i++)    {

         $userScore = getLowScore($i, $result['userID']);

         $win[][week] = $i;
         $win[][user] = $result['userID'];
         $win[][teamName] = $result['teamName'];
         $win[][score] = $userScore;

      }

      $count = count($win);
      $lowest = 0;
      $min[score] = PHP_INT_MAX;
      $min[user] = $result['userID'];
      $min[teamName] = $result['teamName'];

      for($i = 0; $i < $count; $i++)    {
         if($win[$i + 1]['user'] == $result['userID'])        {
            if($win[$i + 3]['score'] < $min[score])            {
               $min[score] = $win[$i + 3]['score'];
               $lowest = $i;
            }

         }

      }

      unset($win[$lowest]);
      unset($win[$lowest + 1]);
      unset($win[$lowest + 2]);
      unset($win[$lowest + 3]);
      $win = array_values($win);


      //print_r ($min);

      //echo $min[teamName] . ' ' . $min[score] . ' ';

   }

}

当我从另一个.php文件中调用它时:

getLow($分钟);

我只获得最后一条记录......为什么?

这也是getLowScores函数。

function getLowScore($week, $userID) {
    global $db_prefix, $user;

    $score = 0;

    //get array of games
    $games = array();
    $sql = "select * from " . $db_prefix . "schedule where weekNum = " . $week . " order by gameTimeEastern, gameID";
    $query = mysql_query($sql);
    while ($result = mysql_fetch_array($query)) {
        $games[$result['gameID']]['gameID'] = $result['gameID'];
        $games[$result['gameID']]['homeID'] = $result['homeID'];
        $games[$result['gameID']]['visitorID'] = $result['visitorID'];
        $games[$result['gameID']]['tie'] = 1;

        if (($result['homeScore'] + (($result['visitorSpread'] * -1))) > ($result['visitorScore'] + (($result['homeSpread'] * -1)))) {
            $games[$result['gameID']]['winnerID'] = $result['homeID'];
        }
        if (($result['visitorScore'] + (($result['homeSpread'] * -1))) > ($result['homeScore'] + (($result['visitorSpread'] * -1)))) {
            $games[$result['gameID']]['winnerID'] = $result['visitorID'];
        }
        if (($result['visitorScore'] + ($result['homeSpread'] * -1)) == ($result['homeScore'] + ($result['visitorSpread'] * -1))) {
            $games[$result['gameID']]['winnerID'] = $result['tie'];
        }

    }

    //loop through player picks & calculate score
    $sql = "select p.userID, p.gameID, p.pickID, p.points, u.paid ";
    $sql .= "from " . $db_prefix . "picks p ";
    $sql .= "inner join " . $db_prefix . "users u on p.userID = u.userID ";
    $sql .= "inner join " . $db_prefix . "schedule s on p.gameID = s.gameID ";
    $sql .= "where s.weekNum = " . $week . " and u.userID = " . $userID . " ";
    $sql .= "order by u.lastname, u.firstname, s.gameTimeEastern";
    $query = mysql_query($sql);
    while ($result = mysql_fetch_array($query)) {
        if (!empty($games[$result['gameID']]['winnerID']) && $result['pickID'] == $games[$result['gameID']]['winnerID']) {
            //player has picked the winning team
            $score++;
        }
        if ($result['tie'] == $games[$result['gameID']]['winnerID']) {
            //player has picked the winning team
            $score++;

        }

    }

    return $score;

}

提前感谢您的帮助!!这让我发疯了?

1 个答案:

答案 0 :(得分:2)

也许不是 的答案,但这段代码非常破碎:

$win[][week] = $i;
$win[][user] = $result['userID'];
$win[][teamName] = $result['teamName'];
$win[][score] = $userScore;

首先,每次使用[]时,会将四个新行添加到$ win,这是一个新行,我非常怀疑你的意图。

其次,应该引用这些内容,因此它是["week"],而不是[week]启用PHP警告并关注它们

我想你想要:

$win[] = array(
    "week" => $i,
    "user" => $result['userID'],
    "teamName" => $result['teamName'],
    "score" => $userScore,
);

您可以显示警告:

error_reporting(E_ALL);
ini_set("display_errors",1);
相关问题