Show Hide div if, if statement is true

Show Hide div if, if statement is true

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

Controlling DOM Visibility with Conditional Logic in PHP

You have a working query that fetches data from your database, but you're facing a common layout issue: how to ensure an HTML element only renders when specific conditions are met. In your current setup, the div appears regardless of whether the database returns any results. The core problem isn't just about executing PHP logic; it's about understanding when that execution happens relative to the browser rendering the page.

Your snippet uses the legacy mysql_* functions and executes the query:

<?php
    $query3 = mysql_query($query3);
    $numrows = mysql_num_rows($query3);
    if ($numrows > 0) {
        $fvisit = mysql_fetch_array($result3); // Note: Variable name mismatch in original snippet
    } else {
        // Logic is empty here
    }
?>

Even though the else block exists, the code following your PHP tags (like <div>...</div>) will execute every single time. This happens because PHP only stops output or alters behavior if you explicitly prevent it. To hide the div, you must wrap the HTML in a conditional block that evaluates to false when the database query returns zero rows.

The most direct approach is to move your HTML inside the if statement itself. If $numrows is greater than 0, the browser sees the code and renders it. If the condition fails, the browser never encounters the tag at all. This is often the cleanest solution for simple inclusion logic:

<?php if ($numrows > 0): ?>
    <div id="results">
        <!-- Content goes here -->
        <?php echo htmlspecialchars($fvisit['column_name']); ?>
    </div>
<?php endif; ?>

However, there is a critical pitfall in your provided source material: the variable name mismatch. You assign the result to $fvisit inside the if block using $result3, but your query variable is $query3. If you rely on $fvisit outside this block without checking its existence first, your script might throw a "Undefined variable" warning if the else branch was taken.

To handle this more robustly, especially in modern development contexts, consider moving the logic into a dedicated function or using null coalescing operators to avoid errors when data is missing. Furthermore, you are relying on deprecated functions. The mysql_* extension was removed from PHP core due to significant security vulnerabilities and lack of support for prepared statements.

If you are maintaining legacy code, your immediate fix is strictly structural: wrap the HTML in the condition. But if you are building new features or refactoring, you should migrate to mysqli or PDO. Using prepared statements prevents SQL injection, which is a severe risk when fetching user data like "visits."

Here is how that logic looks with modern standards:

<?php
$stmt = $pdo->prepare("SELECT * FROM visits WHERE ...");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<?php if (!empty($rows)): ?>
    <div class="visit-details">
        <!-- Safe to render HTML here -->
    </div>
<?php else: ?>
    <!-- Optional: Show a fallback message or empty state -->
    <div class="no-data">No visits found.</div>
<?php endif; ?>

This approach separates the data fetching logic from the presentation layer effectively. It ensures that if your query returns nothing, the browser simply skips the div entirely, preventing layout shifts or broken UI states. Always verify that your variable names match exactly between assignment and usage to prevent silent failures in your conditional checks.

Our network