表头从php中的for循环重复

时间:2015-03-23 12:07:04

标签: php mysql

我正在尝试从数据库创建排行榜。我将数据打印在列表中。当我尝试将这些数据放在一个html表中时,标题在每次数据输入后重复出现。这是for循环导致这一点,但我无法弄清楚如何只有标题打印一次,然后在每行中插入数据。任何帮助将不胜感激。代码和结果的屏幕截图如下。 提前告诉你。

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

结果是&#34;位置,用户名和分数标题在每次循环后重复通过用户名和分数&#34;

2 个答案:

答案 0 :(得分:1)

从循环中删除标题。

这样做:

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

让我知道更多帮助!!

答案 1 :(得分:0)

试试这个,

<?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());

    echo '<table>
        <tr>
        <th>Position</th>
        <th>User Name</th>      
        <th>Score</th>
        </tr>';

    $i = 0;
    while($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>';
?>

请告诉我,如果有任何问题。