Asad Javed
Asad Javed

Reputation: 29

how to display parent categories depended in drop down chlid categories in php mysql

I'm trying to display user select sub category from drop down and display his parent categories , but my code showing display till second level parent categories im new in php help me please thank you

Display All categories

    <?php 

$result = mysqli_query($mysqli, "SELECT category.*, cat.name as parentName FROM category left join category as cat on category.parentId = cat.id  ORDER BY id ASC"); // using mysqli_query instead

    while($res = mysqli_fetch_array($result)) {         
        echo "<tr>";
         echo "<td>".$res['id']."</td>";
        echo "<td>".$res['name']."</td>";
        echo "<td>".$res['description']."</td>";

        echo "<td>".$res['parentId']."</td>"; 
        echo "<td>".$res['parentName']."</td>"; 

        echo "<td><a href=\"update.php?id=$res[id]&parentId=$res[parentId]\">Edit</a> | <a href=\"action/delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";        
    }
    ?>

enter image description here

i get parentid and id and send it into update page,

Update-category.php

        <form action="action/update-category.php" method="post" class="form-horizontal">
    <fieldset>
    <?php 
    include_once './action/connection.php'; 
    ?>
    <?php
    $id = $_GET['id']; // 
    $parentId = $_GET['parentId'];
    $sql  = "SELECT * FROM category WHERE id = $id";

    $result = mysqli_query($mysqli, $sql);
    $mainCategories = array();
    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            $mainCategories[] = $row;
        }
    } else {
        echo "0 results";
    }
    // echo "<b>Thank you! Record UPDATED Successfully!";
      ?>
    <!-- Form Name -->
    <legend>Edit Product</legend>
    <div class="col-md-12">
        <div class="form-group" id="category_main">
          <label class="col-md-6 control-label" for="product_categorie">CATEGORY</label>
          <div class="col-md-6">
          <select for="category_main" onchange="loadChildCategories(this);" id="" name="mainCategory_1" class="form-control category_main">
            <option> Please select parent category</option>
            <?php
        foreach ($mainCategories AS $eachCategory) {
        echo "<option value='".$eachCategory['parentId']."'>".$eachCategory['name']."</option>";
                }

            ?>
             </select>
          </div>
        </div>
    </div>

Updatecategoryby Id

    <?php


include_once 'connection.php';
if(!empty($_POST["parentId"])) {
$id = $_POST['parentId'];
$sql  = "SELECT * FROM category WHERE id='".$id."' Or parentId = '0'";
$result = mysqli_query($mysqli, $sql);
$childCategories = array();
$response = array();
if (mysqli_num_rows($result) > 0) {
    $response['hasChildCategories'] = true;
    $htmlChildCategories = '<div class="form-group" id="category_'.$id.'">
      <label class="col-md-6 control-label" for="product_categorie">CATEGORY</label>
      <div class="col-md-6">
      <select for="category_'.$id.'" onchange="loadChildCategories(this)" name="mainCategory_'.$id.'" class="form-control category_main">
        <option value="'.$id.'"> Select parent category</option>';

    // output data of each rowa
    while($row = mysqli_fetch_assoc($result)) {
        $childCategories[] = $row;
        $htmlChildCategories .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
    }
    $htmlChildCategories .= '</select></div></div>';
    $response['html'] = $htmlChildCategories;
}
else
{
    $response['hasChildCategories'] = false;
    $response['html'] = '';
}

echo json_encode($response);

}
?>

fullfull code

Upvotes: 1

Views: 809

Answers (1)

Saurabh
Saurabh

Reputation: 131

Actually problem is with your Update-category.php code where you select only parent category of selected category. You have to write recursive function to fetch all above parent categories.

if you provided definition of loadChildCategories() then its easy to answer you. but still you can check following URL, It will helpful for you.

How can I recursively get the IDs of all the parent elements in a multidimensional array?

Thanks

Upvotes: 4

Related Questions