PHP HTML表格
我想展示一下PHP HTML表格,php,html,mysql,Php,Html,Mysql,我想展示一下 $row["name"] 通过HTML表格的形式,如下所示: echo "<html> <table class="."table"."> <tr> <td>".$row["name"]."</td> </tr> </table> </html>"; <html> <table class="table"> <tr> &
$row["name"]
通过HTML表格的形式,如下所示:
echo "<html>
<table class="."table".">
<tr>
<td>".$row["name"]."</td>
</tr>
</table>
</html>";
<html>
<table class="table">
<tr>
<td><?php echo $row['name']; ?></td>
</tr>
</table>
</html>
echo”
“$row[“name”]”
";
我也从这里得到了row变量:
$query = "SELECT * FROM servers WHERE public = 1";
if ($result = $db->query($query)) {
while ($row = $result->fetch_assoc()) {
printf ("Server name: %s", $row["name"].'<br>');
printf ("Hostname: %s", $row["host"].'<br>');
printf ("Port: %s", $row["port"].'<br>');
printf ("Player count: %s", $row["players"].'<br>');
printf ("Server Status: %s", $row["status"].'<br>');
printf ("Last pinged: %s", $row["last_ping"].'<br>');
printf ("Current ms: %s", $row["ms"].'<br>');
}
$result->free();
}
$query=“从public=1的服务器中选择*”;
如果($result=$db->query($query)){
而($row=$result->fetch_assoc()){
printf(“服务器名称:%s,$row[“name”]。
”);
printf(“主机名:%s”,$row[“主机”]。
”;
printf(“端口:%s”,$row[“端口”]。
”;
printf(“玩家数量:%s”,$row[“玩家”]。
”;
printf(“服务器状态:%s”,$row[“状态”]。
”;
printf(“上次ping:%s”,$row[“Last_ping”]。
”;
printf(“当前ms:%s”,$row[“ms”]。
”;
}
$result->free();
}
它确实成功地显示了打印的信息,但似乎无法将其放入表中
您是否尝试过从php echo语句中删除整个表 因此,将其作为html,然后只是在表中回显行
<td><?php echo $row["name"] ?></td>
这是:
echo "<html>
<table class="."table".">
<tr>
<td>".$row["name"]."</td>
</tr>
</table>
</html>";
echo”
“$row[“name”]”
";
我们将回应这一点:
<html>
<table class=table>
<tr>
<td>name</td>
</tr>
</table>
</html>
名称
这不是有效的html
根据Kyle Goslan的建议,您应该这样尝试:
echo "<html>
<table class="."table".">
<tr>
<td>".$row["name"]."</td>
</tr>
</table>
</html>";
<html>
<table class="table">
<tr>
<td><?php echo $row['name']; ?></td>
</tr>
</table>
</html>
我会这样做:
<html>
<table class="table">
<tr>
<th>Name</th>
</tr>
<?php
$query = "SELECT * FROM servers WHERE public = 1";
if ($result = $db->query($query)) {
while ($row = $result->fetch_assoc()) { ?>
<tr>
<td>
<?php echo $row['name']; ?>
</td>
</tr>
<?php }
}
$result->free();
?>
名称
您必须将表行
放在while循环中,并为每个数据字段使用单独的
列
示例代码:
echo '<table>';
while{$row}
{
echo '<tr>
<td>'.$row['name'].'</td>
</tr>';
}
echo '</table>';
echo';
而{$row}
{
回声'
“.$row['name']”
';
}
回声';
您从未将WHILE循环中的任何行值实际分配给变量。如果SQL语句返回的行数超过1行,则需要在WHILE循环中构建一个数组以供以后使用
首先定义数组
$myarray = array();
然后将SQL语句返回的对象分配给数组
while($row = $result->fetch_assoc()){
$myarray[] = $row;
//your other printf statements if you want to keep them
}
现在您有了一个填充的$myarray,其中第一级键是行号。由于您可能正在构建一个动态表(其大小取决于返回的信息量),因此还需要将PHP合并到该过程中
<table class = "table">
<?php
for($i = 0; $i < sizeof($myarray); $i++){
echo "<tr>";
echo "<td>";
echo $myarray[$i]["name"];
echo "</td>";
echo "</tr>";
}
?>
</table>
在上面的例子中,您对$myarray中的每一项运行for循环,对于从原始数据库pull返回的每一行,都会创建一个新的表行和相应的表列。第二个代码并没有放入看起来像的表中。@HarryDenley如果您查看imgur链接,它仍然在创建表,只是没有显示$row[“name”]。我尝试了多种方法,但都没有。
?–尝试
或
更有可能解决这个问题。您尝试过var_dump($row)吗在输出HTML之前,要检查$row变量的内容吗?是的,表行应该是。这不是有效的HTML,因为属性必须是引号。我正要对此发表评论。我不打算给出实际答案,因为像这样的问题往往会打开潘多拉魔盒和谚语。”是的,我以前试过,但没有效果。表正在显示,但为空。它没有得到变量,我不明白为什么。要让它工作,真的需要刷新我的PHP,这么长时间都没有。谢谢你的帮助,非常感谢。:~又一个快乐的结局