0

I am having some trouble with getting this to work. I have an image gallery that is calling for the image link from the SQL database. Which works beautifully, however, if a user is only using 'pic1' and 'pic2' but 'pic3' and 'pic4' are empty in the database, I want to display a placeholder image instead. Any recommendations to do that?

    <li><a class="ns-img" href="<?php echo $row_Recordset1['pic1']; ?>"></a></li>
    <li><a class="ns-img" href="<?php echo $row_Recordset1['pic2']; ?>"></a></li>
    <li><a class="ns-img" href="<?php echo $row_Recordset1['pic3']; ?>"></a></li>
    <li><a class="ns-img" href="<?php echo $row_Recordset1['pic4']; ?>"></a></li>
                    
5
  • @user3783243 Thank you for your reply! Dreamweaver is kicking the line back to me as broken, any suggestions? <li><a class="ns-img" href="<?php if(empty($row_Recordset1['pic4'])) { echo 'donate.png' }{ else echo $row_Recordset1['pic4'];?>"></a></li> Commented Apr 17, 2021 at 11:10
  • @Cyndi There were a few typos there. I've corrected and moved to an answer. Commented Apr 17, 2021 at 11:20
  • 1
    On second thought is $row_Recordset1['pic1'] simply a path to an image? Commented Apr 17, 2021 at 11:40
  • This question desperately needs more details about the actual problem. This is not simply a matter of displaying placeholder image. The questioner phrases the question completely differently in the comments to each answer. Commented Apr 17, 2021 at 12:48
  • @Steven correct! Commented Apr 17, 2021 at 13:16

3 Answers 3

2

I would do the check in SQL:

case when pic4 = '' then 'someimage.jpg' else pic4 end as pic4

or

case when pic4 is null then 'someimage.jpg' else pic4 end as pic4

if the data is null when not set.

This can be done in PHP as well:

if(empty($row_Recordset1['pic4'])) { 
    echo 'someimage.jpg';
} else {
    echo $row_Recordset1['pic4'];
    
}

or even

echo (empty($row_Recordset1['pic4']) ?  'someimage.jpg' : $row_Recordset1['pic4']);

for shorthand version.

Since your string has spaces you need to use trim as well:

echo (empty(trim($row_Recordset1['pic4'])) ?  'someimage.jpg' : $row_Recordset1['pic4']);

can be done in SQL as well:

case when trim(pic4) = '' then 'someimage.jpg' else pic4 end as pic4
Sign up to request clarification or add additional context in comments.

10 Comments

var_dump($row_Recordset1['pic4']) gives what?
That must give back a variable type, length, and value.
What is the var_dump result. I need to know that to know why it isnt working.
NULL evalutes to true for empty() so you should have someimage.jpg returned in that scenario. Is it just the attribute just empty or what is there? Demo of how it should be working 3v4l.org/0CEZZ
So do you want no output if there isn't an image, or a placeholder? This should give placeholder.
|
2

The PHP ternary or conditional operator could help you

<li>
    <a href="<?= $img ?: './images/default.jpg' ?>"></a>
</li>

This operator says, that if $img is empty or null the default image will be used.

5 Comments

Unfortunately this does not work. There is no original place holder image. I would need to either put a link to an image into the SQL database, which is fine, and name it something like $row_Recordset1['temp'], as an example, but I am not sure how to tell it to display $row_Recordset1['temp'] if $row_Recordset1['pic4'] is empty
Quote from the original post: > if a user is only using 'pic1' and 'pic2' but 'pic3' and 'pic4' are empty in the database, I want to display a placeholder image instead.
The LINK to the images, are contained in columns that are named 'pic#'. If the 'pic#' column is empty, meaning there is NO LINK in the table, then I want to replace that with a specific place holder. Your recommendation ignores the use of SQL entirely, which is the critical component to the question.
You should ask more detailed questions.
You don't seem to understand the question.
0

This can be done in PHP as well:

<?php
   if($row_Recordset1['pic4'] == NULL){
       <li><a href="<?= $img ?: './images/default.jpg' ?>"></a></li>
   } else {
      <li><a href="<?= $img ?: './images/anyimages.jpg' ?>"></a></li>
   }
?>

You can also use while or forloop into same code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.