为ExtJS树控件准备一个多维数组 [英] Preparing a multi-dimensional array for an ExtJS tree control

查看:159
本文介绍了为ExtJS树控件准备一个多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请不要重复一个这个问题..........
我是新来的php。
我正在开发一个树状网格在extjs.were我需要显示字段在tree format.so为前端我需要发送编码数据。
我想要2个函数来编码和解码字符串变量,多维数组,带有一组分隔符的变量。
例如,如果我有一个数组.........

  array(
数组(task=>rose,
duration=> 1.25,
user=> 15
),
数组=雏菊,
duration=> 0.75,
user=> 25,
),
数组(task=> orchid,
duration=> 1.15,
user=> 7
),
数组(task=>向日葵 b $ bduration=> 1.50,
user=> 70


);

我想将所有的数组字段或单个字段编码为.........

  array(
array(task=>rose,
duration => 1.25,
user=> 15
),
数组(task=>雏菊,
duration=&
user=> 25 $ sbaa,
),
数组(task=>兰花,
duration=> 1.15,
user=> 7 $!ass,
),
数组(task=>向日葵,
duration=> 1.50,
user=> 70 $!abc


);

所以像这样我只需要编码字符串,带分隔符的变量........ 。
以后所有的编码值要被解码,然后再被取回到后端.....为此我必须使用这个插件..........
encode.class .php ...........

 <?php 

/ * -------------------------
作者:Jonathan Pulice
日期:2005年7月26日
名称: JPEncodeClass v1
描述:使用模式的编码器和解码器。
------------------------- * /

class Protector
{

var $ Pattern =;
var $ PatternFlip =;
var $ ToEncode =;
var $ ToDecode =;
var $ Decoded =;
var $ Encoded =;
var $ Bug = false;
var $ DecodePattern =;

函数调试($ on = true)
{
$ this-> Bug = $ on;
}

函数Encode()
{


$ ar = explode(:,$ this-> Pattern) ;
$ enc = $ this-> ToEncode;

if($ this-> Bug)echo<! - BEGIN ENCODING - > \\\
;

foreach($ ar as $ num => $ ltr)
{
switch($ ltr)
{
caseE:
$ enc = base64_encode($ enc);
break;
caseD:
$ enc = base64_decode($ enc);
break;
caseR:
$ enc = strrev($ enc);
break;
caseI:
$ enc = $ this-> InvertCase($ enc);
break;
}
if($ this-> Bug)echo<! - {$ ltr}:{$ enc} - > \\\
;
}

if($ this-> Bug)echo<!--------------------> \\ \
\\\
;

@ $ this-> Encoded =($ enc == $ this-> Str)? < font color ='red'>否检测到编码/解码模式!< / font> :$ enc;

返回$ this->编码;

}

函数Decode()
{

$ pattern =($ this-> DecodePattern!=)? $ this-> DecodePattern:$ this-> Pattern;

//反转模式
$ this-> PatternFlip($ pattern);

// make into a array
$ ar = explode(:,$ this-> PatternFlip);

$ t =($ this-> Encoded ==)? $ this-> ToDecode:$ this-> Encoded;

if($ this-> Bug)echo<! - BEGIN DECODING - > \\\
;

foreach($ ar as $ num => $ ltr)
{
switch($ ltr)
{
caseE:
$ t = base64_encode($ t);
break;
caseD:
$ t = base64_decode($ t);
break;
caseR:
$ t = strrev($ t);
break;
caseI:
$ t = $ this-> InvertCase($ t);
break;
}
if($ this-> Bug)echo<! - {$ ltr}:{$ t} - > \\\
;
}

if($ this-> Bug)echo<!--------------------> \\ \
\\\
;

$ this-> Decoded =($ t == $ this-> Encoded)? < font color ='red'>否检测到编码/解码模式!< / font> :$ t;

返回$ this->解码;

}

函数MakePattern($ len = 10)
{
//可能的字母
// E - Base64编码
// R - Reverse String
// I - 反案例
$ poss = array('E','R','I');

//为$($ i = 0; $ i $ $ len; $ i ++)生成一个字符串

{
$ tmp [] = $ poss [rand(0,2)];
}

// echo $ str。 <峰; br> 中;
//修复无用模式部分RR II
$ str = implode(:,$ tmp);

//修复
$ str = str_replace('R:R:R:R:R:R','R:E:R:E:R:E' );
$ str = str_replace('R:R:R:R:R','R:E:R:E:R',$ str);
$ str = str_replace('R:R:R:R','R:E:R:E',$ str);
$ str = str_replace('R:R:R','R:E:R',$ str);
$ str = str_replace('R:R','R:E',$ str);

//修复
$ str = str_replace('I:I:I:I:I:I','I:E:I:E:I:E',$ str );
$ str = str_replace('I:I:I:I:I','I:E:I:E:I',$ str);
$ str = str_replace('I:I:I:I','I:E:I:E',$ str);
$ str = str_replace('I:I:I','I:E:I',$ str);
$ str = str_replace('I:I','I:E',$ str);

// string很好,设置为pattern
$ this-> Pattern = $ str;
return $ this-> Pattern; //如果我们需要它

}

函数PatternFlip($ pattern)
{
//反转模式
$ str = strrev($图案);

$ ar = explode(:,$ str);

foreach($ ar as $ num => $ ltr)
{
switch($ ltr)
{
caseE:
$ tmp [] =D;
break;
caseD:
$ tmp [] =E;
break;
caseR:
$ tmp [] =R;
break;
caseI:
$ tmp [] =I;
break;
}

}

$ rev = implode(:,$ tmp);

$ this-> PatternFlip = $ rev;

return $ this-> PatternFlip;
}

//这是我的自定义Case Invertor!
//如果你想在脚本中使用这个,请给我一个信息,谢谢
函数InvertCase($ str)
{
//初始转换
$ new = strtoupper($ str);

// spluit into arrays
$ s = str_split($ str);
$ n = str_split($ new);

//现在我们逐步通过每一封信,如果与以前一样,我们将($ i = 0; $ i< count($ s))交换出
$ i $)
{
if($ s [$ i] === $ n [$ i])// SWAP THE LETTER
{
// ge the letter
$ num = ord($ n [$ i]);

//查看ord是否在alpha范围内(65 - 90 | 97 - 122)
if(($ num> = 65 AND $ num <= 90)OR ($ num> = 97 AND $ num <= 122))
{
if($ num <97){$ num = $ num + 32; }
else {$ num = $ num - 32; }

$ newchr = chr($ num);

$ n [$ i] = $ newchr;
}
}
}

//将新的字符串加在一起
$ newstr = implode(,$ n);

return $ newstr;

}

}



