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?