![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
![]() |

|
| Knowledge Base Most common error and how to trouble shoot them off |
![]() |
|
Sorting MySQL Results
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Fixed Error!
Posts: 141
Location: Chennai
Join Date: Feb 2007
Rep Power: 2
IM:
|
What I'd like to do is display a category tree inside a select box like this for example: Web Servers -- Apache -- IIS Web Dev -- PHP --- PHP & Databases --- PHP for Windows The mysql table structure is like this: category_id parent_id category_name Would I use a mysql query for this or let PHP sort the data ? Help would be appreciated! |
|
|
|
|
|
|
|
|
#2 (permalink) |
|
Fixed Error!
Posts: 141
Location: Chennai
Join Date: Feb 2007
Rep Power: 2
IM:
|
<?php $menu = array(); $menu[] = array( 'id' => 1, 'parent_id' => 0, 'name' => 'a' ); $menu[] = array( 'id' => 2, 'parent_id' => 1, 'name' => 'a.1' ); $menu[] = array( 'id' => 3, 'parent_id' => 0, 'name' => 'b' ); $menu[] = array( 'id' => 4, 'parent_id' => 3, 'name' => 'b.1' ); $menu[] = array( 'id' => 5, 'parent_id' => 3, 'name' => 'b.2' ); $menu[] = array( 'id' => 6, 'parent_id' => 5, 'name' => 'b.2.1' ); // acts like a SELECT statement function selectWhereParentIdIs( $id ) { global $menu; $out = array(); for( $i=0; $i<count($menu); $i++ ) { if ($menu[$i]['parent_id']==$id) $out[] = $menu[$i]; } return $out; } function menuToSelect( $id, $depth ) { $ar = selectWhereParentIdIs( $id ); if ( count($ar) > 0 ) { reset( $ar ); foreach( $ar as $value ) { $out .= '<option>'; if ($depth>0) $out .= '-'; $out .= str_repeat( '-', $depth ) . $value['name']; $out .= '</option>'; $out .= menuToSelect( $value['id'], $depth+1 ); } return $out; } else { return ''; } } echo '<form><select>'; echo menuToSelect( 0, 0 ); echo '</select>'; ?> |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|