$ p
$ b从这个插件我需要使用编码和解码功能为我的功能.........
如果有人可以帮助我这个.......这将对我非常有用.......

解决方案

我们来分解一下这个问题。



你有一个数组。数组中的每个元素都是一个散列。该哈希中的一个(或多个)值被使用图书馆可怕的可憎编码进行编码。但是图书馆无法处理数组。



我们必须自己处理数组。



code>< rant>

在我们开始之前,我只是想再次表达Protector代码设计的可怕程度。它是为PHP4编写的,并且是有效的意大利面条代码包装在一个类中。它错误地使用属性,几乎就像用户对于Java的实例变量的工作方式有某种错误的记忆,而以某种方式认为以相同的方式使用PHP是合适或合理的。如果代码的作者现在并不鄙视那些反转的字节,那么他的错误就是严重错误。

< / rant>



我将基于这个知识课程在这个副本,作为一个你提供的格式比原始格式更差。



首先,我们创建一个需要编码的内部数组键列表。

  $ keys_to_encode = array('user'); 

您的示例编码仅列出用户键可编码如果您需要对其他人进行编码,只需向该数组添加更多元素。



现在,我们来准备我们的保护者。它似乎要让您指定一个模式,或者使用 MakePattern 方法来创建一个模式。我们将手动指定一个,因为 MakePattern 可以实现无用的组合。

  $ stupid = new Protector(); 
