-
-
//I use mysql PDO for the database, 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 of $id is set to 0 because I set the fid of the top-level category to 0 in the database
- */
- 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);
< ;/P>
The function first queries all classes with fid 0
Through the while loop one by one, the back investigation is performed to find the subclass whose fid is the id of the current class
-
Copy code
|