我正在创建一个网站,其中包含一些地方以及每个我想拥有标签的地方。我有一个带有表名复选框的数据库,在表格中我有一个名为checkbox_title的列。在我的后端,我有三个复选框,其中包含篮球,棒球和排球的值。如果我检查全部或部分框,我想将它们添加到数据库中,在checkbox_title列中。我使用implode()完成了这个。但是,当我将它们插入我的数据库时,用逗号分隔,这很好但是在我的网站的前端我想将标签显示为分隔包裹在span标签中,而不是使用span标签的组。我想知道是否有办法使用span标签而不是逗号分隔implode()值。希望有人能帮助我解决这个问题。
PS。我在前端添加了评论,以了解我希望如何工作。
后端代码:
<form method="post" action="">
<div class="form-group">
<?php
if(isset($_POST['add_tags'])) {
$tags = implode(', ' , $_POST['tags']);
$query = "INSERT INTO checkbox (checkbox_title) VALUES ('$tags') ";
$result = mysqli_query($connection, $query);
if(!$result) {
die("Error ".mysqli_error($connection));
}
}
?>
<label class="checkbox-inline"><input type="checkbox" name="tags[]" value="basketball">Basketball</label>
<label class="checkbox-inline"><input type="checkbox" name="tags[]" value="baseball">Baseball</label>
<label class="checkbox-inline"><input type="checkbox" name="tags[]" value="volleyball">Volleyball</label>
</div>
<div class="form-group">
<input type="submit" name="add_tags" class="btn btn-primary" value="Submit">
</div>
</form>
前端代码:
<div class="container">
<div class="row">
<div class="col-xs-12">
<?php
$select_all_query = "SELECT * FROM checkbox";
$select_all_query_con = mysqli_query($connection, $select_all_query);
while($tags = mysqli_fetch_assoc($select_all_query_con)) {
$product_tags = $tags['checkbox_title'];
echo "<span class='c_span'>$product_tags</span>";
}
?>
<!--
<span class="c_span">Basketball</span>
<span class="c_span">Baseball</span>
<span class="c_span">Volleyball</span>
-->
</div>
</div>
</div>
答案 0 :(得分:0)
您可以将正面代码更新为
<div class="container">
<div class="row">
<div class="col-xs-12">
<?php
$select_all_query = "SELECT * FROM checkbox";
$select_all_query_con = mysqli_query($connection, $select_all_query);
while($tags = mysqli_fetch_assoc($select_all_query_con)) {
$product_tags = $tags['checkbox_title'];
$tags = explode(',', $product_tags);
foreach ($tags as $item) {
echo "<span class='c_span'>". $item ."</span>";
}
}
?>
<!--
<span class="c_span">Basketball</span>
<span class="c_span">Baseball</span>
<span class="c_span">Volleyball</span>
-->
</div>
</div>
</div>