I have tried to do this a few ways, but it doesn't seem possible. The scenario is this...
I have a database of venues and facilities that each venue has. I.e. Bar 1 serves beer and food (etc). I want to be able to choose a city from a drop down list (easy bit, i have that working!), and from the selected cities only offer checkbox choices that are actually available in the city, so for example if in a city there are 5 restaurants, all serve food, some serve beer, some serve red wine, but none serve white wine, after the city is selected, I want to only see checkboxes for food, beer and red wine (even though white wine is in theory an option on the DB). This is the scenario but using pubs and pub games. I have this code so far, which gets the input from the city drop down list:
<select name="publocation">
<option value="none">Where will you be playing...</option>
<?php
$servername = "*****";
$username = "*****";
$password = "*****";
$dbname = "*****";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
foreach($conn->query("SELECT distinct city from Establishments order by city") as $row) {
print "<option value=".$row['city'].">".$row['city']."</option>";
}
$chosencity = $row['city'];
$conn = null;
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
</select>
So far, so good.
What I then want to do based on that is choose the activities available - there are many, but just using darts here as the example:
<?php
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
foreach($conn->query("SELECT * from Establishments where city like '".$chosencity."'") as $row) {
if ($row['darts'] = '1') {
print "<input type='checkbox' name='adddarts' value='yes'>Darts</input>";
}
}
$conn = null;
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
The problem I have is that of course, that will print darts x times, where x is the amount of places with darts in the chosen city. The second problem is if someone then chooses a different city from the drop down list, the checkboxes stay the same and do not update - is there any way to do this in PHP?
Thanks in advance, Brett
Aucun commentaire:
Enregistrer un commentaire