dony113407 2015-03-23 12:07
浏览 25
已采纳

表头从php中的for循环重复

I am trying to create a leaderboard from a database. I have the data printing out in a list. when i try to put this data in a html table the header is repeating itself after each data entry. It is the for loop causing this but i cant figure out how to just have the header printing once and the data being inserted in each row after that. any help would be greatly appreciated. The code and a screen shot of the result is below. Thak you in advance.

    <?php
require_once 'header.php';
    // Send variables for the MySQL database class.
    $database = mysql_connect('localhost', 'root', 'password') or die('Could not connect: ' . mysql_error());
    mysql_select_db('robinsnest') or die('Could not select database');

    $query = "SELECT * FROM `members` ORDER by `quiz_score` DESC LIMIT 10";
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());

    $num_results = mysql_num_rows($result);  

    for($i = 1; $i <= $num_results; $i++)
    {
         $row = mysql_fetch_array($result);
         echo "<table>
  <tr>
    <th>Position</th>
    <th>User Name</th>      
    <th>Score</th>
  </tr>
  <tr>
    <td>".$i."</td>
    <td>".$row['user']."</td>       
    <td>".$row['quiz_score']."</td>
  </tr>

</table>";
    }
    echo '<footer>
                <p class="pull-right"><a href="#">Back to top</a></p>
                <p>&copy; 2014 Company, Inc. &middot; <a href="#">Privacy</a> &middot; <a href="#">Terms</a></p>
            </footer>';
?>

The result is The "position, user Name and score headers repeated after each loop through the users name and score"

  • 写回答

2条回答 默认 最新

  • dpn517111 2015-03-23 12:09
    关注

    Remove Header from loop.

    Do like this:

    <?php
    require_once 'header.php';
        // Send variables for the MySQL database class.
        $database = mysql_connect('localhost', 'root', 'password') or die('Could not connect: ' . mysql_error());
        mysql_select_db('robinsnest') or die('Could not select database');
    
        $query = "SELECT * FROM `members` ORDER by `quiz_score` DESC LIMIT 10";
        $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    
        $num_results = mysql_num_rows($result);  
    
        echo "<table>
              <tr>
              <th>Position</th>
              <th>User Name</th>      
               <th>Score</th>
               </tr>";
    
        for($i = 1; $i <= $num_results; $i++)
        {
             $row = mysql_fetch_array($result);
    
             echo "<tr>
                  <td>".$i."</td>
                  <td>".$row['user']."</td>       
                  <td>".$row['quiz_score']."</td>
                  </tr>";
        }
    
        echo "</table>";
    
        echo '<footer>
                    <p class="pull-right"><a href="#">Back to top</a></p>
                    <p>&copy; 2014 Company, Inc. &middot; <a href="#">Privacy</a> &middot; <a href="#">Terms</a></p>
                </footer>';
    ?>
    

    Let me know for more help !!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算