Topshed Posted November 9, 2013 Share Posted November 9, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/ Share on other sites More sharing options...
.josh Posted November 9, 2013 Share Posted November 9, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/#findComment-1457647 Share on other sites More sharing options...
alpine Posted November 9, 2013 Share Posted November 9, 2013 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 ( '' ). Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/#findComment-1457661 Share on other sites More sharing options...
Topshed Posted November 10, 2013 Author Share Posted November 10, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/#findComment-1457707 Share on other sites More sharing options...
cyberRobot Posted November 10, 2013 Share Posted November 10, 2013 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'] : ''; ?> Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/#findComment-1457708 Share on other sites More sharing options...
.josh Posted November 10, 2013 Share Posted November 10, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/#findComment-1457712 Share on other sites More sharing options...
cyberRobot Posted November 10, 2013 Share Posted November 10, 2013 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']); Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/#findComment-1457713 Share on other sites More sharing options...
.josh Posted November 10, 2013 Share Posted November 10, 2013 @cyberRobot I *assume* (yeah, I know what they say) all that is sorted, since the OP said the problem was getting to only show when it was set. IOW it sounded like the data showed up alright when it was there.. but he didn't want it to output anything if it wasn't. Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/#findComment-1457714 Share on other sites More sharing options...
Topshed Posted November 10, 2013 Author Share Posted November 10, 2013 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... Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/#findComment-1457719 Share on other sites More sharing options...
.josh Posted November 10, 2013 Share Posted November 10, 2013 okay well uh, in your code you posted, you have an opening html comment tag <!-- N.L.Browne(print... and no closing comment tag anywhere so it's commenting out everything past that point Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/#findComment-1457720 Share on other sites More sharing options...
Topshed Posted November 10, 2013 Author Share Posted November 10, 2013 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 http://www.britishsteam.com/gwr/tanks/9700.php Sorry Topshed Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/#findComment-1457736 Share on other sites More sharing options...
Topshed Posted November 10, 2013 Author Share Posted November 10, 2013 http://www.britishsteam.com/gwr/tanks/9700.php Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/#findComment-1457737 Share on other sites More sharing options...
mac_gyver Posted November 10, 2013 Share Posted November 10, 2013 your actual variable name is apparently $row (no s on the end.) the reason the replies have used $rows is because that is what you yourself used in some of your code (we only see the information you supply.) Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/#findComment-1457754 Share on other sites More sharing options...
Solution Topshed Posted November 13, 2013 Author Solution Share Posted November 13, 2013 Thank you all for your help and patience Problem solved Regards Topshed Quote Link to comment https://forums.phpfreaks.com/topic/283746-echo-only-if-field-is-not-empty/#findComment-1458152 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.