猿问

PHP 数组未显示在页面中

注释列表应显示在 ul li 范围内,有什么原因说明它们没有显示,而是数组显示在页面顶部?


数据库连接似乎工作得很好,但是注释没有显示在跨度内。它还删除了“您尚未添加任何注释文本”


代码


<?php


require_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>


慕的地10843
浏览 115回答 2
2回答

FFIVE

请随意使用下面的示例作为您的查询。// Your sql query$sql&nbsp; = "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 emptyif (!empty($notes)) {&nbsp; &nbsp; //Spit out the array&nbsp; &nbsp; print_r($notes);}

慕雪6442864

工作代码<?phprequire_once 'app/init.php';$notesQuery = $db->prepare("&nbsp; &nbsp; SELECT ID, note&nbsp; &nbsp; FROM notes");$notesQuery->execute();$notes = $notesQuery->rowCount() ? $notesQuery : [];?><!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <title>Notes</title>&nbsp; &nbsp; <link rel="stylesheet" href="styles.css"></head><body>&nbsp; &nbsp; <header>&nbsp; &nbsp; &nbsp; &nbsp; <h1>myNotes</h1>&nbsp; &nbsp; &nbsp; &nbsp; <nav>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="index.php">Home</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="about.html">About</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="contact.html">Contact</a>&nbsp; &nbsp; &nbsp; &nbsp; </nav>&nbsp; &nbsp; </header>&nbsp;&nbsp; &nbsp; <div class="container">&nbsp; &nbsp; &nbsp; &nbsp; <form action="add.php" method="post">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <textarea name="note" placeholder="Insert a note..." autocomplete="off" required></textarea>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="Add" />&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; <div class="notes">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h2>Notes</h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php if(!empty($notes)): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php foreach($notes as $note): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span><?php echo $note['note']; ?></span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endforeach; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php else: ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>you haven't added any notes yet.</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div></body></html>
随时随地看视频慕课网APP
我要回答