我的代码有问题,因为我想同时获取父类别和子类别,但它不起作用 [英] I have a problem in my code because I want fetch both parent category and child category simultaneously but it did not work

查看:67
本文介绍了我的代码有问题,因为我想同时获取父类别和子类别,但它不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是产品上传部分,用户可以在其中选择类别:

This is the product upload section where the user can select the category:

<p class="admin-estate-add-section-input">
   Parent category
 </p>
 
 <option value=""<?=(($parent == '')?' selected':'')?>>
 <?php while($p = mysqli_fetch_assoc($parentQuery)): ?>
 <option value="<?=$p['id'];?>"<?=(($parent == $p['id'])?' selected':'')?>><?=$p['category'];?>
 <?php endwhile; ?>
 
 <br>
 <br>
 <p class="admin-estate-add-section-input">
    Child category
 </p>
 
 <br>
 <br>





这是调用child_categories.php文件的jQuery函数





This is the jQuery function that calls the child_categories.php file

function get_child_options(selected){
 if(typeof selected === 'undefined'){
 var selected = '';
 }
 var parentID = jQuery('#parent').val();
 jQuery.ajax({
 url: '/tartalomkezelo/admin/parsers/child_categories.php',
 type: 'POST',
 data: {parentID : parentID, selected: selected},
 success: function(data){
 jQuery('#child').html(data);
 },
 error: function(){alert("Something went wrong! Can't select category.")},
 });

 }

 jQuery('select[name="parent"]').change(get_child_options);









这是child_categories.php文件:





This is the child_categories.php file:

<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/tartalomkezelo/core/init.php';
$parentID = (int)$_POST['parentID'];
$selected = sanitize($_POST['selected']);
$childQuery = $db->query("SELECT * FROM categories WHERE parent = '$parentID' ORDER BY category");
ob_start(); ?>

 <?php while($child = mysqli_fetch_assoc($childQuery)): ?>
 <option value="<?=$child['id'];?>"<?=(($selected == $child['id'])?' selected':'');?>><?=$child['category'];?>
 <?php endwhile; ?>
<?php echo ob_get_clean(); ?>





然后是产品上传文件的脚本:





And then the script for the product upload file:

jQuery('document').ready(function(){
 get_child_options('&lt;?=$category;?>');
 });





我尝试了什么:



我试图从数据库中获取数据进行编辑,但子类别在父类别



What I have tried:

I have tried to fetch data from database for editing but child category data did not fetched at the sametime with parent category

推荐答案

parent = = '')?' selected':'')? > >
<? php while(
parent == '')?' selected':'')?>> <?php while(


p = mysqli_fetch_assoc(
p = mysqli_fetch_assoc(


parentQuery)):?>
< 选项 = <?=
parentQuery)): ?> <option value="<?=


这篇关于我的代码有问题,因为我想同时获取父类别和子类别,但它不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