2013-09-22 77 views
0

我有這個鱈魚爲我的TOP榜榜首名單由三個部分組成

$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

所以,你有數據正確,你只是想根據他們的分數對它進行不同的格式化? –

+0

你輸出到HTML?這實際上是一個HTML/CSS的問題? –

+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]." "; 
    } 
} 
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]." "; 
    } 
} 
+0

這個鱈魚沒問題......只有一件事情對於後果不好。 頂部列表中的人數不斷增加。 當清單增加,因此增加和後果人數。 d我需要將其限制爲最後2人的後塵。 –

0

假設第一4中不同的顏色,從第五至第三最後(左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]." "; 
    } 
} 
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>'; 
    } 
} 
?>