For help, I have an infinite class tree menu table in the background, I call this table in the foreground, how to call out the series

Source: Internet
Author: User
This post was last edited by xiachao2008 on 2012-05-28 18:47:53

I'm going to implement it like this effect http://www.tmall.com/?spm=1.1000386.220593.1&TBG=146280.176778.1


I'm going to make it work like this.

What should I do, I did the following work

My background infinite classification tree table

CREATE  TABLE IF not EXISTS ' www.dianxin.com '. ' Dx_gd_type ' (  ' t_id ' INT (one) not NULL auto_increment COMMENT ' classification id\  n ',  ' t_name ' varchar (+) null COMMENT ' shopping name category \ n ',  ' t_family_id ' varchar (one) ' Not NULL ' 00000000 ' COMMENT ' Parent ID, top class default to 0\n ',  ' T_orders ' SMALLINT (5) null COMMENT ' shopping sorted ' \ n ',  ' t_remark ' varchar () NULL COMMENT ' remarks \ n ',  PRIMARY KEY (' t_id ')) ENGINE = MyISAM DEFAULT charset=utf8comment = ' Dim sum shopping type Table \ n ';


Note: The t_family_id is classified like this, (the number that is stored is the following law)
10 for clothes 20 for digital 30 for furniture two class 1001 for men's clothing 1002 for women's clothing three class 100201 for women's underwear 100202 for women ...

How do I write this calling code to achieve an effect like this?


Reply to discussion (solution)

Or am I not doing enough now,

What's the difference? I'm going to implement it like this effect http://www.tmall.com/?spm=1.1000386.220593.1&TBG=146280.176778.1


I'm going to make it work like this.

What should I do, I did the following work

My background infinite classification tree table

C # code


CREATE TABLE IF not EXISTS ' www.dianxin.com '. ' dx_gd_t ...

This post was last edited by xuzuning on 2012-05-28 19:18:23

