Sigmond Gatt
Sigmond Gatt

Reputation: 73

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.

EDIT Answer: This is the interface of the checkboxes.

Upvotes: 2

Views: 2014

Answers (2)

Sigmond Gatt
Sigmond Gatt

Reputation: 73

This is the answer. Thanks to the Flash!

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>

click here for code

Upvotes: 1

Luthando Ntsekwa
Luthando Ntsekwa

Reputation: 4218

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>

Upvotes: 1

Related Questions