我想做一个类别和子类别选择 - 所以当我按下一个类别时,它会在它旁边显示所有子类别。
我已经使它与html和js一起工作但现在我正在我的网站中进行dinamically
这就是我现在正在使用的
var categories_0 = [
<?php while ( fanapom_has_categories() ) { ?>
["<?php echo fanapom_category_id() ; ?>","<?php echo fanapom_category_name() ; ?>"],
<?php } ?>
];
<?php while ( fanapom_has_categories() ) { ?>
var categories_<?php echo fanapom_category_id() ; ?> = [<?php while ( fanapom_has_subcategories() ) { ?>["<?php echo fanapom_category_id() ; ?>","<?php echo fanapom_category_name() ; ?>"],<?php } ?>];
<?php } ?>
但我两次使用此代码 - 但我发现它只能运行一次。
<?php while ( fanapom_has_categories() ) { ?>
如果我一个人使用
var categories_0 = [
<?php while ( fanapom_has_categories() ) { ?>
["<?php echo fanapom_category_id() ; ?>","<?php echo fanapom_category_name() ; ?>"],
<?php } ?>
];
或仅此一点
<?php while ( fanapom_has_categories() ) { ?>
var categories_<?php echo fanapom_category_id() ; ?> = [<?php while ( fanapom_has_subcategories() ) { ?>["<?php echo fanapom_category_id() ; ?>","<?php echo fanapom_category_name() ; ?>"],<?php } ?>];
<?php } ?>
我得到正确的输出
但是当我把它们混合在一起时,我只得到第一个的输出,
如何让它能够为我提供正确的输出?我想我不应该两次使用这段代码
<?php while ( fanapom_has_categories() ) { ?>
但我不确定如何将两者混合在一起 - 请帮助。
M O R E I N F O
这就是我希望输出看起来像
var categories_0 = [
["1", "maincategoryname"],
["2", "maincategoryname"],
["3", "maincategoryname"],
["4", "maincategoryname"],
["5", "maincategoryname"],
["6", "maincategoryname"],
["7", "maincategoryname"],
["8", "maincategoryname"]
];
var categories_1 = [
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"]
];
var categories_2 = [
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"]
];
var categories_3 = [
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"]
];
var categories_4 = [
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"]
];
var categories_5 = [
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"]
];
var categories_6 = [
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"]
];
var categories_7 = [
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"]
];
var categories_8 = [
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"],
["subcategoryid", "subcategoryname"]
];
1 2 3 4 5 6 7 8个数字是主要的类别ID
答案 0 :(得分:0)
以下是如何在您的示例中打印多维javascript数组,假设数据源是PHP中类似的多维数组:
$categories = array(
"1" => array("one", array("1" => "a", "2" => "b", "3" => "c"))
, "2" => array("two", array("4" => "d", "5" => "e", "6" => "f"))
, "3" => array("three", array("7" => "g", "8" => "h", "9" => "i"))
);
$categories_js = 'var categories_0 = [';
$category_subs_js = '';
$delimiter1 = '';
$delimiter2 = '';
foreach ( $categories as $category_id => $category_info ) {
$categories_js .= $delimiter1 . '["' . $category_id . '", "' . $category_info[0] . '"]';
$delimiter1 = ', ';
$category_subs_js .= 'var categories_' . $category_id . ' = [';
foreach ( $category_info[1] as $subcat_id => $subcategory ) {
$category_subs_js .= $delimiter2 . '["' . $subcat_id . '", "' . $subcategory . '"]';
$delimiter2 = ', ';
}
$category_subs_js .= "];\n";
$delimiter2 = '';
}
$categories_js .= "];\n";
echo $categories_js;
echo $category_subs_js;
答案 1 :(得分:0)
您的fanapom_has_
函数可能会耗尽迭代器,这会阻止您第二次获取列表。 (原因#422835为什么可变的全球状态是邪恶的,顺便说一句。如果你有选择,我建议你减少对它的依赖。仅仅因为WordPress这样做,并不意味着它&#39;一个好主意。)
为了解决这个问题,您需要以某种方式重置迭代器,或者同时为主类别和每个子类别构建数组(因此您只需要通过列表一次)。 / p>
坦率地说,你可以通过手工构建JSON来放弃放屁。 :P PHP有一个内置函数json_encode
来做到这一点。您所要做的就是构建数组。你可以一次性完成。
也许像
<?php
$categories = [ [] ];
while (fanapom_has_categories()) {
$id = fanapom_category_id();
$categories[0][] = [ "$id", fanapom_category_name() ];
$categories[$id] = [];
while (fanapom_has_subcategories()) {
# btw, this looks odd to me, using the same function to get info on the
# category and subcategory, but using different functions to iterate through
# the respective lists.
$categories[$id][] = [ fanapom_category_id(), fanapom_category_name() ];
}
}
foreach ($categories as $id => $subcategories) {
echo "var categories_$id = ", json_encode($subcategories), ";\n";
}
?>
(注意:[ item1, item2 ]
是PHP 5.4 spiff。如果您仍然使用旧的和已破坏的PHP版本,请改用array(item1, item2)
。)