if (empty) don't want comma to print

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jarow
Forum Commoner
Posts: 83
Joined: Tue Jan 28, 2003 2:58 am

if (empty) don't want comma to print

Post by jarow »

the following prints out just fine if there is an author and year in the database for the field "subgenero". The problem is if there is no author and year in the database the "," between the two fields still prints out even though the fields are empty. Is there a way to write this so that if the author and year fields are empty the "," won't print out either.

Thanks

Code: Select all

<?php
if($row_rsspecies['subgenero']) {
   if($base_subgenero != $row_rsspecies['subgenero']) { 
      echo str_repeat("<dd>",$i++).'Subgen'. " " .$row_rsspecies['subgenero']." ".$row_rsspecies['author'].",".$row_rsspecies['year']; 
      $base_subgenero = $row_rsspecies['subgenero']; 
   } else {
      $i++;
   }
} else {
   $base_subgenero = '';
}

?>
Thanks
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Add something like this maybe:

Code: Select all

<?php
IF (!empty($row_rsspecies['author']) AND !empty($row_rsspecies['year']) {

    $separator = ', ';

} ELSE {

    $separator = '';

}

// .. the echo line would be:

echo str_repeat("<dd>",$i++) . 'Subgen' .  " " . $row_rsspecies['subgenero'] . " " . $row_rsspecies['author'] . $separator . $row_rsspecies['year']; 

?>
Last edited by McGruff on Thu Aug 11, 2005 7:30 am, edited 1 time in total.
jarow
Forum Commoner
Posts: 83
Joined: Tue Jan 28, 2003 2:58 am

Post by jarow »

thanks McGruff...worked real well
Post Reply