1

我试图在我的 PHP 代码中添加 3 张图像,适用于排名 1、2 和 3 的人。但我似乎无法让它工作

我得到了 3 张名为 gold_medal.png、silver_medal.png 和 brown_medal.png 的图片

这是我的代码

<?php 
        //Database Info 
        $host = 'xxx.xxx.xxx.xxx'; 
        $user = 'xxxxxxxxxxxxx'; 
        $pass = 'xxxxxxxxxxxxx';
        $db = 'hall_of_fame'; 

<table> 
    <tr> 
    <th>Rank</th> 
    <th>Player</th> 
    <th>Donation</th> 
    </tr>
<?php
//Connect to the SQL server and select the database... 
mysql_connect($host, $user, $pass) or die('Couldn\'t connect to the MySQL server.'); 
mysql_select_db("$db") or die(mysql_error());

$rowsPerPage = 10;
// if $_GET['page'] defined, use it as page number

function steam2friend($steam_id){ 
$steam_id = strtolower($steam_id); 
    if(substr($steam_id,0,7) == 'steam_0'){ 
        $tmp = explode(':',$steam_id); 
            if((count($tmp)==3) && is_numeric($tmp[1]) && is_numeric($tmp[2])){ 
                return bcadd((($tmp[2]*2)+$tmp[1]),'76561197960265728'); 
            }else{ 
            return false; 
            } 
            }else{ 
            return false; 
        } 
    } 


$result = mysql_query("SELECT * FROM hof_players ORDER BY donation DESC LIMIT $rowsPerPage") or die(mysql_error());
$result2 = mysql_query("SELECT * FROM hof_players ORDER BY donation DESC LIMIT $rowsPerPage") or die(mysql_error());
$Rank = 1;
while ($row = mysql_fetch_row($result))
{
            while($row = mysql_fetch_array($result2)){ 
            echo "<tr align=center>";
            echo "<td>";
            echo $Rank++;
            echo "</td>";
            if(!empty($row['name']) && $row['name'] != ' '){ 
            echo "<td>" . '<i>&#160;&#160;&#160;&#160;&#160;&#160;&#160;</i>' . '<a href="http://steamcommunity.com/profiles/'.steam2friend($row['steamid']).'" target="_blank">'.$row['name'].'</a>' . '<i>&#160;&#160;&#160;&#160;&#160;&#160;</i>' . "</td>"; 
            } 
            else{ 
            echo "<td>" . 'No Player In DB' . "</td>"; 
            }
            if(!empty($row['donation']) && $row['donation'] != ' '){ 
            echo "<td>" . $row['donation'] . '&#160;' . 'DKK' . "</td>"; 
            } 
            else{ 
            echo "<td>" . 'No donations in DB' . "</td>"; 
            } 
            echo "</tr>"; 
            }
}
?>
</table>

我在这里建立了一个测试站点,您可以在其中看到它http://clanroyal.dk/hof_test.php

现在我想要的是排名 1 在他的名字旁边有金牌,排名 2 银牌,排名 3 铜牌。

我对这个完全一无所知,所以任何帮助都将不胜感激

4

4 回答 4

0

为什么要调用 2 个产生相同结果的不同 SQL 查询?

嵌套while循环$row变量覆盖原来的,摆脱第二个$result2查询,试试这个:

$result = mysql_query("SELECT * FROM hof_players ORDER BY donation DESC LIMIT $rowsPerPage") or die(mysql_error());
$Rank = 1;

$rankImages = array(
    1   =>   'gold_medal.png',
    2   =>   'silver_medal.png',
    3   =>   'bronze_medal.png'
);

while($row = mysql_fetch_array($result))
{ 
    echo "<tr align=center>";
    echo "<td>";

    if($Rank>0 && $Rank<=3){
        echo $Rank . ' <img src="' . $rankImages[$Rank] . '">';
    } else {
        echo $Rank . ' - ';
    }

    echo "</td>";

    $rank += 1;

    if(!empty($row['name']) && $row['name'] != ' ' && $row[0])
    { 
        echo "<td>" . '<i>&#160;&#160;&#160;&#160;&#160;&#160;&#160;</i> <a href="http://steamcommunity.com/profiles/' . steam2friend($row['steamid']) . '" target="_blank">' . $row['name'] . '</a><i>&#160;&#160;&#160;&#160;&#160;&#160;</i></td>'; 
    } else { 
        echo "<td>No Player In DB</td>"; 
    }

    if(!empty($row['donation']) && $row['donation'] != ' ')
    { 
        echo "<td>" . $row['donation'] . "&#160;DKK</td>"; 
    } else { 
        echo "<td>No donations in DB</td>"; 
    } 

    echo "</tr>"; 
}

此外,你应该改掉使用mysql_*功能的习惯。它已折旧,请尝试使用mysqliPDO

于 2013-06-01T12:01:26.177 回答
0

If I understood right, you could do just set the variable $Rank to 0 before the loop, so the Rank is valid till the end of loop, and in the loop, at the beggining increment it with $Rank++, and next to player name do something like this:

if ($Rank == 1) echo "<img src='gold_medal.png' />";
if ($Rank == 2) echo "<img src='silver_medal.png' />";
if ($Rank == 3) echo "<img src='bronze_medal.png' />";
于 2013-06-01T12:06:32.177 回答
0

您所要做的就是在循环中放置一个 IF 语句来检查 RANK 的值,并输出正确的奖牌。我注意到你的奖牌图片很大,所以你可能想先缩小它们。

    while ($row = mysql_fetch_row($result))
{
            while($row = mysql_fetch_array($result2)){ 
            echo "<tr align=center>";
            echo "<td>";
            echo $Rank++;
if($Rank == 1)
 echo("<img src=\"gold_medal.png\"/>");
else if ($Rank == 2)
 echo("<img src=\"silver_medal.png\"/>");
else if ($Rank == 3)
 echo("<img src=\"bronze_medal.png\"/>");

            echo "</td>";
            if(!empty($row['name']) && $row['name'] != ' ' && $row[0]){ 
            echo "<td>" . '<i>&#160;&#160;&#160;&#160;&#160;&#160;&#160;</i>' . '<a href="http://steamcommunity.com/profiles/'.steam2friend($row['steamid']).'" target="_blank">'.$row['name'].'</a>' . '<i>&#160;&#160;&#160;&#160;&#160;&#160;</i>' . "</td>"; 
            } 
            else{ 
            echo "<td>" . 'No Player In DB' . "</td>"; 
            }
            if(!empty($row['donation']) && $row['donation'] != ' '){ 
            echo "<td>" . $row['donation'] . '&#160;' . 'DKK' . "</td>"; 
            } 
            else{ 
            echo "<td>" . 'No donations in DB' . "</td>"; 
            } 
            echo "</tr>"; 
            }
}
于 2013-06-01T11:59:52.490 回答
0
while($row = mysql_fetch_array($result2)){ 
            echo "<tr align=center>";
            switch($Rank) {
                case 1:
                     echo "<td><img src=\"gold_medal.png\"/></td>";
                     break;
                case 2:
                     echo "<td><img src=\"silver_medal.png\"/></td>";
                     break;
                case 3:
                     echo "<td><img src=\"bronze_medal.png\"/></td>";
                     break;
                default:
                     echo "<td></td>";
                     break;
            }
            echo "<td>";
            echo $Rank++;
            echo "</td>";
            echo "<td>";
            echo $Rank++;
            echo "</td>";
            if(!empty($row['name']) && $row['name'] != ' ' && $row[0]){ 
            echo "<td>" . '<i>&#160;&#160;&#160;&#160;&#160;&#160;&#160;</i>' . '<a href="http://steamcommunity.com/profiles/'.steam2friend($row['steamid']).'" target="_blank">'.$row['name'].'</a>' . '<i>&#160;&#160;&#160;&#160;&#160;&#160;</i>' . "</td>"; 
            } 
            else{ 
            echo "<td>" . 'No Player In DB' . "</td>"; 
            }
            if(!empty($row['donation']) && $row['donation'] != ' '){ 
            echo "<td>" . $row['donation'] . '&#160;' . 'DKK' . "</td>"; 
            } 
            else{ 
            echo "<td>" . 'No donations in DB' . "</td>"; 
            } 
            echo "</tr>"; 
            }`enter code here`
于 2013-06-01T12:00:05.397 回答