Skip to content Skip to sidebar Skip to footer

How To Display Corresponding Mysql Columns In Html Based On Option Selected From Dynamic Dropdown List

I would like to ask for help in displaying the corresponding mysql table columns after the user selects an option from the dynamic dropdown box. I really don't know where I went wr

Solution 1:

If I understood correctly, you have one dropdown to show all the buildings.

Upon submission, show a table of deliveries based on the selected building.

That is 2 queries (you only have one).

Here's how I would do it:

// never a bad idea to turn on your PHP error reporting in development
error_reporting(E_ALL);
ini_set('display_errors', 1);

$con = new mysqli("localhost" ,"root" ,"" ,"user_databases");

// always query buildings bc we need it 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'];
    // query deliveries based on building; 
    // note the additional condition (I'm assuming building_ID is an integer)
    $dquery = mysqli_query($con, "
        SELECT  delivery_status, starting_time, arrival_time, duration, buildings.building_name, 
                location.location_name
        FROM    delivery_transaction, buildings, location 
        WHERE   delivery_transaction.building_ID = buildings.building_ID
        AND     delivery_transaction.location_ID = location.location_ID
        AND     buildings.building_ID = {$selectedbldg}
    ");

    // though it is not part of the scope of your question, 
    // you should probably use prepared statement above
}
?>


<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">
    <div class="table-responsive">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Delivery Status</th>
                    <th>Starting Time</th>
                    <th>Arrival Time</th>
                    <th>Duration</th>
                    <th>Location</th>
                </tr>
            </thead>
            <tbody>
            <?php if (isset($dquery) && mysqli_num_rows($dquery)) : ?>
                <?php while($row = mysqli_fetch_assoc($dquery)) : ?>
                    <tr>
                        <td><?= $row['delivery_status'] ?></td>
                        <td><?= $row['starting_time'] ?></td>
                        <td><?= $row['arrival_time'] ?></td>
                        <td><?= $row['duration'] ?></td>
                        <td><?= $row['location_name'] ?></td>
                    </tr>
                <?php endwhile ?>
            <?php else : ?>
                <tr>
                    <td>No results to display</td>
                </tr>
            <?php endif ?>
            </tbody>
        </table>
    </div>
</section>

Good to read:


Post a Comment for "How To Display Corresponding Mysql Columns In Html Based On Option Selected From Dynamic Dropdown List"