skip to Main Content

Sometimes the $data[‘filename’] is null or blank. I need the img src="pdf.png" to change to a different image src="no-pdf.png" , or hide when the $data[‘filename’] is null or blank.

<table align="center" border="0" style="font-size: 14px;">
<?php
    $idnum=$_POST['idnum'];
include "dbConn.php"; 
$records = mysqli_query($db,"select * from scan where idnum like '%$idnum%'");
while($data = mysqli_fetch_array($records))
{
?>
  <tr align="center">
    <td><?php echo $data['idnum']; ?>&nbsp;&nbsp;</td>
    
    <td id="test"><a href="https://mysite.co.za/image/<?php echo $data['filename']; ?>" target="popup" 
  onclick="window.open('https://mysite.co.za/image/<?php echo $data['filename']; ?>','popup','width=600,height=600'); return false;">
    <img src="pdf.png"  style="width:30px;height:40px;"></a>&nbsp;&nbsp;</td>
    
  </tr> 
<?php
}
?>
</table>

<script>
$(document).ready(function(){
  if $("#test") = "https://mysite.co.za/image/" then
   $("#test").hide();
</script>

2

Answers


  1. Try this.

    <td id="test">
      <?php if ($data['filename'] == null || $data['filename'] == '') { ?>
        <img src="no-pdf.png" style="width:30px;height:40px;">
      <?php } else { ?>
        <a href="https://mysite.co.za/image/<?php echo $data['filename']; ?>" target="popup"
          onclick="window.open('https://mysite.co.za/image/<?php echo $data['filename']; ?>','popup','width=600,height=600'); return false;">
          <img src="pdf.png" style="width:30px;height:40px;">
        </a>
      <?php } ?>
    </td>
    
    Login or Signup to reply.
  2. Try like this,

    <table align="center" border="0" style="font-size: 14px;">
    <?php
    $idnum = $_POST['idnum'];
    include "dbConn.php";
    $records = mysqli_query($db, "select * from scan where idnum like '%$idnum%'");
    while ($data = mysqli_fetch_array($records)) {
        ?>
      <tr align="center">
        <td><?php echo $data['idnum']; ?>&nbsp;&nbsp;</td>
    
        <td id="test<?php echo $data['idnum']; ?>">
          <?php if (!empty($data['filename'])) : ?>
            <a href="https://mysite.co.za/image/<?php echo $data['filename']; ?>" target="popup" 
            onclick="window.open('https://mysite.co.za/image/<?php echo $data['filename']; ?>','popup','width=600,height=600'); return false;">
              <img src="pdf.png" style="width:30px;height:40px;">
            </a>
          <?php else : ?>
            <img src="no-pdf.png" style="width:30px;height:40px;">
          <?php endif; ?>
        &nbsp;&nbsp;</td>
    
      </tr>
    <?php
    }
    ?>
    </table>
    
    <script>
    $(document).ready(function () {
      $('td[id^="test"]').each(function () {
        var filename = $(this).find('img').attr('src');
        if (filename === "pdf.png") {
          $(this).hide();
        }
      });
    });
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search