views:

99

answers:

6

Hey

I know this question has with out any doubt been asked a whole lot of times. I though can seem to find a solution. So forgive me if it's way to simple.

The question is how do access the end of a while loop.

E.g.

    while($countByMonth = mysql_fetch_array($countByMonthSet)) { 
            $c = $countByMonth["COUNT(id)"];
            echo $c . "," ; 
        }

How do I manage separate each value of the while loop by a comma but of course I don't want the comma at the end of the value.

In advance thank you very much for your help :)

+5  A: 

The simple1 solution:

$isFirst = true;
while($countByMonth = mysql_fetch_array($countByMonthSet)) { 
    $c = $countByMonth["COUNT(id)"];
    if ($isFirst) {
        $isFirst = false;
    } else {
        echo = ', ';
    }
    echo $c; 
}

Alternatively, you could implode() the values. Or - perhaps easier to read/understand/maintain - concatenate it all into a string and remove the last "," (SO eats my whitespace; the string is comma-whitespace):

$list = '';
while($countByMonth = mysql_fetch_array($countByMonthSet)) { 
    $c = $countByMonth["COUNT(id)"];
    $list .= $c . ', '; 
}
echo substring($list, 0, -2); // Remove last ', '

(Several other answers propose the use of an accumulated array and then use implode(). From a performance perspective this method will be superior to string concatenation.)

1 See comments.

jensgram
How you are calling the first solution "simple" is beyond me.
quantumSoup
@quantumSoup :) I guess you're right. It seemed to be very simple in my head ... and very verbose when I wrote it :-S
jensgram
@quantumSoup Perhaps if I define "simple" as *with only little modifications to what you already had, but perhaps not what you would do if you had nothing to start from* :)
jensgram
+3  A: 

Alternatively you can do:

$arr = array();
while($countByMonth = mysql_fetch_array($countByMonthSet)) { 
   $arr[] = $countByMonth["COUNT(id)"];
}

echo implode(', ',$arr);
codaddict
`$arr = $countByMonth["COUNT(id)"]` should be `$arr[] = ...`
Ryan Kinal
@Ryan: Thanks for pointing.
codaddict
How did this get down-voted?
jensgram
@jens It was wrong
quantumSoup
@quantumSoup I see.
jensgram
+5  A: 

You can:

1) Build a string, and remove the last character:

$c = '';
while ($countByMonth = mysql_fetch_array($countByMonthSet)) { 
    $c .= $countByMonth["COUNT(id)"] . ',';
}

$c = substr($c, 0, -1);
echo $c;

2) Build an array and use implode()

$c = array();
while ($countByMonth = mysql_fetch_array($countByMonthSet)) { 
    $c[] = $countByMonth["COUNT(id)"];
}

echo implode(',', $c);

Tip: You can use aliases in your query, like: SELECT COUNT(id) as count FROM .... Then you can access it as $countByMonth['count'], which looks cleaner IMO.

NullUserException
Thanks alot I went for the implode method which works great. Thanks for the tip about aliases.
nickifrandsen
@NulluserException: Update your tip: $countByMonth['count'] to $countByMounth['COUNT(id)'], either way great answer.
Chris
+2  A: 

Or afterwards just trim it off with rtrim($c, ',')

Alex
A: 

int count;// while(i) {

count=i; }

Ravi
A: 

While I think the implode solution is probably best, in situations where you can't use implode, think of the basic algorithm differently. Rather than "how can I add a comma behind every element but the last?" ask yourself "how can I add a comma before every element but the first?"

$str = '';
$count = count( $array );
if( $count ) {
  $i = 0;
  $str = $array[$i];
  $i++;
  while( i < $count ) {
    $str .= ','.$array[$i];
    $i++;
  }
}

If you "shift" the first element, then you can use a foreach loop:

$str = '';
if( count( $array ) ) {
  $str = array_shift( $array );
  foreach( $array as $element ) {
    $str .= ', '.$element;
  }
}
Marshmellow1328