Jump to content

Echo only if field is not empty


Topshed
Go to solution Solved by Topshed,

Recommended Posts

HI some help badly needed please

 

I have an SQL database that I use to furnish details about any  picture in a gallery

 

One of the fields is for Notes and I want to only echo if it is not null

in it's plain form it looks like this

 

     <td colspan='5'>Notes <?php echo $row['Notes']; ?></td>

 

The code I am trying to get to work follows

<td colspan='5'><?php if (is_null($rows['Notes']))
         echo "";
     else ?>
     <td colspan='5'>Notes <?php echo $row['Notes']; ?></td>
  </tr>

Thanks

Topshed

 

Link to comment
Share on other sites

If all you are doing is echoing out an empty string if there aren't notes, then there's no reason to write for that; just echo out the notes if there are notes:

 

 

if ( isset($rows['Notes'])&&trim($rows['Notes'])!='' )
  echo "<td colspan='5'>Notes: {$row['Notes']}</td>";

 

the first part checks if the 'Notes' array index is set, and the 2nd part checks to make sure it's not just set to an empty string (or just stet to whitespace chars)

 

 

Also, I don't think your html table is right. Looks like that first tag should be a <tr> not <td>

Link to comment
Share on other sites

You can also define a variable, and echo that. This means the variable will always be defined.

$notes = (!empty($rows['notes'] ? $rows['notes'] : '');

echo $notes; // if $rows['notes'] contains info, it will be printed, else it prints out nothing ( '' ).
Link to comment
Share on other sites

Thank you both for your replies, but I cannot get a result from either

 

administrator your code snippit  still does not print out the info when it is there

  <tr>
  <?php 
  if ( isset($rows['Notes'])&&trim($rows['Notes'])!='' )
  echo "<td colspan='5'>Notes: {$row['$Notes']}</td>";
?>
 </tr>

Alpine your code produces an error which I do not understand

$notes = (!empty($rows['notes'] ? $rows['notes'] : '');

 

Parse error: syntax error, unexpected '?', expecting ')' in /home4/topshed/public_html/britishsteam.com/gwr/tanks/9700.php on line 77

 

 

 

So some more assistance would be very nice

 

Regards  Topshed
 

Link to comment
Share on other sites

You can also define a variable, and echo that. This means the variable will always be defined.

$notes = (!empty($rows['notes'] ? $rows['notes'] : '');

echo $notes; // if $rows['notes'] contains info, it will be printed, else it prints out nothing ( '' ).

 

Note that the parenthesis are out of place. The above should look more like the following:

 

<?php
$notes = (!empty($rows['notes'])) ? $rows['notes'] : '';
?>
Link to comment
Share on other sites

administrator your code snippit still does not print out the info when it is there

because you changed the code I posted. Inside the echo you changed it from {$rows['Notes']} to {$rows['$Notes']} which is not the same thing unless $Notes happens to contain the value "Notes".. which I'm guessing it doesn't, seeing as how you said it don't work.

Link to comment
Share on other sites

administrator your code snippit  still does not print out the info when it is there

 

Could you provide a little more context for the code? For example, are you adding the code so that $rows is actually defined?

 

Note that you could try adding the following (temporarily) to see what the variable contains:

 

var_dump($rows['Notes']);
Link to comment
Share on other sites

Hi Guru,

 

A block of the code follows

<table id='w1020'>
  <tr>
    <th>BR No.</th>
    <th>GWR No.</th>
    <th>Pre GWR
     No.</th>
    <th colspan='2'>Loco Name</th>
  </tr>
  <tr>
   <?php echo "<td>".$row['br_no']."</td>"; ?> 
   <?php echo "<td>".$row['other_no']."</td>"; ?>
   <?php echo "<td>".$row['prev']."</td>"; ?>
   <?php echo "<td colspan='2'>".$row['loco_name']." </td>"; ?>
  </tr>
  <tr>
   <th colspan="2">Build Details</th>
   <th colspan='2'>Withdrawal Details</th>
   <th>Cut / Fate / Preservation Details</th>
  </tr>
  <tr> 
  <?php echo "<td colspan='2'>".$row['builder']." no.".$row['build_no'].", ".$row['mm_build']."-".$row['yy_build']."</td>"; ?> 
  <?php echo "<td colspan='2'>".$row['mm_wdn']."-".$row['yy_wdn']." <b>(".$row['code'].")</b>".$row['last_shed']."</td>"; ?> 
  <?php echo "<td>".$row['mm_cut']."-".$row['yy_cut'].", ".$row['cut']."</td>"; ?>
  </tr>
  <tr>
    <th colspan='5'><strong>Photograph Copyright © </strong><!-- N.L.Browne(print)<strong>  all rights reserved</strong></th>
  </tr>
  <tr>
    <td colspan='5'>A most un-GWR type with outside Walschaerts valve gear, on shed at Southall Apr-1957</td>
</tr>
  <tr>
  <td colspan='5'><?php echo $row['Notes']; ?></td>
</tr>
  <tr>
    <th colspan='5'><img src="image/1505.jpg" alt="pannier tank 1805" width="1020" height="680"/></th>
  </tr>
</table>






I Hope this clarifies the problem

Topshed...

Link to comment
Share on other sites

Sorry that was sloppy, I make dreamweaver templates because any one gallery can have 100plus scripts. and I decided to clean up the comments that only Dreamweaver reads, and left that little piece behind 

so a false lead so to speak.

How the & got it your code I do not know I always use cut and paste

the amended bit shows

 

<tr>
  <?php 
  if ( isset($rows['Notes'])&&trim($rows['Notes'])!='' )
  echo "<td colspan='5'>Notes: {$row['Notes']}</td>";
?>
 </tr>
 
but alsa still nothing shows
 
BTW it should show "testing"
 
URL
 
Sorry
Topshed
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.