如何正确使用json_encode?

时间:2014-01-31 20:32:07

标签: javascript php variables encode

我正在尝试显示一个家谱。我用来做它的工具叫做javascript basic primitives。您可以在此处找到有关它的更多信息:http://www.basicprimitives.com/index.php?option=com_content&view=article&id=1&Itemid=4&lang=en 显示树的每个叶子的函数:

/**
 * displayLeaf 
 * Displays the leaves of the family tree for current individual and all the relatives
 */

function displayLeaf($type, $individualid, $name, $dates, $gender, $bio, $avatar, $username, $relationships, $relationshipDates) {

    ?>
    var individualid = <?php echo json_encode($individualid); ?>,
        name = <?php echo json_encode($name); ?>,
        dates = <?php echo json_encode($dates); ?>,
        gender = <?php echo json_encode($gender); ?>,
        bio = <?php echo json_encode($bio); ?>,
        avatar = <?php echo json_encode($avatar); ?>,
        username = <?php echo json_encode($username); ?>,
        relationships = <?php echo json_encode($relationships); ?>,
        relationshipDates = <?php echo json_encode($relationshipDates); ?>,
        parentid = null,
        isVisible = true,
        itemType = primitives.orgdiagram.ItemType.Regular,
        placementType = primitives.common.AdviserPlacementType.Auto;
    <?php
    if (!empty($type)) {
        ?>
        for (var i = 0; i < items.length; i++) { 
            if (items[i].id == individualid) {
            parentid = -1;
            }
            else {
                <?php
                if ($type == 'Invisible aggregator') {
                    ?>
                    isVisible = false;

                    if (items[i].relationship == relationships) {


                        parentid = items[i].parent;
                        placementType = primitives.common.AdviserPlacementType.Left;
                        items[i].parent = individualid;
                        items[i].itemType = primitives.orgdiagram.ItemType.Adviser;

                        for (var j = 0; j < items.length; j++) {
                            if (items[j].id.indexOf("aggr") != -1) {
                                if (items[j].id.indexOf(relationships) != -1) {
                                    items[j].parent = individualid;
                                    items[j].itemType = primitives.orgdiagram.ItemType.SubAdviser;
                                }
                                if (items[j].id.indexOf(relationships) == -1) {
                                    if (items[j].parent != null) {
                                    }
                                    else {
                                        items[j].parent = individualid;
                                        items[j].itemType = primitives.orgdiagram.ItemType.SubAdviser;
                                        items[j].placementType = primitives.orgdiagram.AdviserPlacementType.Left;
                                    }

                                }
                            }

                        }   
                    }
                    else if (relationships.indexOf(items[i].id) != -1) {
                        parentid = items[i].id; 
                        itemType = primitives.orgdiagram.ItemType.Adviser;
                    }
                    <?php
                }

                if ($type == 'parent') {
                    ?>
                    if (items[i].relationship == relationships) { 
                        <?php
                        $type = 'Invisible aggregator';
                        $_individualid = $individualid.' aggr ';
                        $this->displayLeaf($type, $_individualid, $name, NULL, NULL, NULL, NULL, NULL, $relationships, NULL);
                        ?>
                        for (var j = 0; j < items.length; j++) {
                            if (items[j].id == <?php echo json_encode($_individualid); ?>) {
                                parentid = items[j].id;
                                isVisible = true;
                                itemType = primitives.orgdiagram.ItemType.Adviser;
                            }
                        }
                    }

                    <?php
                }
                if ($type == 'currentIndividual') {
                    ?>
                    if (items[i].relationship.indexOf(individualid) != -1 && items[i].isVisible == false) {
                        parentid = items[i].id;                             
                    }
                    <?php
                }
                if ($type == 'spouse') {
                    ?>
                    if (items[i].id == relationships) {
                        <?php
                        $type = 'Invisible aggregator';
                        $_individualid = $individualid.' aggr ';
                        $_relationships = json_decode($relationships).' '.json_decode($individualid);
                        $this->displayLeaf($type, $_individualid, $name, NULL, NULL, NULL, NULL, NULL, $_relationships, NULL);
                        ?>
                        for (var j = 0; j < items.length; j++) {
                            if (items[j].id == <?php echo json_encode($_individualid); ?>) {
                                parentid = items[j].id;
                                isVisible = true;
                                itemType = primitives.orgdiagram.ItemType.Adviser;                                  
                            }
                        }
                    }
                    <?php
                }
                if ($type == 'child') {
                    $relationshipArr = explode(' ',json_decode($relationships));
                    ?>
                    if (items[i].relationship == relationships && items[i].isVisible == false) {
                        parentid = items[i].id; 
                    }
                    if ("<?php echo $relationshipArr[0]; ?>".indexOf(items[i].id) != -1 || items[i].id.indexOf("<?php echo $relationshipArr[1]; ?>") != -1) {
                        if (items[i].isVisible == false) {
                            parentid = items[i].id; 
                        }
                    }
                    <?php
                }
                ?>
            }
        }

    items.push(new primitives.orgdiagram.ItemConfig({
                        id: individualid,
                        relationship: relationships, 
                        parent: parentid,
                        isVisible: isVisible,
                        title: name,
                        description: dates,
                        itemType: itemType,
                        adviserPlacementType: placementType,
                        templateName: gender,
                        image: avatar
                    }));
        <?php
    }
}

如果我需要向树中添加一个不可见的项目并给它自定义布局,那么该函数可以被具有父,子,配偶和函数本身信息的其他函数多次调用。这就是树的形成方式。我一直在尝试使用“”将php变量传递给javascript并且一切正常但是当我决定开始使用json_encode和json_decode时,我的树视图消失了。如果执行调用函数本身的statemens,则会发生这种情况。为什么?我究竟做错了什么?如何在上面的函数中正确使用json_encode?

0 个答案:

没有答案