PDA

View Full Version : problem with: field empty - print nothing, else print ...


DeMolen
05-26-2008, 10:56 AM
Dear php coders,

I'd be very grateful for any help with the following problem. I'm setting up a page which calls in data from a linked mySQL database. If a certain field is empty (in this case 'duration'), it shouldn't print anything, if the field has content it should print the content plus an apostrophe and then continue to list the rest of the data for each record.

This is my faulty script - I'd be pleased if anyone can correct it for me. I'm still a beginner with php, as may be obvious if I'm making a silly mistake here.

It's the 6 lines starting if(trim ... that I'm not sure about, the rest works ok.

Many thanks in advance for any assistance.

<?
while( $row=mysql_fetch_assoc($recordset)) {
print '<tr><td valign="top" width="150"><b>'. $row['title'] .'</b></td><td colspan="2" valign="top">'. $row['subtitle'] .'</td><td></td></tr>
<tr><td></td><td valign="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>'. $row['date'] .'&nbsp;&nbsp;';

if (trim($duration)=='') {
print '';
}
else {
print $row['duration'] .'&rsquo;&nbsp;&nbsp;';
}

print $row['format'] .'&nbsp;&nbsp;'. $row['recording'] .'</td><td align="right" valign="top"><i>'. $row['instrumentation'] .'</i>&nbsp;&nbsp;&nbsp;<td align="right" valign="bottom">&pound;'. $row['price'] .'</td></tr>';
}
?>

tomws
05-26-2008, 06:04 PM
if (trim($duration)=='') {
print '';
}
else {
print $row['duration'] .'&rsquo;&nbsp;&nbsp;';
}


Has $duration been defined outside of this loop? If not, you may mean to be testing $row['duration'].

If that's the case, you can test against an empty string like you're doing, or you can test with empty(trim($row['duration'])).

Furthermore, your existing test is meaningless, if I'm understanding it correctly. Essentially, you're saying that if the field is empty, print nothing - in other words, do nothing. A better test might be:
if (!empty(trim($row['duration']))) {
print $row['duration'] .'&rsquo;&nbsp;&nbsp;';
}

DeMolen
05-26-2008, 06:45 PM
Many thanks tomws for your kind reply.

I just needed to change - if (trim($duration)=='')
into - if (trim($row['duration'])=='')
and it worked perfectly.

When I tried the 2nd script including !empty, it returned the following message:
Fatal error: Can't use function return value in write context ... on line 46 - the if (!empty ... line.

Anyway my problem has been solved and I'm enormously grateful.

Warm greetings from Amsterdam!

tomws
05-26-2008, 07:57 PM
When I tried the 2nd script including !empty, it returned the following message:
Fatal error: Can't use function return value in write context ... on line 46 - the if (!empty ... line.


Oh! Sorry about that. It's an error I've made before. As I recall, when using empty(), it must be passed a variable, not another function. It can be easily modified, if you decide to try it that way.

DeMolen
05-27-2008, 06:51 AM
As an eager learner I'd like to see how that empty() option would work, if you have the time.

Thanks

tomws
05-27-2008, 12:59 PM
No problem. The first resource for any php function is the manual. In the case of empty(), http://us.php.net/empty.

Now, to change the if/else combo, you could do something like this, combining into a single if statement (modifying my incorrect version above):
$duration = trim($row['duration']);
if (!empty($duration)) {
print $duration .'&rsquo;&nbsp;&nbsp;';
}

empty() is not the solution to use, though, if the duration can be zero. Check the manual page for the values the are considered empty values.