Link to home
Start Free TrialLog in
Avatar of Joseph Longo
Joseph Longo

asked on

Concatenate When Condition Is Met

Hello experts,

Here is my code:

<input type="checkbox" name="seat_rented[]" value="<?php echo $row['seat_serial'];?>">
$n = count($seat);
foreach($seat as $x) {
    $pdf-> Write(0, $x . ", ");
} 

Open in new window

I only want to concatenate the comma when $n is >1. If $n equals 1, I want to concatenate a period. I can't think of the if or while statement to handle this...
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or classical way

for($i=0;$i<count($seat);$i++) 
{ 
  if ($i = 0) {
     $pdf-> Write(0, $seat[$i]);
  } else {
     $pdf-> Write(0, $seat[$i]  . ", ");
  }
} 

Open in new window


not sure what those 3 parameters are, but you can fix line 4 & 6 as you wish
Avatar of Joseph Longo
Joseph Longo

ASKER

I was unable to get the for loop working correctly, for whatever reason. However, the implode function worked correctly. Thank you for your assistance.
@HainKurt

The implode function worked correctly, with the exception of modifying it like this:

$pdf-> Write(0, implode(", ", $seat));

I am using FPDF to generate PDF documents.
The implode function worked correctly, with the exception of modifying it like this:

yes, implode joins all items with delimiter provided... no need to loop or check first or last item to put delimiter or not... implode does all for you... because I could not get what pdf write function is, I left the syntax to you to fix :)