如何使用PHP和MySQL实现标记?

时间:2015-12-24 11:20:48

标签: php html mysql css sql

我的数据库表中有两列。

ID     Tags
1      tag1,tag2
2      tag3,tag4,tag5,tag6

现在我想用ID打印标签。像:

ID: 1
#tag1 #tag2


ID: 2
#tag3 #tag4 #tag5 #tag6

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

我想如果我明白你需要什么,那么我认为这就是你所需要的

$conn = mysqli_connect(db_host, db_user, db_pass, db_name);
$results = mysqli_query($conn, "select * from tags");
if (mysqli_error($conn)) {
    echo mysqli_error($conn); 
} else {
    if (mysqli_num_rows($conn) > 0) {
        while($result = mysqi_fetch_array($results)) {
            $tags = explode(',', $result['Tags']);
            ?>
                <label>ID: <?php echo $result['ID']; ?></label><br />
                <label><?php echo implode(' #', $tags); ?>
            <?php
        }
    } else {
        ?>
            <label>There is no result</label>
        <?php   
    }
}