【问题标题】:Having some diffculty parsing tree data in ul li format in php在php中以ul li格式解析树数据有一些困难
【发布时间】:2017-12-18 11:42:52
【问题描述】:

我正在尝试创建试算平衡我以某种方式在我的数组中实现了树视图,现在我想打印我的代码但不知道如何制作树,因为我不知道将来我能得到多少孩子我不太擅长递归下面是我的树准备数组,我还附上了我可以通过 foreach 循环制作但卡在子节点中的结果我可以通过 foreach 制作子节点,但问题是它不是最好的方法请按照我的数组帮助我打印我的树结构数据。任何帮助,将不胜感激。

这里我的树数组看起来像:Tree Array

我的结果看起来像这样,这只是一个数组的根节点,我使用 jstree 来显示层次结构。

<?
        $total_opening_debit = 0;
        $total_opening_credit = 0;
        $total_debit = 0;
        $total_credit = 0; 
        foreach($this->trees as $key => $value) 
        {
            foreach($value as $tree)
            {
                if($tree["id"] != "")
                {
                ?>
                    <li>
                        <span class="first"><? echo $tree["name"]; ?></span>
                        <span class="other">
                        <?
                            if ($tree["opening_debit"] > $tree["opening_credit"])
                            {
                                echo format_currency($tree["opening_debit"] - $tree["opening_credit"], 2) . " Dr.";
                            }
                            elseif ($tree["opening_credit"] > $tree["opening_debit"])
                            {
                                echo format_currency($tree["opening_credit"] - $tree["opening_debit"], 2) . " Cr.";
                            }
                            else
                            {
                                echo "0";
                            }

                            $total_opening_debit += $tree["opening_debit"];
                            $total_opening_credit += $tree["opening_credit"];
                        ?>
                        </span>
                        <span class="other">
                        <?
                            if($tree["period_debit"]<>"" && $tree["period_debit"]<>"0")
                            {
                                echo format_currency($tree["period_debit"], 2);
                                $total_debit += $tree["period_debit"];
                            }
                            else
                            {
                                echo 0;
                            }
                        ?>
                        </span>
                        <span class="other">
                        <? 
                            if($tree["period_credit"]<>"" && $tree["period_credit"]<>"0")
                            {
                                echo format_currency($tree["period_credit"], 2);
                                $total_credit += $tree["period_credit"];
                            }
                            else
                            {
                                echo 0;
                            } 
                        ?>
                        </span>
                        <span class="other">
                        <? 
                            if ($tree["opening_debit"] + $tree["period_debit"] > $tree["opening_credit"] + $tree["period_credit"])
                            {    
                                $closing =  format_currency($tree["opening_debit"] + $tree["period_debit"] - $tree["opening_credit"] - $tree["period_credit"], 2) . " Dr.";
                            }
                            elseif ($tree["opening_credit"] + $tree["period_credit"] > $tree["opening_debit"] + $tree["period_debit"])
                            {             
                                $closing = format_currency($tree["opening_credit"] + $tree["period_credit"] - $tree["opening_debit"] - $tree["period_debit"], 2) . " Cr.";
                            }
                            else
                            {
                                $closing = "0";
                            }
                            echo $closing;

                            //calculate all opeing and closing debit & credit.
                            if ($tree["opening_debit"] + $tree["period_debit"] > $tree["opening_credit"] + $tree["period_credit"])
                            {     
                                $total_closing_debit += ($tree["opening_debit"] + $tree["period_debit"] - $tree["opening_credit"] - $tree["period_credit"]);
                            }

                            if ($tree["opening_credit"] + $tree["period_credit"] > $tree["opening_debit"] + $tree["period_debit"])
                            {
                                $total_closing_credit += ($tree["opening_credit"] + $tree["period_credit"] - $tree["opening_debit"] - $tree["period_debit"]);
                            }
                        ?>
                        </span>
                        <?
                        if(count($tree["children"]) > 0)
                        {
                            //make_children_tree();
                        }
                        ?>
                    </li>
                <?
                }
            }        
        }


    ?>

