1

I am trying to use php code within html to grab content from a database to populate the values for a drop down menu (see below). When I run the php script on its own (test.php) I get all the expected values. When nested within html I get nothing but a single blank value under Select a Species. I would expect this to run through the while loop more than once as there are about 7 values returned and I would also expect the contents to include data derived from the table.

What I am attempting is possible correct?? Is it just an error with code (no errors popping up in the logs)

<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
    <option id = "0">-- Select a Species -- </option>
    <?php
        $con = mysqli_connect("localhost","xxx","xxxx","databases");
        $result = mysqli_query($con, "SELECT * FROM bc_species WHERE 1"); 
        while ($row = $result->fetch_assoc()){
     ?>
     <option><?php echo $row['species'];?></option> 
     <?php } ?>
</select>
2
  • whats that json_encode for? just echo it combining the markup option Commented Mar 2, 2016 at 5:09
  • check where 1 in this SELECT * FROM bc_species WHERE 1. Commented Mar 2, 2016 at 5:42

4 Answers 4

1
    <option><?php echo $row["species"]; ?></option>            
Sign up to request clarification or add additional context in comments.

Comments

0
<?php 
        $optionData = '<option id = "0">-- Select a Species -- </option>';

        $con = mysqli_connect("localhost","xxx","xxxx","databases");
        $result = mysqli_query($con, "SELECT * FROM bc_species WHERE 1"); 
        while ($row = $result->fetch_assoc()){
          $optionData .= "<option>".$row['species']."</option>" ;
        } 
?>
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
    <?php echo $optionData;?>
</select>

Comments

0

//Put your fetched data on an array then loop them like this

<?php 
                        foreach ($list as $data  => $item) {
                    ?>
                        <option value="<?php echo $data?>">
                            <?php echo $data?>
                        </option>
                   <?php
                        }
                    ?>

Comments

0

This should work:

<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
    <option id = "0">-- Select a Species -- </option>
    <?php
        $con = mysqli_connect("localhost","xxx","xxxx","databases");
        $result = mysqli_query($con, "SELECT * FROM bc_species"); 
        while ($row = $result->fetch_assoc()){
           echo '<option>'. $row["species"].'</option> 
     } ?>
</select>

You were not iterating through the values within the HTML <option> tags, so I moved those into the while loop. (notice the PHP section includes echoing those tags).

I also removed the unnecessary "SQL injection-looking" where 1 clause from your query.

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.