Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid adding a comma at the last row?

Tags:

php

mysql

I use the following code to add a comma , at the end of every filename. After this using explode i put the rows on an array.

The images array if I echo it, it shows a.jpg,b.jpg,c.jpg, (please mind the last comma at the end of c.jpg)

How can I avoid adding a comma to the last found row?

<?php       
 while($image = mysql_fetch_array($images)) { 
 $images_path .= $image['FILE_NAME'].',';
}

$images_array = explode(",", $images_path);
?>
like image 819
EnexoOnoma Avatar asked Dec 12 '25 03:12

EnexoOnoma


2 Answers

Make use of rtrim() by providing a comma as the second paramter as character mask

$images_path = rtrim($images_path,","); //<---- The added code
$images_array = explode(",", $images_path);
like image 165
Shankar Narayana Damodaran Avatar answered Dec 13 '25 16:12

Shankar Narayana Damodaran


Why add it to a string and then convert it to an array, if you can add it straight away?

$images_array = array();
while($image = mysql_fetch_array($images)) { 
    $images_array[] = $image['FILE_NAME'];
}
like image 34
h2ooooooo Avatar answered Dec 13 '25 15:12

h2ooooooo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!