dorpbn1027 2013-06-10 18:19
浏览 13
已采纳

标记前3个条目

my question is finding out how to label the top 3 entries based on column "avg_score, in a table, while still showing the rest of the entries, but without a label. So say I have a table like this:

 Entry   avg_score

entry_1 | 4.3
entry_2 | 9.4
entry_3 | 4.6
entry_4 | 7.1
entry_5 | 2.1
entry_6 | 1.9

I want to be able to find the top 3 based on the column 'avg_score' and display it like:

"1st place: entry_2

2nd place: entry_4

3rd place: entry_3

entry_1

entry_5

entry_6"

Any help would be appreciated!

  • 写回答

2条回答 默认 最新

  • duanshan3331 2013-06-10 18:25
    关注

    At first you must make your query which would order by avg_score desceing.

    After that you can do something like this:

    <?
    $x = 1;
    while($row = mysql_fetch_array($q)){
        if($x < 4){
            echo $x.". place: ";
        }
        echo $row['Entry'];
    }
    ?>
    

    if you want it to show 1st etc then use:

    <?
    $x = 1;
    while($row = mysql_fetch_array($q)){
        if($x == 1){echo "1st place: ";
        }elseif($x == 2){echo "2nd place: ";
        }elseif($x == 3){echo "3rd place: ";}
        echo $row['Entry']."<br />";
        $x++;
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?