1

I know it's bad putting JavaScript inside PHP, but I'm rendering HTML with PHP and i want to check if row is empty and hide each of those HTML elements in a class. I know about Ajax, I just want this in PHP.

PHP:

if ($row['image1'] != ''){
echo   '<script>
        $(".image1Class").each(function() {
        $(this).show();
        });
    </script>';
}

3 Answers 3

1

I don't clearly understand what you want to do, but instead of

if ($row['image1'] != '')

you can use

if(!empty($row['image1']))
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it like this:

<script>
  $(document).ready(
    function (){
      <?php if ($row['image1'] != ''): ?>
        $(".image1Class").show();
      <?php else: ?>
        $(".image1Class").hide();
      <?php endif; ?>
    }
  );
</script>

1 Comment

You are hiding everything in that class, i want to hide only empty div, that's why i used $(this).
0

Hope this will help your

 $(".image1Class").each(function () { 
     if ($(this).attr("src") == "" ) 
     {
       $(this).css("display","none");  
      }
     });

2 Comments

You are suppose to evaluate the presence of a value from table with PHP first before performing operations, i want this to be done before anything is rendered to the browser.
So your condition should be done with PHP, to get the table state from sql.

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.