用户需要向数据库中添加一朵花,并且他可以在不同的场合使用它们(复选框)。现在要破坏我制作此代码的数据:
$occasions = implode( ';' , $_POST['reg_occassions'] );
$categories= implode( ';' , $_POST['reg_categories'] );
$query =
"INSERT INTO tbl_flower (flower_type, website_description,florist_description,name,image,enabled,florist_choice,categories,occasions)
VALUES ('$flowertype', '$websitedescription', '$floristdescription','$name', '$upfile', '$enabled','$floristchoice', '$categories','$occasions')";
在数据库中,它们保存如下:
id flower name occasions categories
1 Rose Birthday;Valentines Bouqet;flower arrangment
问题!: 我不知道如何再次制作复选框并告诉他他检查了哪一个,他可以更改数据。我需要知道如何显示复选框以及他检查的那个也将被检查。
感谢,如果有人可以帮助我,我真的很感激。
答案 0 :(得分:1)
您可以执行以下操作:
$results = "Bouqet,flower arrangment"; //results from your database
$resultsArray = explode(",", $results); //split the comma
$checkValue = array("Bouqet", "flower arrangment", "other"); //your checkbox values
$html = "";
foreach($checkValue as $val)
{
if(in_array($val,$resultsArray ))
$html .= '<input type="checkbox" name="flower" value="'.$val.'" checked>'.$val.'<br>';
else
$html .= '<input type="checkbox" name="flower" value="'.$val.'">'.$val.'<br>';
}
echo $html;
编辑:由于您的评论,我正在进行此编辑,您需要执行以下操作:(未经过测试)
<div class="table-responsive col-md-4">
<table>
<thead>Occasions</thead>
/**flower occassions **/
$flowerCategoriesQry "SELECT categories FROM tbl_flower where id = 1"; //you need to change the where clause to variable
$flowerResults = mysqli_query($conn, $flowerCategoriesQry)
$categoriesRow = $flowerResults->fetch_assoc();
$resultsArray = explode(",", $categoriesRow['categories']);
/**All Occassions **/
$query544="SELECT name FROM tbl_occasions";
$results544 = mysqli_query($conn, $query544);
while ($row = $results544->fetch_assoc()) { ?>
<div class="col-md-12">
<label class="checkbox" for="checkboxes">
<?php
if(in_array($row['name'],$resultsArray ))
{
?>
<input type="checkbox" name="flower" value="<?php echo $row['name'];?>" checked> <?php echo $row['name'];?> >
<?php }
else{
?>
<input type="checkbox" name="flower" value="<?php echo $row['name'];?>" ><?php echo $row['name'];?> >
<?php echo} $row['name']?> </label>
</div> <?php }?>
</table>