We're going to make an unlimited assortment of goods.
First, the database fields are:
ID----------Commodity primary Key ID
FID----------Product Parent ID
Name----------Product Name
the final output of the array format is
Copy CodeThe code is as follows:
Array
0=>array (
' ID ' =>1,
' FID ' =>0,
' Name ' = ' French '
' Child ' =>array (
' ID ' =>12,
' FID ' =>1,
' Name ' = ' Perfume '
' Child ' =>array (
0=>array (
' ID ' =>34,
' FID ' =>12,
' Name ' = ' Women's perfume '
)
)
),
1=>array (
' ID ' =>13,
' FID ' =>1,
' Name ' = ' Notebook '
' Child ' =>null
)
)
),
1=>array (), //format Ibid. I don't have to repeat, it doesn't make any sense.
2=>array ()
)
PHP Code:
Database I use MySQL PDO but the whole idea is the same.
$conn =mysql_connect (' localhost ', ' root ', ' 123 ');
if (Mysql_errno ()) {
printf (' Connection failed '. Mysql_error ());
}
mysql_select_db (' Edeng ');
Mysql_set_charset (' UTF8 ');
/*
* Recursive function
* @param ID to query all subclasses of fid= $id the default value for $id is set to 0 because I have the FID of the topmost category in the database at 0
*/
function Get_array ($id =0) {
$sql = "Select Id,fid,cname from E_cat where fid= $id";
$result =mysql_query ($sql);
$arr =array ();
if ($result && mysql_affected_rows ()) {
while ($rows =mysql_fetch_assoc ($result)) {
$rows [' Child ']=get_array ($rows [' id ']);
$arr [] = $rows;
}
return $arr;
}
Echo '';
$result = Get_array ();
Print_r ($result);
The function first queries out all classes with FID 0
Callback by looping through while to find the FID as the subclass of the ID of the current class
http://www.bkjia.com/PHPjc/327545.html www.bkjia.com true http://www.bkjia.com/PHPjc/327545.html techarticle we're going to make an unlimited assortment of goods first database fields: ID----------Commodity primary key ID FID----------commodity parent ID Name----------product name the last output array format is complex ...