PHP数组输出到列表

标签 php html mysql arrays

我正在尝试输出我的数组以列出我的 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/

相关文章:

php - 将空字符串绑定(bind)到 pdo 准备查询时出现 HY104 Sql 服务器错误

mysql - 将 2 个值合并到 1 列 MySQL

Php计算每一天的花费

php - Mysql查询字段中匹配id

php - 如何从用于连接到我的网站的上一页获取 URL?不是链接

html - 你如何在 chrome 表格中重复获得图像背景以留在圆形边框中?

html - Paypal 运输下拉默认方法

javascript - html 密码提示

php - 条件超链接列所需的语法

php - 支付宝整合