基于PHP / MySQL生成x个html元素

For every comma inside a certain MYSQL row,

I want to create x html elements, where x = number of commas inside a row.

So for example, if my mysql row looks like this:

dance, singing, drawing, acting,

That is 4 commas.

I want to create a html box (using a div) for every comma, and inside that box, show the hobby..

So like on the main.php page, show :

[ dance ]

[ singing ]

[ drawing ]

[ acting ]

How does one go about creating a certain number of html elements based on a certain number of commas in a mysql table, assuming that I will add more hobbies to the table?

BTW, I am using php as the server side languange.

Any help would be appreciated, thank you.

This is untested, so consider it psuedo-code. You may need to escape the square brackets. The other issue you could encounter is that there could be an empty div output at the very end. In that case, just check that $elem != ''.

$elements = explode(',', $mysql_data_column);
foreach($elements as $elem)
{
   echo "<div style=\"block\">[$elem]</div>";
}