0

Hi,
I have this situation

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
          $images[] = [
               'image' => $row['image']
          ];
}
function image_html($image) {
      return '<img src="' . $image['image'] . '" class="img-responsive" />';
}

echo '<div class="small-box">            
         '. image_html($images[0]) .'
      </div>
      <div class="small-box">            
         '. image_html($images[1]) .'
      </div>';

      // some more divs

Is it possible to check if there is no image_html($images[0]) to hide the <div class="small-box"> because right now I got empty box on the page?

Edited by pritaeas: Moved to PHP.

3
Contributors
6
Replies
36
Views
2 Years
Discussion Span
Last Post by ryantroop
0

Like this?

foreach ($images as $image)
{
    echo '<div class="small-box">' . image_html($image) . '</div>';
}
0

Thank's for the answer but I need to use my current source because I have different columns with different image size etc.
So along with small-box I have also medium-box and large-box.

p.s. I also don't see how your answer will hide( or place default image ) if there is no record in database table?

Edited by JasonWung

0

Correct, not hide, it doesn't display any. If you want to hide one:

$style = '';
if (!isset($images[0]))
{
    $style = ' style="display:hidden" ';
}

echo '<div class="small-box"' . $style . '>' . image_html($images[0]) . '</div>';
0

Is it possible to apply this to whole $images[] ... $images[0], $images[1].... and so on. I mean do I need to write something like:

if (!isset($images[0]) && !isset($images[1]) && !isset($images[1]) && !isset($images[N]))
{
....
}

or there is another solution?

1
if (count($images) == 0)

It's hard to answer without knowing the bigger picture.

Edited by pritaeas

1

I think what priteas is trying to get across to you is that you need to change your thinking.

Consider it this way: "only display the box when there are images to display."

That way, you are not trying to "fix" something post render or mid build.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.