I would like to ask help in displaying the number of times a location value appeared in a table that corresponds to an option user clicks from dropdown list.
Here's what I did so far: `
$con = new mysqli("localhost" ,"root" ,"" ,"user_databases");
//query buildings table for the dropdown
$bquery = mysqli_query($con, "SELECT building_ID, building_name FROM buildings");
$selectedbldg = null;
// if the form was submitted
if (!empty($_POST['bldg']))
{
// store selected building_ID
$selectedbldg = $_POST['bldg'];
//count instances of location_name in delivery_transaction table;
$count = mysqli_query($con, "
SELECT location_ID, COUNT(location_ID)
FROM delivery_transaction
GROUP BY (location_ID)
");
}
?>
<!--Building dropdown contents-->
<form name="bldg_form" method="post" action="">
<select name="bldg">
<option value="">Choose Building</option>;
<?php while ($row = mysqli_fetch_assoc($bquery)) : ?>
<option value="<?= $row['building_ID'] ?>" <?= $row['building_name'] == $selectedbldg ? 'selected' : '' ?>><?= $row['building_name'] ?></option>
<?php endwhile ?>
</select>
<input type="submit" name="view" />
</form>
<section class="row text-center placeholders">
<!--For the table to display everytime user selects an option from dropdown-->
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Location</th>
<th>Number of Visits</th>
</tr>
</thead>
<tbody>
<!--PHP alternative syntax for control structures: if; open brace-a colon (:) and the closing brace-endif-->
<!--the isset function to check if variable has value assigned or not ; mysqli_num_rows returns the number of rows in the result set-->
<?php if (isset($count) && mysqli_num_rows($count)) : ?>
<?php while($row = mysqli_fetch_assoc($count)) : ?>
<tr>
<td><?= $row['location_ID'] ?></td>
<td><?= $row['COUNT(location_ID)'] ?></td>
</tr>
<?php endwhile ?>
<?php else : ?>
<tr>
<td>No results to display</td>
</tr>
<?php endif ?>
</tbody>
</table>
</div>
</section>`
Which I believe is literally wrong as it displays all the location IDs:
Please help :(