Page 1 of 1

Joining with comma seperation

Posted: Mon Apr 14, 2008 8:58 pm
by wasir
I am trying to display list of items only they are available. If the variables are blank, I get
, , ,
How can I get the value only if it exists?

Code: Select all

 
if ($row['type'] == service) {
   echo 'service request';
} else {
   echo $row['type']; 
}
 
// incident
if ($row['break'] == yes) {
   $break = 'Break & Enter';
} else {
   $break = '';
}
 
if ($row['medical'] == yes) {
   $medical = 'Medical Emergency';
} else {
   $medical = '';
}               
 
if ($row['assault'] == yes) {
   $assault = 'Assault';
} else {
   $assault = '';
}           
                    
if ($row['claim'] == yes) {
   $claim = 'Insurance Claim';
} else {
   $claim = '';
}
                    
$incident = array($break, $medical, $assault, $claim);
$incident_comma = join(", ", $incident);
echo $incident_comma;
 

Re: Joining with comma seperation

Posted: Mon Apr 14, 2008 9:04 pm
by John Cartwright

Code: Select all

if ($row['type'] == 'service') {
   echo 'service request';
}
 
$incident = array(); 
// incident
if ($row['break'] == 'yes') {
   $incident[] = 'Break & Enter';
} 
 
if ($row['medical'] == 'yes') {
   $incident[] = 'Medical Emergency';
}
 
if ($row['assault'] == 'yes') {
   $incident[] = 'Assault';
}         
                   
if ($row['claim'] == 'yes') {
   $incident[] = 'Insurance Claim';
}
                  
$incident_comma = join(", ", $incident);
echo $incident_comma;

Almost had it ;). Instead of adding all of the elements to the array, only add the ones that you want to use (as shown above). You also need to quote your strings (which I've taken the liberty to do for you).

Enjoy.

Re: Joining with comma seperation

Posted: Mon Apr 14, 2008 9:09 pm
by wasir
Thanks HEAPS.
:D