Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中,表头从for循环重复_Php_Mysql - Fatal编程技术网

在php中,表头从for循环重复

在php中,表头从for循环重复,php,mysql,Php,Mysql,我正在尝试从数据库创建排行榜。我把数据打印在一个列表中。当我尝试将此数据放入html表中时,每个数据条目后的标题都会重复自身。这是for循环造成的,但我不知道如何只打印一次标题,然后在每行中插入数据。任何帮助都将不胜感激。下面是代码和结果的屏幕截图。 提前跟你说 <?php require_once 'header.php'; // Send variables for the MySQL database class. $database = mysql_conn

我正在尝试从数据库创建排行榜。我把数据打印在一个列表中。当我尝试将此数据放入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>';
?>


结果是“通过用户名和分数在每个循环后重复位置、用户名和分数标题”

从循环中删除标题

这样做:

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

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

试试这个

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

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

试试这个

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

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

试试这个

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


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

谢谢,它成功了!!我是沿着同样的路线走的,但是在循环的一侧留下了结束表标签。非常感谢。谢谢你。它成功了!!我是沿着同样的路线走的,但是在循环的一侧留下了结束表标签。非常感谢。谢谢你。它成功了!!我是沿着同样的路线走的,但是在循环的一侧留下了结束表标签。非常感谢。谢谢你。它成功了!!我是沿着同样的路线走的,但是在循环的一侧留下了结束表标签。非常感谢你。