Re: Better way of doing this? (menu and submenus)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hi Ryan,

(btw: if this thread needs spicing up with DoS threats give me a shout
and I'll see if can come up with something ;-)

with regard to your original question/comments - do not go the route of
using 2 tables... you will very quickly find it unwieldy and limiting
(e.g. one table for each level in your menu? or if a element is a parent
and a child in which table does it belong?)

Mark Steudel wrote:
> You could use a recursive function and keep it all in one table. 
> 
> My Table is similar to the one below:
> 
> ID	NAME	PID	DISPLAY_ORDER

I would recommend this basic structure as well (there are other ways of
specifying a tree structures but I can't recommend them as I'm still learning
about them) as it is the most obvious/simplest form of hierarchical data structure.

> 
> Here's crude recursive function to display this in a list typical of what
> you used to style a menu. The great thing about this is that you could have
> unlimited submenus if you wanted ...

using Marks example as a starting point - I would suggest spliting the functionality
into [atleast] 2/3 parts (trying to make the functionality generic so that it is
flexible):

1. grab all the data rows you need in a single query (or as little queries
as possible)

2. use the retrieved data to build a mutlidimensional array
that reflects/models the structure of the menu you want to output. (this
could also be a collection of objects)

3. use the m-dimensional array to generate the html that will be outputted.

basically it comes down to minimizing the number of calls to the DB, making
it possible to generate different menu structures (e.g. the whole tree just a
certain level of the tree, only the siblings of the selected node and all the
relevant parent, etc) and giving yourself the ability to using different types
of actual output (even if in practice that means different kinds of html, e.g.
nested <UL>s, a table, simple horizonal list, etc)

> 
> function displayMenu( $pid )
> {
> 	global $db;
> 	
> 	$res =& $db->query( "SELECT * FROM tblmenu WHERE pid = '".$pid."'
> ORDER BY display_order" );
> 	mysqlErrorCheck( $res );
> 
> 	while( $res->fetchInto( $objData ) )
> 	{
> 		$html .= "<li>".$objData['name'];
> 		$html .= "<ul>".displayMenu( $objData['id'] )."</ul>";
> 		$html .= "</li>";
> 	}
> 	
> 	return $html;
> }
> 
> 
> echo "<ul>".displayMenu('0')."</ul>";
> 
> 
> -----Original Message-----

...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux