【问题标题】:Add images to rank 1, 2 and 3 with MySQL and PHP使用 MySQL 和 PHP 将图像添加到排名 1、2 和 3
【发布时间】:2013-06-01 11:53:56
【问题描述】:

我正在尝试将 3 张图片添加到我的 PHP 代码中,用于排名 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 铜牌。

我对此一无所知,所以任何帮助都将不胜感激

【问题讨论】:

    标签: php mysql image rank


    【解决方案1】:

    为什么要调用 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

    【讨论】:

    • 嗨,伙计,感谢您的回复。我正在尝试你的代码,它可以工作,唯一的问题是它现在在排名行下没有显示排名数字,所以前 3 个有图像,其余的什么都没有
    • 我通过稍微编辑你的代码让它工作了 :) 非常感谢您的帮助!
    • @MikkelGlenvig,对不起,我忘了在图片中添加排名数字,不过,很高兴你已经成功了 :)
    • hmm 你知道如何让它只显示前 3 个图像,然后显示数字吗?所以它会是金银青铜 4 5 6 等等
    • 我已经更新了我的答案,希望这能如你所愿
    【解决方案2】:

    您所要做的就是在循环中放入一个 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>"; 
                }
    }
    

    【讨论】:

    • 谢谢伙伴 :) 我知道它们很大,在我开始工作之前我不会缩小它们 ^^
    • 嗯,你的代码没有显示金牌,而是将银牌放在 1 级,铜牌放在 2 级
    • 您可以将 Rank 初始化为 0(我想您在我给出答案之前已经这样做了),或者将显示图像的代码放在“echo Rank++;”之前。
    【解决方案3】:
    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`
    

    【讨论】:

      【解决方案4】:

      如果我理解正确,您可以在循环之前将变量 $Rank 设置为 0,因此 Rank 在循环结束之前有效,并且在循环中,在开始时使用 $Rank++ 递增它,然后下一步给玩家名字做这样的事情:

      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-07-07
        • 1970-01-01
        • 2014-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-18
        • 1970-01-01
        • 2019-12-14
        相关资源
        最近更新 更多