drfals1307 2014-11-30 18:00
浏览 25
已采纳

如何从php中的mysql表中显示两次数据

Im making a page that will display top ten voters in a table and show the number one voter aswell. I got the top ten table to work but I can't figure out how to get the second part to work. What would I do to get it to work where it would show the top voter in a img tag like this example

img src="https://minotar.net/avatar/TOPVOTERHERE"

First part

<div class="col-md-6">
        <table class="table table-striped">
            <tr>
                <td><strong>Username</strong></td>
                <td><strong>Votes</strong></td>
            </tr>
            <?php
            $username2 = "exampleuser";
            $password2 = "pass";
            $hostname = "127.0.0.1"; 

            $dbhandle = mysql_connect($hostname, $username2 , $password2) 
              or die("Unable to connect to MySQL");

            $selected = mysql_select_db("mc711",$dbhandle) 
              or die("Could not select database");

            $result = mysql_query("SELECT * FROM GALTotals ORDER BY votes DESC"); 
            $count = 0;
            while ($row = mysql_fetch_assoc($result)) {
            $count++;
            if($count>10){

            }else{
            echo "<tr>";
                echo "<td>".$row['IGN']."</td>";
                echo "<td>".$row['votes']."</td>";
            echo "</tr>";
}
}
?>
        </table>
    </div>

Second Part - This is the part I dont know how to do

    <div class="col-md-6">
        <center>
        <img src="https://minotar.net/avatar/TOPVOTERHERE">




        </center>
    </div>
  • 写回答

1条回答 默认 最新

  • doudou8783 2014-11-30 18:10
    关注

    The simplest way to do it would be to create a variable to store the top voter data before you start your while loop. Then, in the while loop, you can set this variable if you're in the first iteration.

    First part:

    $result = mysql_query("SELECT * FROM GALTotals ORDER BY votes DESC"); 
    $count = 0;
    $topVoterIGN = NULL;
    while ($row = mysql_fetch_assoc($result)) {
        $count++;
        if($count>10){
    
        }else{
            echo "<tr>";
            echo "<td>".$row['IGN']."</td>";
            echo "<td>".$row['votes']."</td>";
            echo "</tr>";
            if(is_null($topVoterIGN)) {
                $topVoterIGN = $row['IGN'];
            }
        }
    }
    

    Second Part:

    <div class="col-md-6">
        <center>
            <img src="https://minotar.net/avatar/<?php echo $topVoterIGN; ?>">
        </center>
    </div>
    

    Alternatively, you can store the top 10 voters in an array and then retrieve the first item later.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向