从MySQL获取选择选项。

What I am trying to do is to populate an HTML select from a MYSQL query result.

There are two files, this is Food.php:

public function selectBrand()
{
    // creating a database connection
        $this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    $query = $this->db_connection->query("SELECT DISTINCT brand_food FROM food;");
    $result_row = $query->fetch_object();
    echo $result_row->total;
}

And this is products.php :

<?php
// include the configs / constants for the database connection
require_once("model/db.php");
require_once("model/Food.php");
$brand = new Food();?>    

<div class="panel-body">    
<!-- Filter -->
<center>
    <form method="post" action="index.php" name="filter_form"> 
        <div class="form-group">
            <div class="input-group">
            <span class="input-group-addon" style="box-shadow: 0 0 1px 1px darkgray;"><strong>Brand</strong></span>
            <select id="filter_brand" class="form-control" name="brand" style="box-shadow: 0 0 1px 1px darkgray;"  required >
            <?php
            foreach($brand as $brand->selectBrand()){
            ?>
            <option value="<?php echo $brand ?>" ><?php echo $brand ?></option>
            <?php } ?>
            </select>
            </div>
        </div>
        <input type="submit"  name="Filter" value="Filter" class="btn btn-block btn-default"/>
    </form>
</center>
<!-- /Filter -->    

I´ve made it work before but I haven´t code for a while. Can you please help me?

Your code has 2 problems:

  1. The model return unexpected result, it should be an array of brands
  2. to render the HTML output, the foreach loop syntax is incorrect

For Model:

public function selectBrand()
{
    $this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    $query = $this->db_connection->query("SELECT DISTINCT brand_food FROM food;");
    $brands = array();
    while($obj = $query->fetch_object()){ //fetch each object by while loop
        array_push($brands, $obj->brand_food); //push the value into $brands
    }
    return $brands; //return the array
}

For View (Render):

<?php foreach($brand->selectBrand() as $brand){ ?>
    <option value="<?php echo $brand ?>" ><?php echo $brand ?></option>
<?php } ?>

This should work

public function selectBrand()
{
        // creating a database connection
        $this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        $query = $this->db_connection->query("SELECT DISTINCT brand_food FROM food;");
        $result_row = $query->fetch_object();
        return $result_row->brand_food;
}

<?php foreach($brand->selectBrand() as $brand){ ?>
     <option value="<?php echo $brand ?>" ><?php echo $brand ?></option>
<?php } ?>

Wrong way you are using foreach, try following code. I aspect that your brand is arrays.

        <?php
        foreach($brand->selectBrand() as $select){
        ?>
        <option value="<?php echo $select; ?>" ><?php echo $select; ?></option>
        <?php } ?>