Jump to content

How to avoid unwanted puntuation?


Guest

Recommended Posts

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

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.