Skip to content
PHP Logo

How can PHP be used to dynamically hide table rows based on database content?

To dynamically hide table rows based on database content using PHP, you can retrieve the necessary data from the database and then use conditional statements to determine whether to display each row or not. This can be achieved by looping through the database results and outputting the table rows only for the desired data.

php
<?php
// Connect to your database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Query to retrieve data from the database
$query = "SELECT * FROM your_table";
$result = mysqli_query($connection, $query);

// Output the table with rows based on database content
echo "<table>";
while ($row = mysqli_fetch_assoc($result)) {
    if ($row['column_to_check'] == 'desired_value') {
        echo "<tr>";
        echo "<td>" . $row['column1'] . "</td>";
        echo "<td>" . $row['column2'] . "</td>";
        echo "</tr>";
    }
}
echo "</table>";

// Close the database connection
mysqli_close($connection);
?>