0

I'm having a bit of struggle putting together a selection list in HTML where the options or values are being retrieved from a table in the database. I get the following result: Select List

As you can see this is not my intention, I rather have all options in 1 Selection List, with multiple options clickable.

This is my HTML Code (Bootstrap Framework):

<div class="container-fluid well well-lg">

     <?php while ($itemsrow = $query->fetch()) : ?>

<div class="form-group">

      <label for="sel1">Select list:</label>

<select class="form-control" id="sel1">

         <option><?php echo $itemsrow['Beschrijving']; ?></option>

The PHP Logic:

$conn = Db::getInstance();
$query = $conn->prepare("SELECT * FROM items WHERE user_id = $userID");
$query->execute();

Thanks in advance!

0

2 Answers 2

3

Currently, you're creating new elements inside the loop so yes, you will get a new select per iteration.

You should only keep the <option>-elements inside the loop:

<div class="container-fluid well well-lg">
    <div class="form-group">
        <label for="sel1">Select list:</label>

            <select class="form-control" id="sel1">

            <?php while ($itemsrow = $query->fetch()) : ?>  

                <option><?php echo $itemsrow['Beschrijving']; ?></option>

            <?php endwhile; ?>
Sign up to request clarification or add additional context in comments.

2 Comments

lol thanks, I feel a bit stupid but thanks for replying so quickly!
@Moya - No worries. Just remember to check one of the answers as correct so the question gets closed.
1

You should put your loop into <select> tag

<select class="form-control" id="sel1">
    <?php while ($itemsrow = $query->fetch()) : ?>
        <option><?php echo $itemsrow['Beschrijving']; ?></option>

1 Comment

lol thanks, I feel a bit stupid but thanks for replying so quickly!

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.