开发者

How to display multiple cell entries individually from a MySQL database and using PHP

开发者 https://www.devze.com 2022-12-23 22:07 出处:网络
I have a column called post_tags where there is sometimes one tag entry and sometimes multiple tags stored. These are separated by * symbols. I want to display these out to the screen one by one. If t

I have a column called post_tags where there is sometimes one tag entry and sometimes multiple tags stored. These are separated by * symbols. I want to display these out to the screen one by one. If there were just one item inside the cell I would have used:

$query = mysql_query("SELECT post_tags FROM posts WHERE id=$id");

while ($result = mysql_fetch_assoc($query)) {
         $result['post_tags'];
}
开发者_如何学Go

But how can I display each entry individually when there are multiple ones in one cell (is this what the explode function is for)?


You should use a text-splitting function such as preg_split to split the field contents. Also, I've found on many occasions that it's faster than split or explode.

$tags_separated = preg_split('/\\*/', $result['post_tags']);


You could use explode, but you are better off constructing your database in a normalized way and using multiple rows for multiple tags.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号