Sigmond Gatt Sigmond Gatt - 3 months ago 18
PHP Question

IMPLODE data by checkbox

i have this Interface where the user can add a flower interface

The user need to add a flower to the database and he can make them in different occasions a categories (the check boxes). Now to implode the data i made this code:

$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')";


In the database they save as follows :

id flower name occasions categories

1 Rose Birthday;Valentines Bouqet;flower arrangment


Now the user can edit a flower too.This is the interface
enter image description here

PROBLEM!:
i dont have an idea how can i make the checkboxes again and show him which one he checked and he can change the data. I need to know how can i show the checkboxes and which one he checked will be checked too .

Thank and I really appreaciate if someone can help me.

Answer

You can do something like:

$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

EDIT: I am making this edit because of your comments, you need to do something like:(not tested)

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