jeudi 17 mars 2016

show checkbox checked when edit multi level category product php mysql

Here is my database design:

tbl_categories
id | name | parentid

tbl_product
id | name

tbl_product_cat
id | product_id | cat_id

I am trying to edit categories, i get the checkbox value checked but other categories doesn't showing checkbox. Please try to help here below i add my code and screen-shoot also:

$qry="SELECT * FROM tbl_categories";
             $result=mysql_query($qry);

             $arrayCategories = array();

             while($row = mysql_fetch_assoc($result)){ 
                $arrayCategories[$row['id']] = array("parentid" => $row['parentid'], "name" => $row['name']);   
             }
            //createTree($arrayCategories, 0);

            function createTree($product_id, $array, $currentParent, $currLevel = 0, $prevLevel = -1, $product_id) {                
            foreach ($array as $categoryId => $category) {

            if ($currentParent == $category['parentid']) {                       

                if ($currLevel > $prevLevel) echo " <ul> "; 

                if ($currLevel == $prevLevel) echo " </li> ";

                echo '<li id="'.$categoryId.'"><span>'.$category['name'].'</span>';

                $prosql   = "SELECT * FROM tbl_product_cat WHERE product_id=".$product_id."";
                $proquery = mysql_query($prosql);
                $catTotRecord = mysql_num_rows($proquery);


                    while ($proRes = mysql_fetch_object($proquery)) {                       
                        if($proRes->cat_id == $categoryId){
                          //echo "t";                             
                          echo '<input type="checkbox" value="'.$categoryId.'" checked="checked" name="category[]"/>';
                        }
                    }                                   

                //echo '<input type="checkbox" value="'.$categoryId.'" name="category[]"/>';                                    
                if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }

                $currLevel++; 

                createTree ($product_id, $array, $categoryId, $currLevel, $prevLevel);

                $currLevel--;               
                }   

            }

            if ($currLevel == $prevLevel) echo " </li>  </ul> ";

            }   
            ?>
            <div id="content" class="general-style1">
            <?php
             if(mysql_num_rows($result)!=0)
             {
            ?>
            <ul>                
            <?php
             $product_id = $id;
             createTree($product_id, $arrayCategories, 0); 
            ?>
            </li>
            </ul>
            <?php
            }
            ?>

while loop is here:

$prosql   = "SELECT * FROM tbl_product_cat WHERE product_id=".$product_id."";
                $proquery = mysql_query($prosql);
                $catTotRecord = mysql_num_rows($proquery);


                    while ($proRes = mysql_fetch_object($proquery)) {                       
                        if($proRes->cat_id == $categoryId){
                          //echo "t";                             
                          echo '<input type="checkbox" value="'.$categoryId.'" checked="checked" name="category[]"/>';
                        }
                    }                                   

                //echo '<input type="checkbox" value="'.$categoryId.'" name="category[]"/>'; 

Here is the screen shoot: enter image description here




Aucun commentaire:

Enregistrer un commentaire