$ stupid-> Pattern ='E:I:E:R:D:I:E'

这将base64编码,翻转案例,再次base64,反转它,un-base64,翻转的情况下,再重新base64。请注意,PHP的base64解码器正确忽略了不良的填充和意外的字符,这是base64-reverse-unbase64的唯一原因。所得到的字符串对于不知道base64看起来如何的人来说看起来像是乱七八糟的,而对于要知道base64看起来如何的人来说,这些字符串是非常基础的。



如果您需要以某种方式对值进行编码,只需更改模式即可。重要的是,您可以对模式进行硬编码或将其存储在某处与数据,因为没有它,您将很难做解码。 (我的意思是,它可以手工完成,但你不想这样做。)我希望你的老板会给你具体的说明,模式,因为你在课堂上没有使用这个失败的选项。



现在是时候处理我们的数据了。假设中的 $包含您的原始数组。

  $ out = array(); 
foreach($ in $ k => $ target){
foreach($ keys_to_encode as $ target_key){
$ stupid-> ToEncode = $ target [$ target_key];
$ target [$ target_key] = $ stupid-> Encode();
}
$ out [$ k] = $ target;
}

这个循环遍历哈希数组,然后在每个散列内,只有键我们要对编码进行编码。编码散列放置在新的数组中, $ out 。这是你传给你的树小部件的。



解码同样简单。您已经表示,您的目标是让用户在树窗口小部件中编辑某些数据,并且您似乎希望保护底层密钥。这告诉我,你可能只需要一次处理一个哈希值。因此,我将使用只有一个值 $无论编写此解码示例。

  $ stupidest = new Protector(); 
$ stupidest-> Pattern ='E:I:E:R:D:I:E' // SAME PATTERN!
$ stupidest-> ToDecode = $ whatever;
$ decoded = $ stupidest-> Decode();

再次,这是关键,您在这里使用相同的解码模式。 / p>

至于与小部件的实际交互,你自己就在那里。我只知道ExtJS才能知道它存在,并且GUI创建者真的很有趣玩。


please dont close this question as repeated one.......... I am new to php. I am developing one tree grid in extjs.were i need to display field in tree format.so for front end i need to send the encoded data. I want 2 functions for encoding and decoding a string variable,multidimensional array,variables with a set of delimiters. For example if i am having an array.........

array(
array( "task" => "rose",
"duration" => 1.25,
"user" => 15
),
array( "task" => "daisy",
"duration" => 0.75,
"user" => 25,
),
array( "task" => "orchid",
"duration" => 1.15,
"user" => 7
),
array( "task" => "sunflower",
"duration" => 1.50,
"user" => 70
)

);

i want to encode all the array fields or single field as..........

array(
array( "task" => "rose",
"duration" => 1.25,
"user" => 15
),
array( "task" => "daisy",
"duration" => 0.75,
"user" => 25$sbaa,
),
array( "task" => "orchid",
"duration" => 1.15,
"user" => 7$!ass,
),
array( "task" => "sunflower",
"duration" => 1.50,
"user" => 70$!abc
)

);

So like this only i need to encode string,variables with delimiters......... later all the encoded values to be decoded before its taken back to back end.....for this i have to use this plugin.......... encode.class.php...........

<?php

/*-------------------------
Author: Jonathan Pulice
Date: July 26th, 2005
Name: JPEncodeClass v1
Desc: Encoder and decoder using patterns.
-------------------------*/

