0

我的 TOP LIST 有这条鳕鱼

$pos=0;

$upit = mysql_query("SELECT * FROM wp_entries ORDER BY vote DESC");

while ($sms = mysql_fetch_array($upit)) {
$pos++;
$data = unserialize($sms['data']);
echo "  ".$pos." ".$data[1]['value']." ".$data[2]['value']."  ".$sms[vote]." ";
}
  • 数据[1] 是 NAME

  • 数据[2] 是姓氏

  • sms[vote] 是 NUMBER OF VOTES

这些顶级列表看起来像这样

1 Novak Djokovic 50
2 Rafael Nadal 35
3 Roger Federer 30
4 Andre Agassi 26
5 Pete Sampras 22
6 Andy Murray 19
7 Chris Evert 13
8 Michael Chang 10
9 Andy Roddick 7
10 Boris Becker 5
11 Björn Borg 1

我希望我的顶级列表看起来像这样

// FINALIST // Limit first 4 // green font
        1 Novak Djokovic 50 
        2 Rafael Nadal 35 
        3 Roger Federer 30 
        4 Andre Agassi 26 
// OTHERS // All other except first 4 and last 2 // black font
        5 Pete Sampras 22 
        6 Andy Murray 19 
        7 Chris Evert 13 
        8 Michael Chang 10 
        9 Andy Roddick 7        
// FALL OUT // Limit last 2 // red font
        10 Boris Becker 5
        11 Björn Borg 1

我希望你能理解我,并且有人知道如何解决这个问题。谢谢你。

4

4 回答 4

1

您可以使用以下代码:

$pos=0;
$upit = mysql_query("SELECT * FROM wp_entries ORDER BY vote DESC");
$max = mysql_num_rows($upit);
$max = $max - 2;
while ($sms = mysql_fetch_array($upit)) {
    $pos++;
    $data = unserialize($sms['data']);
    if ($pos < 5){
        // first 4 in different color. Green Font
        echo "  ".$pos." ".$data[1]['value']." ".$data[2]['value']."  ".$sms[vote]." ";
    } elseif ($pos <= $max) {
        //from 5th to 3rd last (left 2 from bottom) in different color. Black Font
        echo "  ".$pos." ".$data[1]['value']." ".$data[2]['value']."  ".$sms[vote]." ";
    } else {
        // last 2 in different color. Red Font
        echo "  ".$pos." ".$data[1]['value']." ".$data[2]['value']."  ".$sms[vote]." ";
    }
}
于 2014-01-02T00:06:09.137 回答
0

我的猜测是你想要这样的东西......然后你可以echo根据你想要的格式来更改你的代码。

$pos=0;

$upit = mysql_query("SELECT * FROM wp_entries ORDER BY vote DESC");

while ($sms = mysql_fetch_array($upit)) {
    $pos++;
    $data = unserialize($sms['data']);
    if ($pos < 5){
        // finalist format (1-4)
        echo "  ".$pos." ".$data[1]['value']." ".$data[2]['value']."  ".$sms[vote]." ";
    } elseif ($pos > 9) {
        // format fallout users (9+)
        echo "  ".$pos." ".$data[1]['value']." ".$data[2]['value']."  ".$sms[vote]." ";
    } else {
        // format other (5-9)
        echo "  ".$pos." ".$data[1]['value']." ".$data[2]['value']."  ".$sms[vote]." ";
    }
}
于 2013-09-22T11:52:12.193 回答
0

假设前 4 个不同颜色,从第 5 个到第 3 个最后(左下 2 个)不同颜色,最后 2 个不同颜色。

因此,首先从 SQL 查询中找到总行数,以便我们可以放置第二个 if 条件。

$pos=0;
$upit = mysql_query("SELECT * FROM wp_entries ORDER BY vote DESC");
$max = mysql_num_rows($upit);
$max = $max - 2;
while ($sms = mysql_fetch_array($upit)) {
    $pos++;
    $data = unserialize($sms['data']);
    if ($pos < 5){
        // first 4 in different color. Green Font
        echo "  ".$pos." ".$data[1]['value']." ".$data[2]['value']."  ".$sms[vote]." ";
    } elseif ($pos <= $max) {
        //from 5th to 3rd last (left 2 from bottom) in different color. Black Font
        echo "  ".$pos." ".$data[1]['value']." ".$data[2]['value']."  ".$sms[vote]." ";
    } else {
        // last 2 in different color. Red Font
        echo "  ".$pos." ".$data[1]['value']." ".$data[2]['value']."  ".$sms[vote]." ";
    }
}
于 2013-09-22T11:58:55.720 回答
0

假设您需要 HTML 输出,这是一种非常粗略的方法:

<?php


$pos = 0;

if( $upit = mysql_query( "SELECT * FROM wp_entries ORDER BY vote DESC" ) )
{
    $num_rows = mysql_num_rows( $upit );
    if( $num_rows > 0 )
    {
        echo '<ul>';

        while ($sms = mysql_fetch_array($upit))
        {
            $pos++;

            $style = "";

            if ($pos < 5)
            {
                $style = ' style="color:#00FF00;"';
            }
            elseif ($pos <= ($num_rows-2))
            {
                $style = ' style="color:#000000;"';
            }
            else
            {
                $style = ' style="color:#FF0000;"';
            }


            $data = unserialize($sms['data']);
            echo '  <li'.$style.'>'.$pos." ".$data[1]['value']." ".$data[2]['value']."  ".$sms['vote']."<li>";
        }

        echo '<ul>';
    }
}
?>
于 2013-09-22T12:06:01.187 回答