在php中,表头从for循环重复
我正在尝试从数据库创建排行榜。我把数据打印在一个列表中。当我尝试将此数据放入html表中时,每个数据条目后的标题都会重复自身。这是for循环造成的,但我不知道如何只打印一次标题,然后在每行中插入数据。任何帮助都将不胜感激。下面是代码和结果的屏幕截图。 提前跟你说在php中,表头从for循环重复,php,mysql,Php,Mysql,我正在尝试从数据库创建排行榜。我把数据打印在一个列表中。当我尝试将此数据放入html表中时,每个数据条目后的标题都会重复自身。这是for循环造成的,但我不知道如何只打印一次标题,然后在每行中插入数据。任何帮助都将不胜感激。下面是代码和结果的屏幕截图。 提前跟你说 <?php require_once 'header.php'; // Send variables for the MySQL database class. $database = mysql_conn
<?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>© 2014 Company, Inc. · <a href="#">Privacy</a> · <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>© 2014 Company, Inc. · <a href="#">Privacy</a> · <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>© 2014 Company, Inc. · <a href="#">Privacy</a> · <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>© 2014 Company, Inc. · <a href="#">Privacy</a> · <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>© 2014 Company, Inc. · <a href="#">Privacy</a> · <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>© 2014 Company, Inc. · <a href="#">Privacy</a> · <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>© 2014 Company, Inc. · <a href="#">Privacy</a> · <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>© 2014 Company, Inc. · <a href="#">Privacy</a> · <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>© 2014 Company, Inc. · <a href="#">Privacy</a> · <a href="#">Terms</a></p>
</footer>';
?>
如果有任何问题,请告诉我。谢谢,它成功了!!我是沿着同样的路线走的,但是在循环的一侧留下了结束表标签。非常感谢。谢谢你。它成功了!!我是沿着同样的路线走的,但是在循环的一侧留下了结束表标签。非常感谢。谢谢你。它成功了!!我是沿着同样的路线走的,但是在循环的一侧留下了结束表标签。非常感谢。谢谢你。它成功了!!我是沿着同样的路线走的,但是在循环的一侧留下了结束表标签。非常感谢你。