From the database it is easy to get such an array of $ar = Array ('  10 ', ' represent clothes '), Array ('  20 ', ' for digital '), Array (  ' 30 ', ' for furniture '),  Array (' 1001 ', ' on behalf of men's clothing '),  Array (' 1002 ', ' for women's clothing '),  array (' 100201 ', ' on behalf of lingerie '),  array (' 100202 ', ' on behalf of women ... '),);// Decompose the hierarchy of classifications = Array (), foreach ($ar as $v) {  $p =& $r;  foreach (Str_split (Array_shift ($v), 2) as $k) {    if (! isset ($p [$k])) $p [$k] = array ();    $p =& $p [$k];  }  $p [' value '] = $v;} Print_r ($R);
Get an array like this, then it's too easy.
Array ([ten] = = Array ([value] = = Array ([0] = = Clothes                            ) = = Array ([value] = = Array (                [0] = = for men's clothing) [[in] + = Array                        ([value] = = Array ([0] = = Women's Clothing ) = = Array ([value] = =                                Array ([0] = = Women's underwear  ) [[]] = = Array ([value]                                = = Array ([0] = = women ...                )        )) [+] = Array ([value] = = Array (                [0] = = digital)) [+] = Array ([value] = array ([0] = = furniture)))

How do I get a shiver every time I hear an infinite assortment ...

This tnnd in the end is what fetish, is the landlord said that several levels of goods?

The heap code is.

............ How do I get a shiver every time I hear an infinite assortment ...

This tnnd in the end is what fetish, is the landlord said that several levels of goods?

The heap code is.

You will, you in the group to send a good classification plug-out, I do not have to bother.
How do I get a shiver every time I hear an infinite assortment ...

This tnnd in the end is what fetish, is the landlord said that several levels of goods?

The heap code is.


I'm the one that iterates. The array will not be written.
Also learned a trick, hehe, thank
PHP Code

It's easy to get such an array from the database
$ar = Array (
Array (' 10 ', ' on behalf of clothes '),
Array (' 20 ', ' on behalf of the digital '),
Array (' 30 ', ' stands for furniture '),
Array (' 1001 ', ' on behalf of men's clothing '),
Array (' 1002 ', ' on behalf of women's clothing '),
Array (' 100201 ', ' on behalf of women's underwear '),
Array ...

It wouldn't be better to just make a static page. Ha ha

I also want to ah directly into a static page is not better. Ha ha




This code is a little bit "simple" to see if you can give me a hint "= Array (); foreach ($ar as $v) {  $p =& $r;  ?????? What is the work of this?  foreach (Str_split (Array_shift ($v), 2) as $k) {    if (! isset ($p [$k])) $p [$k] = array ();    $p =& $p [$k];  ???? This work has to do what Yo, why Add &  }  $p [' value '] = $v;} Print_r ($r); Alas, in short, the above code, I understand half, did not read half. Can you give me some pointers?




PHP Code

It's easy to get such an array from the database
$ar = Array (
Array (' 10 ', ' on behalf of clothes '),
Array (' 20 ', ' on behalf of the digital '),
Array (' 30 ', ' stands for furniture '),
Array (' 1001 ', ' on behalf of men's clothing '),
Array (' 1002 ', ' on behalf of women's clothing '),
Array (' 100201 ', ' on behalf of women's underwear '),
Array ...

$p =& $r;
$p =& $p [$k];
Quote Yes!

Set the original string to 100201, after cutting for the $r an array
The $p in turn refers to
$r
$r [' 10 ']
$r [' 10 '] [' 02 ']
$r [' 10 '] [' 02 '] [' 01 ']

PHP Code

It's easy to get such an array from the database
$ar = Array (
Array (' 10 ', ' on behalf of clothes '),
Array (' 20 ', ' on behalf of the digital '),
Array (' 30 ', ' stands for furniture '),
Array (' 1001 ', ' on behalf of men's clothing '),
Array (' 1002 ', ' on behalf of women's clothing '),
Array (' 100201 ', ' on behalf of women's underwear '),
Array ... Too simple to pull! Teachable

In fact, my problem is that when I encounter such a code & I can not understand it.
In particular, the array generation & such symbols, it is difficult to understand

Previously in the online search for some of this document, feel still understand, but when in practice, a encounter, and will not.
$p =& $r;
$p =& $p [$k];
Quote Yes!

Set the original string to 100201, after cutting for the $r an array
The $p in turn refers to
$r
$r [' 10 ']
$r [' 10 '] [' 02 ']
$r [' 10 '] [' 02 '] [' 01 ']

If you don't resent using the Eval function, you can write this

$r = Array (), foreach ($ar as $v) {  $k = "r[". Join ("'] ['", Str_split (Array_shift ($v), 2)). [' value '];  Eval ("\$ $k = \ $v;");}

It seems that I have not reached this level, still can not understand.



If you don't resent using the Eval function, you can write this
PHP Code
$r = Array ();
foreach ($ar as $v) {
$k = "r[". Join ("'] ['", Str_split (Array_shift ($v), 2)). [' Value '];
Eval ("\$ $k = \ $v;");
}

In fact I only so anxious to understand this, because, this array I do not know how the foreground to traverse out.

So I think, to understand how the background is generated, in order to know how to call the foreground,

I used three loops at the front desk, the first loop-level classification, the second loop-level classification, the third-cycle three-level classification,

If there are n classifications, then I'm going to write N loops yo.

I feel that my thinking is wrong, but I can not find other ideas.



PHP Code

It's easy to get such an array from the database
$ar = Array (
Array (' 10 ', ' on behalf of clothes '),
Array (' 20 ', ' on behalf of the digital '),
Array (' 30 ', ' stands for furniture '),
Array (' 1001 ', ' on behalf of men's clothing '),
Array (' 1002 ', ' on behalf of women's clothing '),
Array (' 100201 ', ' on behalf of women's underwear '),
Array ...

The most important thing is that
I wrote three times that the result of the loop traversal was wrong
PHP Code

It's easy to get such an array from the database
$ar = Array (
Array (' 10 ', ' on behalf of clothes '),
Array (' 20 ', ' on behalf of the digital '),
Array (' 30 ', ' stands for furniture '),
Array (' 1001 ', ' on behalf of men's clothing '),
Array (' 1002 ', ' on behalf of women's clothing '),
Array (' 100201 ', ' on behalf of women's underwear '),
Array ...

Recursion, yes!
is not a tree-shaped array generated?

function func ($ar) {
foreach ($ar as $k = = $v) {
if ($k = = ' value ') {
Output this level of data
}else {
Func ($v);
}
}
}

Complex problems with recursion Bai, which has a circular universal solution!

Following your instructions, I wrote down below.

But now the question is, I don't know how to judge the end of this subcategory. It's over, I have to add, or say,

There is also a problem, now traversed out of the array only the first 3 elements of a large classification of one, two-dimensional classification of a three-level classification of one.
Can you help me see where the problem is?

I also want to ask a question, I get the source array in the database, I use what function output string, they can use this array?

Public function func ($arr, $count, $beforcount, $spList) {foreach ($arr as $key + = $val) {if ($key = = ' value ') {if ($count = = 1) {$html. = ';} else if ($count ==2) {$html. = '
 
 

'. $val ["T_name"]. '

';} else if ($count ==3) {$html. = '. $val ["T_name"]. ';} $spList = Array_merge ($spList, Array ($html)); Array_shift ($arr);} else {$beforcount = $count; $count + +; $this->func ($val, $count, $beforcount, $spList);}} if (count ($arr) ==0) {$s _html = '; foreach ($spList as $key = = $val) {$s _html = $s _html. $val;} echo "
";p Rint_r ($spList); exit ();}}


Recursion, yes!
is not a tree-shaped array generated?

function func ($ar) {
foreach ($ar as $k = = $v) {
if ($k = = ' value ') {
Output this level of data
}else {
Func ($v);
}
}
}

I don't know how to write it.

Complex problems with recursion Bai, which has a circular universal solution!

Using Var_export (array) to get the PHP code, you can use the

Oh, thanks.
My traversal method above is wrong again.

Using Var_export (array) to get the PHP code, you can use the

In fact, the design of the database is more important, oh, I did not get this aspect, but feel the database design method will affect the algorithm

I wonder if an experienced person can point out whether the structure of the data table shown by the landlord is a typical infinite rating table structure.

Oh, thanks.
My traversal method above is wrong again.
You give the results with Var_export (array)
Let me show you.

      Array (' value ' = = Array (' t_id ' = ' 4092 ', ' t_name ' = ' ladies ', ' t_family_id ' = ' 10 '),        ' T_orders ' = null, ' T_remark ' = null, ' ' "'" "= = Array (' value ' = = Array (        ' t_id ' = ' 4099 ', ' t_name ' and ' ' tops ', ' t_family_id ' = ' 1001 ', ' t_orders ' and ' = NULL ', ' T_remark ' = NULL, ' "the '" "=" = "Array (' value ' = = Array (' t_id ' =& Gt ' 4123 ', ' t_name ' and ' t-shirts/sweatshirt ', ' t_family_id ' = ' 100101 ', ' t_orders ' and ' = NULL '  T_remark ' = NULL,), ' "" + "=" Array (' value ' = = Array (' t_id ')          = ' 4124 ', ' t_name ' + ' knit sweater/sweater ', ' t_family_id ' = ' 100102 ', ' t_orders ' and ' = NULL ',          ' T_remark ' = NULL,), ' "" \ "=" Array "(' value ' = = Array ( ' t_id ' = ' 4125', ' t_name ' + ' shirts/chiffon shirts ', ' t_family_id ' = ' 100103 ', ' t_orders ' and ' = NULL, ' t_re Mark ' = NULL,), ' "" "=" Array (' value ' = = Array (' t_id ' =&G T          ' 4126 ', ' t_name ' = ' small suit/small coat ', ' t_family_id ' = ' 100104 ', ' t_orders ' and ' = NULL ', ' T_remark ' = NULL,), ' + ' = Array (' value ' = = Array (' T_i          d ' = ' 4127 ', ' t_name ' + ' windbreaker/coat ', ' t_family_id ' = ' 100105 ', ' t_orders ' = NULL,          ' T_remark ' = NULL,), ' + ' = Array (' value ' = = Array ( ' t_id ' = ' 4128 ', ' t_name ' + ' vest/camisole ', ' t_family_id ' = ' 100106 ', ' t_orders ' =&gt ;           NULL, ' t_remark ' = null,), ' ' + ' = Array (' value ' = = Array ( ' t_id ' =&Gt ' 4129 ', ' t_name ' = ' vest ', ' t_family_id ' and ' 100107 ', ' t_orders ' = NULL, ' T_r Emark ' = NULL,), ' ' ' = ' = Array (' value ' = = Array (' t_id ' =& Gt ' 4130 ', ' t_name ' = ' cotton-padded ', ' t_family_id ' and ' 100108 ', ' t_orders ' and ' = NULL '  T_remark ' = NULL,), ' the ' "+ ' = Array (' value ' = = Array (' t_id ')          = ' 4131 ', ' t_name ' = ' down ', ' t_family_id ' = ' 100109 ', ' t_orders ' and ' = NULL ', ' T_remark ' = NULL,),), ten = = Array (' value ' = = Array (' T_          id ' = ' 4132 ', ' t_name ' + ' leather/Fur ', ' t_family_id ' = ' 100110 ', ' t_orders ' = NULL,        ' T_remark ' = NULL,),), ' The ', ' "" = = Array (' value ' = = Array ( ' t_id ' = ' 41xx ', ' t_name ' + ' under ', ' t_family_id ' = ' 1002 ', ' t_orders ' = NULL, ' t_remark ' = NULL,), ' t_id ' = = Array (' value ' = = ' = ' 4133 ', ' t        _name ' + ' pants ', ' t_family_id ' = ' 100201 ', ' t_orders ' = null, ' T_remark ' = null,          ), ' the ' "--" + = Array (' value ' = = Array (' t_id ' = ' 4134 '), ' T_name ' + ' jeans ', ' t_family_id ' = ' 100202 ', ' t_orders ' = NULL, ' T_remark ' and ' N '          ULL,),), ' t_id ' = Array (' value ' = = ' array ', ' + ' = ' = ' 4135 ', ' T_name ' + ' tights/stockings/leggings ', ' t_family_id ' = ' 100203 ', ' t_orders ' and ' = NULL, ' T_rema RK ' = NULL,),), ' t_id ' = = Array (' value ' = = Array (' = ' = ' = ' 41 =&G ', ' t_name 'T ' Skirts ', ' t_family_id ' = ' 1003 ', ' t_orders ' = null, ' T_remark ' = null, ' =&gt '       ; Array (' value ' = = Array (' t_id ' = ' 4136 ', ' t_name ' = ' dress ', ' T_fami       ly_id ' = ' 100301 ', ' t_orders ' = null, ' T_remark ' = null, '), ' "" Array (' value ' = = Array (' t_id ' = ' 4137 ', ' t_name ' = ' skirt ', ' t _family_id ' = ' 100302 ', ' t_orders ' = null, ' T_remark ' = null, '),), 2 0 = = Array (' value ' = = Array (' t_id ' = ' 4093 ', ' t_name ' = ' mens ', ' t_family_id ' =&gt ; ' A ', ' t_orders ' = null, ' T_remark ' = null, ' ' "'" "=" Array "(' value ' = arr Ay (' t_id ' = ' 4102 ', ' t_name ' and ' tops ', ' t_family_id ' = ' 2001 ', ' t_orders ' = ' NU '        LL,' T_remark ' = NULL, ' "the '" "=" = "Array (' value ' = = Array (' t_id ' = ' 4 138 ', ' t_name ' = ' t-shirts ', ' t_family_id ' and ' 200101 ', ' t_orders ' and ' NULL, ' T_rem Ark ' = NULL, '), ' ' The ' "" + = Array (' value ' = = Array (' t_id ' =&gt ; ' 4139 ', ' t_name ' and ' shirts ', ' t_family_id ' and ' 200102 ', ' t_orders ' and ' NULL, ' T_r Emark ' = NULL,), ' the ' $ ' = Array (' value ' = = Array (' t_id ' =& Gt ' 4140 ', ' t_name ' + ' coats ', ' t_family_id ' and ' 200103 ', ' t_orders ' = NULL, ' T_r Emark ' = NULL,), ' "" "=" = Array (' value ' = = Array (' t_id ' =& Gt ' 4141 ', ' t_name ' + ' polo shirts ', ' t_family_id ' + ' 200104 ', ' t_orders ' and ' = NULL ' T_remark ' =>          NULL,),), ' + ' = Array (' value ' = = Array (' t_id ' = ' 4142 '), ' T_name ' + ' knit sweater/sweater ', ' t_family_id ' = ' 200105 ', ' t_orders ' and ' = NULL, ' T_remark ' = NULL, '), ' + ' = Array (' value ' = = Array (' t_id ' = ') ' 4143 ', ' t_name ' + ' sweatshirt ', ' t_family_id ' and ' 200106 ', ' t_orders ' = NULL, ' t_re Mark ' = NULL, '), ' ' ' + ' = Array (' value ' = = Array (' t_id ' =&G T ' 4144 ', ' t_name ' = ' down ', ' t_family_id ' and ' 200107 ', ' t_orders ' and NULL, ' T_  Remark ' = NULL,),), ' a. ' = ' + ' array (' value ' = = Array (' t_id ' = ' 4103 ', ' t_name ' = ' down ', ' t_family_id ' and ' 2002 ', ' t_orders ' = NULL, ' T_remark ' =& Gt  NULL,),    ' t_id ' = Array (' value ' = ' = ' = ' 4145 ', ' t_name ' + ' pants      Son ', ' t_family_id ' = ' 200201 ', ' t_orders ' = null, ' T_remark ' = null, ), ' t_id ' = Array (' value ' = ' = ' = ' ' 4104 ', ' t_name ' = ' accessories ')       , ' t_family_id ' = ' 2003 ', ' t_orders ' = null, ' T_remark ' = null, ' ' " Array (' value ' = = Array (' t_id ' = ' 4146 ', ' t_name ' = ' men's shoes ', ' t_ family_id ' = ' 200301 ', ' t_orders ' = null, ' T_remark ' = null, '), ' =&G '       T Array (' value ' = = Array (' t_id ' = ' 4147 ', ' t_name ' = ' sneakers ', ' T_fami       ly_id ' = ' 200302 ', ' t_orders ' = null, ' T_remark ' = null, '), ' "   Array (     ' Value ' = = Array (' t_id ' = ' 4148 ', ' t_name ' = ' bag ticket holder ', ' t_family_id ' =& Gt ' 200303 ', ' t_orders ' = null, ' T_remark ' = null, '), '), '),?>

Using Var_export (array) to get the PHP code, you can use the

Why is there such a feeling? In fact, the design of the database is more important, oh, I did not get this aspect, but feel the database design method will affect the algorithm

I wonder if an experienced person can point out whether the structure of the data table shown by the landlord is a typical infinite rating table structure.

So write, format yourself control

function func ($ar) {  if (! Is_array ($ar)) return;  foreach ($ar as $key = + $item) {    echo '
 
    
    
; foreach ($item as $k = + $t) if ($k = = ' value ') { echo '
$key
"; foreach ($t as $v) echo "
$v
"; } else func ($t); Echo '
; }}

Alas, still did not understand

But you've got a tip that says 4 loops to walk him out,
There is time to learn in the future.

Thank

So write, format yourself control
PHP Code
function func ($ar) {
if (! Is_array ($ar)) return;
foreach ($ar as $key = = $item) {
Echo '

';
foreach ($item as $k = + $t)
if ($k = = ' value ') {
echo
$ke ...

All of the 123-level category menus are read from the database?? Or put it in a collection for code, thank you:

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.