You are here: Re: Generic MySQL/PHP Tree Schema? « All PHP « IT news, forums, messages
Re: Generic MySQL/PHP Tree Schema?

Posted by Tyno Gendo on 05/16/07 09:14

ZeldorBlat wrote:
> On May 14, 12:52 pm, ashore <shor...@gmail.com> wrote:
>> Folks, any url's around re subject matter? Performance isn't
>> necessarily a problem, and the depth will be under, say, twenty.
>>
>> Thanks, all.
>>
>> --AS
>
> <http://www.google.com/search?q=sql+tree+structure>
>

If you're looking to create a menu type tree you need to look into
recursion, i use a function like below, not saying this is the best as
I'm a newbie but it work and creates a menu tree, I then use another
function to parse this tree into the format i need (usually html) using
another similar recursing function.

public function makeTree( $menu_seq, $depth ) {

$sql = "SELECT * FROM menu WHERE menu_parent=" . $menu_seq . "
ORDER BY menu_dspseq ASC;";
$ds = mysql_query( $sql ) or die(mysql_error());
if (mysql_num_rows($ds)>0) {
while ( $dr = mysql_fetch_assoc($ds) ) {
$sql = "SELECT COUNT(menu_seq) FROM menu WHERE
menu_parent = " . $dr['menu_seq'];
$dsc = mysql_query($sql);
$drc = mysql_fetch_array($dsc);
$array[$dr['menu_seq']]['menu_text'] =
htmlentities($dr['menu_text']);
$array[$dr['menu_seq']]['menu_parent'] =
$dr['menu_parent'];
$array[$dr['menu_seq']]['menu_page'] =
$dr['menu_page'];
$array[$dr['menu_seq']]['depth'] = $depth;
if ($drc>0) {
$array[$dr['menu_seq']]['children'] =
$this->makeTree( $dr['menu_seq'], $depth+5 );
} else {
$array[$dr['menu_seq']]['children'] = null;
}
}
mysql_free_result($ds);
}
return (isset($array) ? $array : false);

}

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация