Guest Posted October 22, 2009 Share Posted October 22, 2009 Hi there, I am struggling with something today and I wanted to see if anybody could help me on that. Here is my code: <ul> <?php $actor_credits = get_credits_for_actor($sel_actor['id'], 1); while($credit = mysql_fetch_array($actor_credits)) { echo "<li><a href=\"edit_actor_credit.php?credit={$credit['id']}&actor={$sel_actor['id']}\"> {$credit["production_year"]}, {$credit["character_role"]}, {$credit["production_name"]}, {$credit["production_company"]}, {$credit["director"]}</a></li>"; } ?> </ul> The thing is, all the fields are not mandatory. So for an entry where there would not be any character role entered, I will have something like: " 2000, , Gladiator, ...". So my question is: how do I avoid having this "," when the field is not entered? Do I have to do a if statement for all the fields? Thanks. Krys Link to comment https://forums.phpfreaks.com/topic/178604-how-to-avoid-unwanted-puntuation/ Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 I would put the values in an array (if they exist) and then use join to combine them into a string. I.e. something like this: $values = array(); foreach (array('production_year', 'character_role', /* etc */) as $field) { if (!empty($credit[$field])) { $values[] = $credit[$field]; } } $foo = join(', ', $values); Link to comment https://forums.phpfreaks.com/topic/178604-how-to-avoid-unwanted-puntuation/#findComment-941973 Share on other sites More sharing options...
Guest Posted October 22, 2009 Share Posted October 22, 2009 That's perfect Daniel, it works perfectly! Thanks! Link to comment https://forums.phpfreaks.com/topic/178604-how-to-avoid-unwanted-puntuation/#findComment-942013 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.