表头从PHP中的for循环重复 [英] table header is repeating from a for loop in php

查看:72
本文介绍了表头从PHP中的for循环重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库创建排行榜.我将数据打印在列表中.当我尝试将这些数据放在html表中时,标题将在每个数据条目之后重复其自身.这是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"

推荐答案

从循环中删除标题.

这样做:

<?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>';
?>

让我知道更多帮助!

这篇关于表头从PHP中的for循环重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