【问题讨论】:

    标签: php data-structures tree


    【解决方案1】:

    我在这里解决了我自己选择什么方法我将子数组提取到其他数组中,并在该数组上使用我的递归来做魔术。

    <? $array = $trial_tree["children"]; ?>
    
    
    $trial_balance_tree = $this->trees;
    
    $total_opening_debit = 0;
    $total_opening_credit = 0;
    $total_debit = 0;
    $total_credit = 0;
    $total_closing_debit = 0;
    $total_closing_credit = 0;
    $total_opening = 0;
    $total_closing = 0;
    
    function makeListItems($a) {
        global $total_opening_debit,$total_opening_credit,$total_credit,$total_debit,$total_closing_debit,$total_closing_credit,$total_opening,$total_closing;
        $out = '';
        foreach($a as $val) {
        //print_r($key); echo is_array($val);exit;
            if($val["id"] > 0)
            {
                $out .= '<li>';
                $out .= "<span class='first'>" . $val["name"] . "</span>";
                $out .= "<span class='other'>"; 
                            if ($val["opening_debit"] > $val["opening_credit"])
                            {
                                $out .= format_currency($val["opening_debit"] - $val["opening_credit"], 2) . " Dr.";
                            }
                            elseif ($val["opening_credit"] > $val["opening_debit"])
                            {
                                $out .= format_currency($val["opening_credit"] - $val["opening_debit"], 2) . " Cr.";
                            }
                            else
                            {
                                $out .= "0";
                            }
    
                            $total_opening_debit += $val["opening_debit"];
                            $total_opening_credit += $val["opening_credit"]; 
                 $out .= "</span>";
                 $out .= "<span class='other'>";
                            if($val["period_debit"]<>"" && $val["period_debit"]<>"0")
                            {
                                $out .= format_currency($val["period_debit"], 2);
                                $total_debit += $val["period_debit"];
                            }
                            else
                            {
                                $out .= 0;
                            }
                 $out .= "</span>";
                 $out .= "<span class='other'>";
                            if($val["period_credit"]<>"" && $val["period_credit"]<>"0")
                            {
                                $out .= format_currency($val["period_credit"], 2);
                                $total_credit += $val["period_credit"];
                            }
                            else
                            {
                                $out .= 0;
                            }
                 $out .= "</span>";
                 $out .= "<span class='other'>";
                            if ($val["opening_debit"] + $val["period_debit"] > $val["opening_credit"] + $val["period_credit"])
                            {    
                                $closing =  format_currency($val["opening_debit"] + $val["period_debit"] - $val["opening_credit"] - $val["period_credit"], 2) . " Dr.";
                            }
                            elseif ($val["opening_credit"] + $val["period_credit"] > $val["opening_debit"] + $val["period_debit"])
                            {             
                                $closing = format_currency($val["opening_credit"] + $val["period_credit"] - $val["opening_debit"] - $val["period_debit"], 2) . " Cr.";
                            }
                            else
                            {
                                $closing = "0";
                            }
                            $out .= $closing;
    
                            //calculate all opeing and closing debit & credit.
                            if ($val["opening_debit"] + $val["period_debit"] > $val["opening_credit"] + $val["period_credit"])
                            {     
                                $total_closing_debit += ($val["opening_debit"] + $val["period_debit"] - $val["opening_credit"] - $val["period_credit"]);
                            }
    
                            if ($val["opening_credit"] + $val["period_credit"] > $val["opening_debit"] + $val["period_debit"])
                            {
                                $total_closing_credit += ($val["opening_credit"] + $val["period_credit"] - $val["opening_debit"] - $val["period_debit"]);
                            }
                 $out .= "</span>";
    
                if(array_key_exists('children', $val)) {
                    $out .= makeList($val['children']);
                }
                $out .= '</li>';
            }
        }
    
        return $out;
    }
    
    function makeList($a) {
        $out = '<ul>';
        $out .= makeListItems($a);
        $out .= '</ul>';
    
        return $out;
    }
    ?>
    <table style="width:100%;background-color: #A5C6DB;">
        <tr>
            <th class="table_first" style="width:42.5%;">Ledger Name</th>
            <th class="table_other" style="width:14.7%;">Opening</th>
            <th class="table_other" style="width:14.7%;">Debit</th>
            <th class="table_other" style="width:14.7%;">Credit</th>
            <th class="table_other" style="width:15%;">Closing</th>
        </tr>
    </table>
    <?
    echo "<div id='trial_balance_tree'>";
    echo makeList($trial_balance_tree);
    echo "</div>";
    

    现在我的视图看起来像这样,这正是我想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-26
      • 2018-10-29
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      • 2013-12-27
      • 2022-12-20
      • 1970-01-01
      相关资源
      最近更新 更多