将php代码转换为javascript代码 [英] Convert a php code into javascript code

查看:115
本文介绍了将php代码转换为javascript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示表中的数据,每个条目都有一个父项,我的php代码如下所示:

I need to display data from a table and each entry has a parent, my php code looks like this:

function makeArbo($array, $currentParent = 0, $currLevel = 0, $prevLevel = -1, &$result = '')
        {
            if (is_array($array) && count($array) > 0)
            {
                foreach ($array as $item)
                {
                    if ($currentParent == $item['idParent'])
                    {                      
                        if ($currLevel > $prevLevel)
                            $result .= '<ul>';

                        if ($currLevel == $prevLevel)
                            $result .= '</li>';

                        $result .= '<li>'.$item['name'].'</li>';

                        if ($currLevel > $prevLevel)
                            $prevLevel = $currLevel;

                        $currLevel++;

                        makeArbo($array, $item['id'], $currLevel, $prevLevel, $result);

                        $currLevel--;
                    }  
                }

                if ($currLevel == $prevLevel)
                    $result .=  '</li></ul>';

                return $result;
            }
        }

        echo makeArbo($array);
        ?>
}

我的表格如下:

<?php
        $array = array(
            array(
                'id'       => 1,
                'name'      => 'Maths',
                'idParent' => 0
            ),
            array(
                'id'       => 2,
                'name'      => 'Topologie',
                'idParent' => 1
            ),
            array(
                'id'       => 3,
                'name'      => 'Algèbre',
                'idParent' => 1
            ),
            array(
                'id'       => 4,
                'name'      => 'Algèbre linéaire',
                'idParent' => 3
            ),
            array(
                'id'       => 5,
                'name'      => 'Arithmétique',
                'idParent' => 3
            ),
            array(
                'id'       => 6,
                'name'      => 'Thérorème de Bézout',
                'idParent' => 5
            ),
            array(
                'id'       => 7,
                'name'      => 'Informatique',
                'idParent' => 0
            ),
            array(
                'id'       => 8,
                'name'      => 'C-C++',
                'idParent' => 7
            ),
            array(
                'id'       => 9,
                'name'      => 'Les pointeurs',
                'idParent' => 8
            )
        );
         }

,结果非常完美:

 <ul>
        <li>Maths</li>
        <ul>
            <li>Topologie</li>
            <li>Algèbre</li>
            <ul>
                <li>Algèbre linéaire</li>
                <li>Arithmétique</li>
                <ul>
                    <li>Thérorème de Bézout</li>
                </ul>
            </ul>
        </ul>
        <li>Informatique</li>
        <ul>
            <li>C-C++</li>
            <ul>
                <li>Les pointeurs</li>
            </ul>
        </ul>
    </ul>

但我需要将此代码从php翻译为javascript;

but i need to translate this code from php to javascript ;

我试过这样的javascript代码:

I tried a javascript code like this :

function makeArbo(array,currentParent,currLevel, prevLevel, result)
    {   
        if (typeof(currentParent)=='undefined'){

            currentParent=0 ;
            currLevel= 0 ;
            prevLevel=-1 ;
            result='';

    }
        if (array.length >0)
        {
          for (i=0;i< array.length;i++)
            {
                if (currentParent == array[i]['parentid'])
                {        
                   if (currLevel > prevLevel)
                        result += '<ul>';

                    if (currLevel == prevLevel)   
                        result += '</li>';

                    result += '<li>'+ array[i]['name']+'</li>';

                    if (currLevel > prevLevel)
                        prevLevel = currLevel;

                    currLevel++;
                    result+=makeArbo (array,array[i]['id'], currLevel, prevLevel, result.valueOf());

                    currLevel--;
                }  
           }
                  if  (currLevel == prevLevel) 
                  result += '</li></ul>';



         return result ;
        }

    }
}

返回不好,我没有像我的第一个PHP代码一样的返回。我认为这是一个问题交叉引用(通过引用),因为在我的PHP代码中结果通过引用传递& $ result =''。

The return isn't good, I don't have the same return like my first php code. I think that it is a problem crossing reference (by ref), because in my php code result is passed by reference "&$result = ''" .

任何建议?

推荐答案

我认为你的代码在js中因为$ array而不正确。至于js,当一个数组被传输时,它只复制数组的地址,而不是整个状态。对于状态,我的意思是数组中的当前位置,这在您的代码中是必需的。所以每次函数makeArbo在js'部分运行时,数组总是以其位置的第一个元素开始,这不是你所期望的。

解决方案只是将数组移到函数外部,让它成为一个全局变量,一切都会好的。

---------------------------------刚开始在这里阅读----------- ------------------------

---------------------------------Just begin reading here-----------------------------------

1 Javascript不提供关联数组,您需要使用对象。参见

1 Javascript don't provide associative arrays, you need to use an object instead. See

function myVar(id, name, idParent){
          this.id = id;
          this.name = name;
          this.idParent = idParent;
}

2在Javascript中,itselt之前没有var的变量是全局变量。参见

2 In Javascript, a variable without a var before itselt is a global variable. See

array = new Array(...

3相应地更换您的其他代码。以下代码在我自己的服务器中作为html文件进行测试。

3 Replacing your other codes accordingly. The code below is tested in my own server as an html file.

4另外你应该处理返回值。使用一个对象来保存返回值,否则我们无法得到我们想要的东西。最后这一切一切正常,我可以去我的床...

4 Also, you should take care of the return value. Use an object to hold the return value or we can't get what we want. Finally everything works fine this time and I can go to my bed...

干杯!!

我的html文件:

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>

<p id="demo">My First Paragraph</p>

<script>

function myVar(id, name, idParent){
      this.id = id;
      this.name = name;
      this.idParent = idParent;
}

array = new Array(
    new myVar(1, "maths", 0),
    new myVar(2, "Topologie", 1),
    new myVar(3, "Algèbre", 1),
    new myVar(4, "Algèbre linéaire", 3),
    new myVar(5, "Arithmétique", 3),
    new myVar(6, "Thérorème de Bézout", 5),
    new myVar(7, "Informatique", 0),
    new myVar(8, "C-C++", 7),
    new myVar(9, "Les pointeurs", 8)
);

function run() {
    var result = {};//the object to hold the values' position
    result.value = "";//the real values;
    document.getElementById("demo").innerHTML = makeArbo(0, 0, -1, result).value;
}


function makeArbo(currentParent , currLevel, prevLevel, result)
{
    if (array.length >0)
    {
       for (var i = 0 ; i < array.length; i++)
       {
           if (currentParent == array[i].idParent)
           {        
               if (currLevel > prevLevel)
                   result.value += '<ul>';

               if (currLevel == prevLevel)   
                   result.value += '</li>';

               result.value += '<li>' + array[i].name + '</li>';

               if (currLevel > prevLevel)
                   prevLevel = currLevel;

               currLevel++;

               makeArbo(array[i].id, currLevel, prevLevel, result);

               currLevel--;
          }   
     }    
     if (currLevel == prevLevel) 
         result.value += '</li></ul>';

     return result;
}

}

window.onload = run;
</script>

</body>
</html>

这篇关于将php代码转换为javascript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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