0

I am trying to design a website for my project and I got confused in the following.If I want to add a dropdown menu I do it with the following code.

<select>
    <option value="January"<?php if ($row[month] == 'January') echo ' selected="selected"'; ?>>January</option>
    <option value="February"<?php if ($row[year] == 'February') echo ' selected="selected"'; ?>>February</option>
</select>

But what I want to do is following.I have a database and bunch of data which I will query with a select statement.And I want to give all of them as an option to do this drop-down menu.How can I accomplish it?To simplify the question assume I have a query

$sql= "select Name FROM Course WHERE courseName='".$something."'"; 
$resulting=mysqli_query($connection,$sql);
$resultarray=mysqli_fetch_array($resulting);

How can I accomplish this?

Edit:Without a dropdown menu I would normally create a form in this way.

<form action="restaurantAction.php" method="post">
Restaurant Name:<input type="text" name="deleteRestaurant">
<input type="submit">
</form>

How can I apply this to a drop down menu based on the answer of Minh

1
  • 1
    foreach array item create an <option> Commented May 7, 2016 at 14:16

3 Answers 3

2

You can write like this

<select>
<?php foreach ($resultarray as $key => $value) {?>
    <option value="<?php echo $value?>"><?php echo $value?></option>
<?php } ?>
</select>

For your update

<form action="restaurantAction.php" method="post">
Restaurant Name:<input type="text" name="deleteRestaurant">
<select name="restaurantName">
<?php foreach ($resultarray as $key => $value) {?>
    <option value="<?php echo $value?>"><?php echo $value?></option>
<?php } ?>
</select>
<input type="submit">
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

Mask this to right answer if it is solutions :p. Thanks
Yeah it works.I also upvoted the other answers but I already used this one so I ll accept this as an answer.I still have one little question.How can I turn this into a form.I will edit question to show the format
1
<?php
// List filtered items
// Fetch data as associated array
<select>
while ($row = mysqli_fetch_assoc($resulting)) : ?>
<option value="<?php echo $resulting['game_name']?>"><?php echo $resulting['game_name']?></option>// $resulting['game_name'], or however called that field in the DB
</select>

Comments

1
      <?php                    
         $result=mysqli_query($con,"select Name FROM Resturant WHERE ResturantName='".$something."'");
         echo '<form action="restaurantAction.php" method="post">'
         echo 'Resutarant Name:<select name="ResturantName">';
            while ($row = mysqli_fetch_array($result)) {
               echo '<option value="'.$row['Name'].'">'.$row['Name'].'</option>';
              }
         echo '</select>';
         echo '<input type="submit">';
         echo '</form>';
      ?>       

Please Check this

1 Comment

What does this line accomplish?echo 'Resutarant Name:<select name="ResturantName">'; .Also there is a typo in Resutarant Name but it is not a big deal.Does this work without writing that line and using an already fetched array?.So is it ok if I just omit the first 3 lines and apply the code starting from while statement.It seems like ok to me

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.