我有这个鳕鱼用于我的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]是SURNAME
短信[投票]是投票数
这些顶级列表看起来像这样
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
我希望你理解我,并且有人知道如何解决这个问题。 谢谢。
答案 0 :(得分: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]." ";
}
}
答案 1 :(得分: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]." ";
}
}
答案 2 :(得分:0)
假设前4个采用不同的颜色,从最后的第5个到第3个(从左下2个)用不同的颜色,最后2个用不同的颜色。
因此,首先从SQL查询中查找行的总数,以便我们可以在条件下放置第二行。
$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]." ";
}
}
答案 3 :(得分: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>';
}
}
?>