通过复选框内爆数据
IMPLODE data by checkbox
我有这个界面,用户可以在其中添加一朵花
用户需要将一朵花添加到数据库中,他可以将它们在不同的场合分类(复选框)。现在要爆破我制作此代码的数据:
$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
现在用户可以编辑花too.This是界面
问题!:
我不知道如何再次制作复选框并告诉他他选中了哪个,他可以更改数据。我需要知道如何显示复选框以及他选中的哪个也会被选中。
谢谢,如果有人能帮助我,我真的很感激。
编辑答案: 这是复选框的界面。
您可以这样做:
$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>
这就是答案。感谢闪电侠!
session_start();
$conn = ConnectToSql();
?>
<div class="table-responsive col-md-6" style="border:1px solid blue">
<div class="col-md-6">Categories</div>
<div class="col-md-6">Occasions</div>
<hr>
<div class="col-md-6">
<?php
/**flower occassions **/
$flowerCategoriesQry= "SELECT categories FROM tbl_flower where id ='$_SESSION[flower]'";
$flowerResults = mysqli_query($conn, $flowerCategoriesQry) ;
$categoriesRow = $flowerResults->fetch_assoc();
$resultsArray = explode(";", $categoriesRow['categories']);
/**All Occassions **/
$query544="SELECT name FROM tbl_categories";
$results544 = mysqli_query($conn, $query544);
while ($row = $results544->fetch_assoc()) { ?>
<label class="checkbox" for="checkboxes">
<?php
if(in_array($row['name'],$resultsArray ))
{
?>
<input type="checkbox" name="edit_categories[]" value="<?php echo $row['name'];?>" checked> <?php echo $row['name'];?>
<?php
}
else{
?>
<input type="checkbox" name="edit_categories[]" value="<?php echo $row['name'];?>" ><?php echo $row['name']; } ?>
</label>
<?php
}
?>
</div>
我有这个界面,用户可以在其中添加一朵花
用户需要将一朵花添加到数据库中,他可以将它们在不同的场合分类(复选框)。现在要爆破我制作此代码的数据:
$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
现在用户可以编辑花too.This是界面
问题!: 我不知道如何再次制作复选框并告诉他他选中了哪个,他可以更改数据。我需要知道如何显示复选框以及他选中的哪个也会被选中。
谢谢,如果有人能帮助我,我真的很感激。
编辑答案: 这是复选框的界面。
您可以这样做:
$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>
这就是答案。感谢闪电侠!
session_start();
$conn = ConnectToSql();
?>
<div class="table-responsive col-md-6" style="border:1px solid blue">
<div class="col-md-6">Categories</div>
<div class="col-md-6">Occasions</div>
<hr>
<div class="col-md-6">
<?php
/**flower occassions **/
$flowerCategoriesQry= "SELECT categories FROM tbl_flower where id ='$_SESSION[flower]'";
$flowerResults = mysqli_query($conn, $flowerCategoriesQry) ;
$categoriesRow = $flowerResults->fetch_assoc();
$resultsArray = explode(";", $categoriesRow['categories']);
/**All Occassions **/
$query544="SELECT name FROM tbl_categories";
$results544 = mysqli_query($conn, $query544);
while ($row = $results544->fetch_assoc()) { ?>
<label class="checkbox" for="checkboxes">
<?php
if(in_array($row['name'],$resultsArray ))
{
?>
<input type="checkbox" name="edit_categories[]" value="<?php echo $row['name'];?>" checked> <?php echo $row['name'];?>
<?php
}
else{
?>
<input type="checkbox" name="edit_categories[]" value="<?php echo $row['name'];?>" ><?php echo $row['name']; } ?>
</label>
<?php
}
?>
</div>