hi!
i'm stuck with hierarchial fold out data display taken from database.
say i've got main menu elements:
if one category is pressed and it has children, these children should appear.
audio is accessed:
Code:
home
audiospeakers
headphones
foo
speakers is accessed:
Code:
home
audiospeakers2.1
4.1
headphones
foo
foo is accessed and audio->speakers are closed:
Code:
home
audio
foosomething
my db layout:
Code:
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| cat_id | int(10) unsigned | | PRI | NULL | auto_increment |
| parent_cat_id | int(100) | YES | MUL | NULL | |
| cat_name | varchar(150) | | UNI | | |
| cat_order | int(11) | | | 0 | |
+---------------+------------------+------+-----+---------+----------------+
finally, my php code:
PHP Code:
function select( $level = 0, $pre = '' )
{
$SQL = 'SELECT
*
FROM
' . CATEGORIES_TABLE . '
WHERE
parent_cat_id = ' . $this->parent . ' ORDER BY cat_id';
$result = mysql_query( $SQL ) or die( mysql_errno() );
$i = 0;
while( $row = mysql_fetch_assoc( $result ) )
{
$this->cats .= str_repeat( ' ', $level ) . "<a href=\"" . $_SERVER['PHP_SELF'] . "?cat_id="
. $row['cat_id'] . "\">"
. $row['cat_name'] . "</a><br>" . "\n";
$this->parent = $row['cat_id'];
if( $_REQUEST['cat_id'] == $row['cat_id'] )
{
$this->select( $level + 1, $pre = $row['parent_cat_id'] );
}
//before i fetched the entire structure
//$this->select( $level + 1, $pre = $row['parent_cat_id'] );
$i++;
}
}
initially, it displays all the main level cats. then, if any category is hit and one's got children they're displayed too, this condition is responsible of it: 'if( $_REQUEST['cat_id'] == $row['cat_id'] )'. but if this child cat is accessed, condition is evaluated to false b/c $_REQUEST['cat_id'] != $row['cat_id'] and it doesn't fold out anylonger. any ideas on how to implement such behaviour?
thanx!