Iam Coding A Small Portal And Need Help

This Is My Code

echo "<div class='posterdecoration'><img src='$row[scr1]' width='640' alt='$row[name]' /></div>";
echo "<div class='posterdecoration'><img src='$row[scr2]' width='640' alt='$row[name]' /></div>";

When I Put Scr1 + Scr2 From Site Panel

It Show On The Site

I Need To Use <If Condition>

On The Scr2

So If I Didnt Put The Field Scr2 It Dont Appear

I Used To Made It In vBulletin Forum

<If Condition="#post[field10]"> Something </if>

But Here In My Code I Dont Know How To Do It

All I Need To Do Is If The $row2 Is Empty Dont Show It In The Page

Dani AI

Generated

— the goal is to not output the entire .posterdecoration element when scr2 is empty. Both and pointed you in the right direction: test the field before you print it. Two clarifications that often solve the “empty box but no image” problem:

  1. Database values can contain whitespace or the literal string "0", so use trim() and a strict empty check instead of only isset() or != "".
  2. Always quote array keys (use $row['scr2'], not $row[scr2]) to avoid PHP notices and unexpected behavior. Also escape output to avoid XSS.

A compact, robust pattern (PHP 7+) is:

$src = trim($row['scr2'] ?? '');
if ($src !== '') {
    echo '<div class="posterdecoration">'
       . '<img src="' . htmlspecialchars($src, ENT_QUOTES, 'UTF-8') . '" width="640" alt="' . htmlspecialchars($row['name'] ?? '', ENT_QUOTES, 'UTF-8') . '" />'
       . '</div>';
}

If running older PHP, replace the null-coalescing ?? with an isset() check.

Extra notes:

  • If images may be local files, you can optionally verify existence with file_exists() or getimagesize() (be aware these are I/O-heavy). For external URLs, use filter_var(..., FILTER_VALIDATE_URL) or fetch headers.
  • Debugging tip: when a div still appears, view the page source or var_dump($row) to see the exact string stored (hidden whitespace is common).
  • Do not rely on client-side CSS to “hide” empty markup; preventing the server from outputting the container is cleaner and avoids layout glitches.

These changes ensure the whole container is omitted when there is no usable scr2 value and keep output safe and predictable.

Recommended Answers

All 4 Replies

Try this:

echo "<div class='posterdecoration'><img src='$row[scr1]' width='640' alt='$row[name]' /></div>";
echo (isset($row['scr2'])) ? "<div class='posterdecoration'><img src='$row[scr2]' width='640' alt='$row[name]' /></div>" : NULL;

bye :)

commented: Thanks For Reply Plz Continue With Me +2

First Thanks Sir For Reply

After Edit

When I Add Only Scr1

And Leave Scr2 Empty

The Field Appear But With No Image Inside It

I Need The Whole Class Not Appear If $Row2 It Empty

I Mean I Dont Want This To Appear

<div class='posterdecoration'>

Try this

if($row[scr1]!="")
{
echo "<div class='posterdecoration'><img src='$row[scr1]' width='640' alt='$row[name]' /></div>";
}
if($row[scr2]!="")
{
    echo "<div class='posterdecoration'><img src='$row[scr2]' width='640' alt='$row[name]' /></div>";
}
commented: You Are Amazing Thanks So Much +2

You Are Amazing Thanks So Much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.