php - 通过复选框内爆数据

标签 php explode implode

我有这个界面,用户可以在其中添加花 interface

用户需要将一朵花添加到数据库中,他可以将它们在不同的场合分类(复选框)。现在为了破坏数据,我编写了这段代码:

 $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

现在用户也可以编辑花朵了。这是界面 enter image description here

问题!: 我不知道如何再次制作复选框并向他显示他选中了哪个复选框并且他可以更改数据。我需要知道如何显示复选框以及他选中的复选框也会被选中。

谢谢,如果有人能帮助我,我真的很感激。

编辑答案:这是复选框的界面。

最佳答案

你可以这样做:

$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;

Check Demo Here

编辑:由于您的评论,我正在进行此编辑,您需要执行以下操作:(未经测试)

<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>

关于php - 通过复选框内爆数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38911668/

相关文章:

php - 如何使用 PHP 生成 JSON 数据?

python - 如何在使用 pandas DataFrame.explode() 后创建一个新的 'index' 列?

php - 来自数组的链接

php - JPGraph + mysql数组不读取值

php - mysql查询和内爆

php - 关于如果在 while 数组中找到空值则添加零

php - 更新数据库中与 html 表单中的复选框关联的列表的最佳实践

php - 将 SQL 表 ID 转换为 PHP 中的另一个字段

php - 如何更新字段以将值推送到 Laravel 中的现有值

php - php中如何去掉大括号