0

更新 2(球员让分指数计算)

    $sql3 = "SELECT roundID FROM rounds WHERE userID='$userID'";
    $result3 = mysql_query($sql3) or die(mysql_error());
    $total_rounds = mysql_num_rows($result3);


    //CALCULATE USER HANDICAP INDEX IF TOTAL_ROUNDS > 4
    if($total_rounds > 4){              
        if($total_rounds<7) { $score_count = 1; }
        elseif($total_rounds<9) { $score_count = 2; }
        elseif($total_rounds<11) { $score_count = 3; }
        elseif($total_rounds<13) { $score_count = 4; }
        elseif($total_rounds<15) { $score_count = 5; }
        elseif($total_rounds<17) { $score_count = 6; }
        elseif($total_rounds<18) { $score_count = 7; }
        elseif($total_rounds<19) { $score_count = 8; }
        elseif($total_rounds<20) { $score_count = 9; }
        else { $score_count = 10; }

        $sql2 = "SELECT differential FROM rounds WHERE userID='$userID' ORDER BY date DESC LIMIT 20";
        $result2 = mysql_query($sql2) or die(mysql_error());

        $diff_results = array();
        while($row = mysql_fetch_assoc($result2)){
            $diff_results[] = $row['differential'];
        }

        sort($diff_results);

        $diff_results = array_slice($diff_results, 0, $score_count);

        $handicapIndex = array_sum($diff_results) / $score_count * 0.96;
        $handicapIndex = (floor($handicapIndex * 10)) / 10;

希望这能让您了解我如何计算球员差点指数。现在我想向玩家(用户)展示用于计算他的指数的回合(排序日期)。

永远感激!

UPDATE(圆桌结构)

    roundID - auto incrementing primary key
    userID - INT
    courseID - INT
    tee - VARCHAR
    differential - FLOAT
    date - DATE

我什至很难开始使用我正在尝试实现的这个功能。任何帮助将非常感激。

我有一组 mysql db 结果我想按字段差异排序。我像这样将它们从数据库中拉出来:

    $sql4 = "SELECT * FROM rounds WHERE userID='$userID' ORDER BY date DESC LIMIT 20";
    $result4 = mysql_query($sql4) or die(mysql_error());
    $total_rounds = mysql_num_rows($result4);

正如你在上面看到的,我计算了返回运行 if-elseif-else 语句的行数,以确定我需要用不同的 css 突出显示多少分数:

    if($total_rounds<7) { $score_count = 1; }
    elseif($total_rounds<9) { $score_count = 2; }
    elseif($total_rounds<11) { $score_count = 3; }
    elseif($total_rounds<13) { $score_count = 4; }
    elseif($total_rounds<15) { $score_count = 5; }
    elseif($total_rounds<17) { $score_count = 6; }
    elseif($total_rounds<18) { $score_count = 7; }
    elseif($total_rounds<19) { $score_count = 8; }
    elseif($total_rounds<20) { $score_count = 9; }
    else { $score_count = 10; }

例如,如果 $total_rounds = 16 我的 $score_count 将是 6。现在我需要获取这个 16 行的数据集并用 php 将其吐出,所以我保持我的 ORDER BY 日期,同时对图中的 6 应用不同的 css 格式上面的 if-elseif-else 语句。计算 $score_count 是因为我需要在保持日期顺序的同时突出显示(也就是应用不同的 css)到 16 行数据集的 6 个最低分数。

所需的输出看起来像这样(* 表示单独的 css 格式 *)。

01-08-2013 - 16

01-07-2012 - 1 *

01-06-2013 - 15

01-05-2012 - 2 *

01-04-2013 - 14

01-03-2012 - 3 *

01-02-2013 - 13

01-01-2012 - 4 *

12-31-2012 - 12

2012 年 12 月 30 日 - 5 *

12-29-2012 - 11

2012 年 12 月 28 日 - 6 *

12-27-2012 - 10

12-26-2012 - 9

12-25-2012 - 8

12-24-2012 - 7

如果您有任何问题,请告诉我。

谢谢

4

2 回答 2

0

我将以$total_rounds = 16我的$score_count would be 6.

$total_rounds = 16;
$score_count = 6 // comes from the if-else loop you already have

// now we loop over the result
// the css is applied to every odd result, until $score_count is not 0

$counter = 0;
while( ( $data = mysql_fetch_assoc( $result4 ) ) !== false ) {
    if( ( $counter % 2 ) && $score_count ) {
        echo $data['date']; // apply your css here
    } else {
        echo $data['date'];
    }
    $counter++;
    $score_count--;

}

希望这可以帮助。

于 2013-01-09T06:16:49.787 回答
0

您必须遵循几个步骤。

1) 用 score(differential) 对结果进行排序,得到得分最低的 6 条记录的 id。

$sql4 = "SELECT * FROM rounds WHERE userID='$userID' ORDER BY differential LIMIT 20";
$result4 = mysql_query($sql4) or die(mysql_error());
$total_rounds = mysql_num_rows($result4);

if($total_rounds<7) { $score_count = 1; }
elseif($total_rounds<9) { $score_count = 2; }
elseif($total_rounds<11) { $score_count = 3; }
elseif($total_rounds<13) { $score_count = 4; }
elseif($total_rounds<15) { $score_count = 5; }
elseif($total_rounds<17) { $score_count = 6; }
elseif($total_rounds<18) { $score_count = 7; }
elseif($total_rounds<19) { $score_count = 8; }
elseif($total_rounds<20) { $score_count = 9; }
else { $score_count = 10; }

$idsArray = array();
for($i=0; $i<$score_count; $i++){
    $dets = mysql_fetch_array($result4);
    $idsArray[] = $dets['roundID'];
}

2)循环记录并匹配您在第一步中创建的数组中的id。如果 id 匹配,则应用 CSS,否则不应用。

$sql4 = "SELECT * FROM rounds WHERE userID='$userID' ORDER BY date DESC LIMIT 20";
$result4 = mysql_query($sql4) or die(mysql_error());

while($dets = mysql_fetch_array($result4)){
    if(in_array($dets['roundID'], $idsArray)){
        //apply CSS
        //display details here
    }
    else{
        //display details here
    }
}

注意:您现在应该停止使用 mysql_*。专家强烈建议使用 mysqli_* 代替

于 2013-01-09T06:42:27.923 回答