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 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?
  • ¥50 cocos2d-x lua 在mac上接入lua protobuf?