为了账号安全,请及时绑定邮箱和手机立即绑定

PHP 数组未显示在页面中

PHP 数组未显示在页面中

PHP
慕的地10843 2023-06-24 19:17:54
注释列表应显示在 ul li 范围内,有什么原因说明它们没有显示,而是数组显示在页面顶部?数据库连接似乎工作得很好,但是注释没有显示在跨度内。它还删除了“您尚未添加任何注释文本”代码<?phprequire_once 'app/init.php';$notesQuery = $db->prepare("    SELECT ID, note    FROM notes");$notesQuery->execute();$notes = $notesQuery->rowCount() ? $notesQuery : [];foreach($notes as $note) {    print_r($note);}?><!DOCTYPE html><html><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Notes</title>    <link rel="stylesheet" href="styles.css"></head><body>    <header>        <h1>myNotes</h1>        <nav>            <a href="index.php">Home</a>            <a href="about.html">About</a>            <a href="contact.html">Contact</a>        </nav>    </header>     <div class="container">        <form action="add.php" method="post">                 <textarea name="note" placeholder="Insert a note..." autocomplete="off" required></textarea>                <input type="submit" value="Add" />        </form>        <div class="notes">            <h2>Notes</h2>            <?php if(!empty($notes)): ?>            <ul>                <?php foreach($notes as $note): ?>                    <li>                        <span><?php echo $note['note']; ?></span>                    </li>                <?php endforeach; ?>            </ul>                <?php else: ?>                    <p>you haven't added any notes yet.</p>                <?php endif; ?>        </div>
查看完整描述

2 回答

?
FFIVE

TA贡献1797条经验 获得超6个赞

请随意使用下面的示例作为您的查询。


// Your sql query

$sql  = "SELECT ID, note FROM notes";


// Prepare your statement

$stmt = $db -> prepare($sql);


// Execute your prepared statement

$stmt -> execute();


// Retreive all rows

$notes = $stmt -> fetchAll();


// Check if array is not empty

if (!empty($notes)) {

    //Spit out the array

    print_r($notes);

}


查看完整回答
反对 回复 2023-06-24
?
慕雪6442864

TA贡献1812条经验 获得超5个赞

工作代码


<?php


require_once 'app/init.php';


$notesQuery = $db->prepare("

    SELECT ID, note

    FROM notes

");


$notesQuery->execute();


$notes = $notesQuery->rowCount() ? $notesQuery : [];



?>

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Notes</title>

    <link rel="stylesheet" href="styles.css">

</head>

<body>

    <header>

        <h1>myNotes</h1>

        <nav>

            <a href="index.php">Home</a>

            <a href="about.html">About</a>

            <a href="contact.html">Contact</a>

        </nav>

    </header> 

    <div class="container">

        <form action="add.php" method="post"> 

                <textarea name="note" placeholder="Insert a note..." autocomplete="off" required></textarea>

                <input type="submit" value="Add" />

        </form>

        <div class="notes">

            <h2>Notes</h2>


            <?php if(!empty($notes)): ?>


            <ul>

                <?php foreach($notes as $note): ?>

                    <li>

                        <span><?php echo $note['note']; ?></span>

                    </li>

                <?php endforeach; ?>

            </ul>

                <?php else: ?>

                    <p>you haven't added any notes yet.</p>

                <?php endif; ?>

        </div>

    </div>

</body>

</html>


查看完整回答
反对 回复 2023-06-24
  • 2 回答
  • 0 关注
  • 94 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信