class Protector
{

var $Pattern = "";
var $PatternFlip = "";
var $ToEncode = "";
var $ToDecode = "";
var $Decoded = "";
var $Encoded = "";
var $Bug = false;
var $DecodePattern = "";

function Debug($on = true)
{
$this->Bug = $on;
}

function Encode()
{


$ar = explode(":", $this->Pattern);
$enc = $this->ToEncode;

if ($this->Bug) echo "<!-- BEGIN ENCODING -->\n";

foreach ($ar as $num => $ltr)
{
switch ($ltr)
{
case "E":
$enc = base64_encode($enc);
break;
case "D":
$enc = base64_decode($enc);
break;
case "R":
$enc = strrev($enc);
break;
case "I":
$enc = $this->InvertCase($enc);
break;
}
if ($this->Bug) echo "<!-- {$ltr}: {$enc} -->\n";
}

if ($this->Bug) echo "<!-------------------->\n\n";

@$this->Encoded = ($enc == $this->Str) ? "<font color='red'>No Encoding/Decoding Pattern Detected!</font>" : $enc;

return $this->Encoded;

}

function Decode()
{

$pattern = ($this->DecodePattern != "") ? $this->DecodePattern : $this->Pattern;

//Reverse the pattern
$this->PatternFlip($pattern);

//make into an array
$ar = explode(":", $this->PatternFlip);

$t = ($this->Encoded == "") ? $this->ToDecode : $this->Encoded;

if ($this->Bug) echo "<!-- BEGIN DECODING -->\n";

foreach ($ar as $num => $ltr)
{
switch ($ltr)
{
case "E":
$t = base64_encode($t);
break;
case "D":
$t = base64_decode($t);
break;
case "R":
$t = strrev($t);
break;
case "I":
$t = $this->InvertCase($t);
break;
}
if ($this->Bug) echo "<!-- {$ltr}: {$t} -->\n";
}

if ($this->Bug) echo "<!-------------------->\n\n";

$this->Decoded = ($t == $this->Encoded) ? "<font color='red'>No Encoding/Decoding Pattern Detected!</font>" : $t;

return $this->Decoded;

}

function MakePattern($len = 10)
{
//possible letters
// E - Base64 Encode
// R - Reverse String
// I - Inverse Case
$poss = array('E','R', 'I');

//generate a string
for ( $i = 0 ; $i < $len ; $i++ )
{
$tmp[] = $poss[ rand(0,2) ];
}

//echo $str. "<br>";
//fix useless pattern section RR II
$str = implode(":", $tmp);

//fix
$str = str_replace( 'R:R:R:R:R:R' , 'R:E:R:E:R:E' , $str );
$str = str_replace( 'R:R:R:R:R' , 'R:E:R:E:R' , $str );
$str = str_replace( 'R:R:R:R' , 'R:E:R:E' , $str );
$str = str_replace( 'R:R:R' , 'R:E:R' , $str );
$str = str_replace( 'R:R' , 'R:E' , $str );

//fix
$str = str_replace( 'I:I:I:I:I:I' , 'I:E:I:E:I:E' , $str );
$str = str_replace( 'I:I:I:I:I' , 'I:E:I:E:I' , $str );
$str = str_replace( 'I:I:I:I' , 'I:E:I:E' , $str );
$str = str_replace( 'I:I:I' , 'I:E:I' , $str );
$str = str_replace( 'I:I' , 'I:E' , $str );

//string is good, set as pattern
$this->Pattern = $str;
return $this->Pattern; //if we need it

}

function PatternFlip($pattern)
{
//reverse the pattern
$str = strrev($pattern);

$ar = explode(":", $str);

foreach ($ar as $num => $ltr)
{
switch ($ltr)
{
case "E":
$tmp[] = "D";
break;
case "D":
$tmp[] = "E";
break;
case "R":
$tmp[] = "R";
break;
case "I":
$tmp[] = "I";
break;
}

}

$rev = implode(":", $tmp);

$this->PatternFlip = $rev;

return $this->PatternFlip;
}

// This is my custom Case Invertor!
// if you would like to use this in a script, please credit it to me, thank you
function InvertCase($str)
{
//Do initial conversion
$new = strtoupper( $str );

//spluit into arrays
$s = str_split( $str );
$n = str_split( $new );

//now we step through each letter, and if its the same as before, we swap it out
for ($i = 0; $i < count($s); $i++)
{
if ( $s[$i] === $n[$i] ) //SWAP THE LETTER
{
//ge the letter
$num = ord( $n[$i] );

//see if the ord is in the alpha ranges ( 65 - 90 | 97 - 122 )
if ( ( $num >= 65 AND $num <= 90 ) OR ( $num >= 97 AND $num <= 122 ) )
{
if ($num < 97 ) { $num = $num + 32; }
else { $num = $num - 32; }

$newchr = chr($num);

$n[$i] = $newchr;
}
}
}

//join the new string back together
$newstr = implode("", $n);

return $newstr;

}

}

