How do I echo a comma on all but the last value? Here\'s my code:
while ($servdescarrayrow = mysql_fetch_array($servdescarray)) {
ECHO $servdescarrayrow
$array = array();
while ($servdescarrayrow = mysql_fetch_array($servdescarray))
$array[] = $servdescarrayrow['serv_desc'];
ECHO implode(',', $array);
Here's one possible way. Not very graceful, but does the job:
$Ix = 0;
while ($servdescarrayrow = mysql_fetch_array($servdescarray)) {
if ($Ix > 0)
echo ",";
$Ix++;
echo $servdescarrayrow['serv_desc'];
}
Why not:
if ($servdescarryrow = mysql_fetch_array($servdescarray)) {
echo $servdescarryrow;
}
while ($servdescarrayrow = mysql_fetch_array($servdescarray)) {
ECHO ",".$servdescarrayrow['serv_desc'];
}
you can remove the last comma after generating the string like so:
$str = '';
while ($servdescarrayrow = mysql_fetch_array($servdescarray)) {
$str .= $servdescarrayrow['serv_desc'].",";
}
$str = substr($str, 0, strlen($str) - 1);
echo $str;
I am wondering why there was a dozen answers to your first question and noone to answer the edited part.
Nope, it shouldn't work. Because $servdescarrayrow variable represents only one row and you and thus the whole code makes no sense.
Although you can involve the number of returned records into process and get desired result, I doubt you really need that.
Not to mention that with the way you echo, you will always have the trailing comma anyway.