PHP排行榜添加(t)关系

时间:2014-09-17 18:38:29

标签: php

我有一个PHP排行榜,效果很好,并且可以很好地处理关系。目前,它将从第1位到最后位置(无论哪个是#)对用户进行编号,如果存在关系,则会使用相同的编号将它们全部列出。

例如:

  
      
  1. userC 2. userG 3. userA 3. userT 3. userJ 4. userW 5. userP
  2.   

我想要的是有关系的时候,排行榜要显示"(t)"数字旁边,如下:(t)3。userT

这是我的代码,感谢任何帮助:

<table cellpadding="4" cellspacing="0" class="table1" width="100%"><caption>
      <h2>Leaderboard</h2>
    </caption>
            <tr><th align="left">Player</th><th align="left">Wins</th><th>Pick Ratio</th></tr>

<?php
if (isset($playerTotals)) {
 $playerTotals = sort2d($playerTotals, 'score', 'desc');
 $i = 1;
 $tmpScore = 0;

 //show place #
foreach($playerTotals as $playerID => $stats) {
    if ($tmpScore < $stats[score]) $tmpScore = $stats[score];
//if next lowest score is reached, increase counter
    if ($stats[score] < $tmpScore ) $i++;

    $pickRatio = $stats[score] . '/' . $possibleScoreTotal;
    $pickPercentage = number_format((($stats[score] / $possibleScoreTotal) * 100), 2) . '%';

//display users/stats
$rowclass = ((($i - 1) % 2 == 0) ? ' class="altrow"' : '');
echo '  <tr' . $rowclass . '><td style="height: 25px;"><b>' . $i . '</b>. &nbsp;&nbsp;' . $stats[userName] . '</td><td align="center">' . $stats[wins] . '</td><td align="center">' . $pickRatio . ' (' . $pickPercentage . ')</td></tr>';
    $tmpScore = $stats[score];
  }
 }
echo '      </div>' . "\n";
?>

        </table>

2 个答案:

答案 0 :(得分:0)

我要创建一个新变量$placeholder。所以:

if ( $i != 0 ) {
    if ($tmpScore < $stats[score]) {
        $tmpScore = $stats[score];
    } 
    if ( $tmpScore == $stats[score] ) {
        $placeholder = $i.'(t)';
    } else if ($stats[score] < $tmpScore ) 
        $placeholder = $++i;
    }
} else {
    $placeholder = $++i;
    $firstScore = $stats[score];
    $tmpScore = $stats[score];
}

然后代替打印$ i print $ placeholder

所以第一次通过你可以在echo中做到这一点:

//It makes more sense if you read it from bottom to top but I put it this way 
//so you will not have to keep running through every condition when you will
//only evaluate the first condition most often
if ( $i != 0 && $i != 1 ) {
    echo ;//Your normal print
} else if ( $i = 1 && $tmpScore != $firstScore ) {
    echo '<b>'; //and the rest of the first line plus second line NOT a tie
} else if ( $i = 1 ) {
    echo ' (t)'; //Plus the rest of your first line plus second line TIE
} else {
    //This is your first time through the loop and you don't know if it's a tie yet so just
    //Print placeholder and then wait to figure out the rest
    echo '  <tr' . $rowclass . '><td style="height: 25px;"><b>' . $placeholder;
}

答案 1 :(得分:0)

试试此代码...希望它能解决您的问题

<table cellpadding="4" cellspacing="0" class="table1" width="100%">
    <caption><h2>Leaderboard</h2></caption>
    <tr><th align="left">Player</th><th align="left">Wins</th><th>Pick Ratio</th></tr>

<?php
if (isset($playerTotals)) {
    $playerTotals = sort2d($playerTotals, 'score', 'desc');
    $j = 1;
    $tmpScore = 0;

    //show place #
    $tieflag=false;
    for($i=0; $i<=count($playerTotals)-1; $i++) {
        if(($i<count($playerTotals) && $playerTotals[$i][score]==$playerTotals[$i+1][score]) || ($i>0 && $playerTotals[$i][score]==$playerTotals[$i-1][score])) $tieflag=true;
        $pickRatio = $$playerTotals[$i][score] . '/' . $possibleScoreTotal;
        $pickPercentage = number_format((($playerTotals[$i][score] / $possibleScoreTotal) * 100), 2) . '%';
        $rowclass = ((($j - 1) % 2 == 0) ? ' class="altrow"' : '');     
        echo '  <tr' . $rowclass . '><td style="height: 25px;"><b>' . ($tieflag?'(t)'.$j:$j) . '</b>. &nbsp;&nbsp;' . $playerTotals[$i][userName] . '</td><td align="center">' . $playerTotals[$i][wins] . '</td><td align="center">' . $pickRatio . ' (' . $pickPercentage . ')</td></tr>';
        $j++;
    }
}
echo '</div>'. "\n";
?>
</table>
相关问题