jim turner
jim turner

Reputation: 28

PHP array output to list

I am trying to output my array to list items within my HTML. It echo out fine but I want to format the output as a list. When I surround the echo $row["item"] in a <li> <?php echo $row["item"] ?> </li> it errors out. I don't know why.

Right now it will echo out from the PHP echo functions just fine into a large block but I want to format the array output into list items which I can style using css and I can't get this to function.

Code

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

Upvotes: 1

Views: 216

Answers (1)

Isac
Isac

Reputation: 1874

You get an error because your <?php echo $row["item"] ?> is outside the while that gets the records from the database. What yout need is something like:

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

instead of the :

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

Upvotes: 2

Related Questions