A _php tutorial on implementing infinite Classification formatted arrays with PHP recursion

Source: Internet
Author: User
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 ...

  • 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.