?> ............ from this plugin i need to use encode and decode functions for my functions......... if anyone can help me on this.......it will be very much useful for me.......

解决方案

Okay, let's break down the problem.

You have an array. Each element in the array is a hash. One (or more) of the values in that hash has to be encoded using that horrible abomination of a library. But the library can't process arrays.

We'll have to process the array ourselves.

<rant>
Before we begin, I'd just like to again express how horribly that "Protector" code is designed. It's written for PHP4 and is effectively spaghetti code wrapped in a class. It mis-uses properties, almost as if the user had some sort of mis-remembering about how Java's instance variables work and somehow thought it would be appropriate or sane to use PHP in the same way. If the author of the code doesn't look back on that revolting chunk of bytes now with utter disdain, something is severely wrong with him.
</rant>

I'm going to base my knowledge of this class on this copy of it, as the one you've provided is formatted even worse than the original.

First, let's create a list of inner array keys that we need to encode.

$keys_to_encode = array( 'user' );

Your example encoding lists only the user key as encodable. If you need to encode others, just add more elements to that array.

Now, let's prepare our "Protector." It seems to want you to either specify a pattern, or use the MakePattern method to have it create one. We're going to manually-specify one because MakePattern can come up with effectively useless combinations.

$stupid = new Protector();
$stupid->Pattern = 'E:I:E:R:D:I:E';

This will base64-encode, flip the case, base64 again, reverse it, un-base64, flip the case, and then re-base64. Be aware that PHP's base64 decoder "correctly" ignores bad padding and unexpected characters, which is the only reason the base64-reverse-unbase64 thing will work. The resulting string will look like gibberish to people that don't know what base64 looks like, and un-base64 to gibberish for people that to know what base64 looks like.

If you need to encode the values in a certain way, you'd do so by just changing the pattern. It's important that you either hard-code the pattern or store it somewhere with the data, because without it, you'll have a hard time doing the decode. (I mean, it can be done by hand, but you don't want to have to do that.) I expect that your boss is going to give you specific instructions on the pattern, given that you don't have an option in using this utter failure at a class.

Now it's time to process our data. Let's pretend that $in contains your original array.

$out = array();
foreach($in as $k => $target) {
    foreach($keys_to_encode as $target_key) {
        $stupid->ToEncode = $target[ $target_key ];
        $target[ $target_key ] = $stupid->Encode();
    }
    $out[$k] = $target;
}

This loops through the array of hashes, then inside each hash, only the keys we want to encode are encoded. The encoded hash is placed in a new array, $out. This is what you'd pass to your tree widget.

Decoding is just as easy. You've stated that your goal is letting users edit certain data in the tree widget, and you seem to want to protect the underlying keys. This tells me that you probably only need to deal with one of the hashes at a time. Therefore, I'm going to write this decode sample using only one value, $whatever.

$stupidest = new Protector();
$stupidest->Pattern = 'E:I:E:R:D:I:E'; // SAME PATTERN!
$stupidest->ToDecode = $whatever;
$decoded = $stupidest->Decode();

Again, it's critical that you use the same decode pattern here.

As for the actual interaction with the tree widget, you're on your own there. I know only enough about ExtJS to know that it exists and that the GUI creators for it are really fun to play with.

这篇关于为ExtJS树控件准备一个多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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