Mir
Mir

Reputation: 1

Count values separated by commas and don't put comma in the last value

So I am selecting values from database and they are like

this

and I am displaying the values on the site like

this

but it is not counting how much values are selected and it is inserting one comma in the last value expecting one more value to come.

This is the php code:

<?php
while ($row2 = mysqli_fetch_assoc($result_t)): ?>
<?php echo $row2['tipo'] . ", ";?>
<?php endwhile;?>

Upvotes: 0

Views: 70

Answers (3)

Aravind
Aravind

Reputation: 1

Implode the values (you will need to check how implode works Link here) with a comma (,)

Upvotes: 0

libregeek
libregeek

Reputation: 1274

You can get all the values from the DB in an array and then call implode() to join the individual values using comma.

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269753

Use group_concat() in MySQL:

select group_concat(tipo) as tipos
from t;

Upvotes: 2

Related Questions