So I'm using php and trying to echo some stuff to <select>
options from database.
First I want to echo let's say all Cars to the first <select>
(I can do this) but then I want to echo the SELECTED Car's models to the next <select>
and I can't do that. I'm pretty novice with all of this stuff. Also I'm using MVC.
From view:
<select name="card_id">
<?php foreach ($cars as $car) {
if (isset($car->id)) {
echo "<option value=\"" . $tcar->id . "\">";
echo $car->car . "</option>\n";
}
} ?>
</select>
<p>Choose model</p>
<select name="model_id">
<?php foreach ($models as $model) {
if (isset($model->id)) {
echo "<option value=\"" . $model->id . "\">";
echo $model->model . "</option>\n";
}
} ?>
</select>
From the model view:
public function getAllCars()
{
$sql = "SELECT * FROM cars";
$query = $this->db->prepare($sql);
$query->execute();
return $query->fetchAll();
}
I don't know how the model to pick the car model should look like, but I guess it would be something like this:
public function getmodels()
{
$sql = "SELECT * FROM models WHERE
$query = $this->db->prepare($sql);
$query->execute();
return $query->fetchAll();
}
Also I don't fully understand the part that's inside the select tag so if you could explain it thoroughly to me it would be nice.