gpt4 book ai didi

php - 如何在php mysql中显示依赖于下拉子类别的父类别

转载 作者:行者123 更新时间:2023-11-29 04:34:27 25 4
gpt4 key购买 nike

我正在尝试显示用户从下拉列表中选择的子类别并显示他的父类别,但是我的代码显示显示直到二级父类别我在 php 中是新的,请帮助我,谢谢

显示所有类别

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

我得到 parentid 和 id 并将其发送到更新页面,

更新类别.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>

通过 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);

}
?>

已满 full code

最佳答案

实际上问题出在您的 Update-category.php 代码中,您在其中仅选择了所选类别的父类别。您必须编写递归函数来获取以上所有父类别。

如果您提供了 loadChildCategories() 的定义,那么很容易回答您。但您仍然可以查看以下 URL,它会对您有所帮助。

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

谢谢

关于php - 如何在php mysql中显示依赖于下拉子类别的父类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47361563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com