1

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>
4
  • Why don't you just write down all the array items in the list and it will come for you Commented Jun 10, 2017 at 13:42
  • 1
    <? php is that your actual syntax? with the space in there. Edit: this is a question asking for clarification; so.. is it or is it not? If it is, then that's the problem, being a parse error. If it is not, then edit your question. Commented Jun 10, 2017 at 13:42
  • If this is not the case and then be specific about your doubt Commented Jun 10, 2017 at 13:42
  • Sorry, my actual syntax is without the space <?php echo $row["item"] ?> Commented Jun 10, 2017 at 13:48

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.