【问题标题】:PHP array output to listPHP数组输出到列表
【发布时间】:2017-11-12 10:10:46
【问题描述】:

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

【问题讨论】:

  • 你为什么不把列表中的所有数组项写下来,它会来找你的
  • &lt;? php 是您的实际语法吗?里面有空间。编辑:这是一个要求澄清的问题;所以..是还是不是?如果是,那么这就是问题所在,是解析错误。如果不是,请编辑您的问题。
  • 如果不是这种情况,请具体说明您的疑问
  • 对不起,我的实际语法没有空格

标签: php html mysql arrays


【解决方案1】:

您收到错误消息,因为您的 &lt;?php echo $row["item"] ?&gt; 位于从数据库获取记录的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-04
    • 2014-01-06
    • 1970-01-01
    • 2014-08-11
    • 2011-01-28
    • 2011-12-25
    • 2022-06-12
    • 2018-01-10
    相关资源
    最近更新 更多