我正在尝试输出我的数组以列出我的 HTML 中的项目。它echo
很好,但我想将输出格式化为列表。当我包围 echo $row["item"]
时在<li> <?php echo $row["item"] ?> </li>
它出错了。我不知道为什么。
现在它将从 PHP 回显函数中回显到一个大块中,但我想将数组输出格式化为我可以使用 css 设置样式的列表项,但我无法让它运行。
代码
<?php include 'database.php' ; ?>
<?php
$result = mysqli_query($con, "SELECT * FROM shouts");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Shout IT!</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>
<div id="container">
<header>
<h1>SHOUT IT! shoutbox</h1>
</header>
<div id="shouts">
<?php while($row = mysqli_fetch_array($result)){
echo $row["user"] , $row["message"], $row{"time"};
}
?>
<ul>
<li class="shout">test</li>
<li class="shout"><?php echo $row["user"] ?></li>
<li class="shout"></li>
</ul>
</div>
<div id="input">
<form method="post" action="process.php">
<input type="text" name="user" placeholder="Enter Your Name" />
<input type="text" name="message" placeholder="Enter A Message" />
<br>
<input class="btn" type="submit" name="submit" value="Shout it Out" />
</form>
</div>
</div>
</body>
</html>
最佳答案
你得到一个错误,因为你的 <?php echo $row["item"] ?>
在while
之外从数据库中获取记录。你需要的是这样的:
<ul>
<?php while($row = mysqli_fetch_array($result)){
?>
<li class="shout">test</li>
<li class="shout"><?=$row["user"]?></li>
<li class="shout"><?=$row["message"]?></li>
<li class="shout"><?=$row["time"]?></li>
<?php
}
?>
</ul>
而不是:
<?php while($row = mysqli_fetch_array($result)){
echo $row["user"] , $row["message"], $row{"time"};
}
?>
<ul>
<li class="shout">test</li>
<li class="shout"><? php echo $row["user"] ?></li>
<li class="shout"></li>
</ul>
关于PHP数组输出到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44473929/