PHP'de sonsuz derinlikte menü yapmak için düzgün bir tablo yapısı ve çok basit bir recursive fonksiyon yeterli. Tablo Yapısı CREATE TABLE `category` ( `sid` int(12) unsigned NOT NULL auto_increment, `module` set('estate','cars') collate utf8_bin NOT NULL, `parentId` int(6) NOT NULL, `title` varchar(255) collate utf8_bin NOT NULL, `isDefault` int(1) NOT NULL default '1', PRIMARY KEY (`sid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=14 ; INSERT INTO `category` VALUES (1, 0x657374617465, 0, 0x536174c4b16cc4b16b20456d6c616b, 1); INSERT INTO `category` VALUES (2, 0x657374617465, 1, 0x45766c6572, 1); INSERT INTO `category` VALUES (3, 0x657374617465, 1, 0xc4b0c59f7965726c657269, 1); INSERT INTO `category` VALUES (4, 0x657374617465, 1, 0x417273616c6172, 1); INSERT INTO `category` VALUES (5, 0x657374617465, 0, 0x4b6972616cc4b16b20456d6c616b, 1); INSERT INTO `category` VALUES (6, 0x657374617465, 5, 0x45766c6572, 1); INSERT INT...
PHP'de sonsuz derinlikte menü yapmak için düzgün bir tablo yapısı ve çok basit bir recursive fonksiyon yeterli.
Tablo Yapısı
CREATE TABLE `category` (
`sid` int(12) unsigned NOT NULL auto_increment,
`module` set('estate','cars') collate utf8_bin NOT NULL,
`parentId` int(6) NOT NULL,
`title` varchar(255) collate utf8_bin NOT NULL,
`isDefault` int(1) NOT NULL default '1',
PRIMARY KEY (`sid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=14 ;
INSERT INTO `category` VALUES (1, 0x657374617465, 0, 0x536174c4b16cc4b16b20456d6c616b, 1);
INSERT INTO `category` VALUES (2, 0x657374617465, 1, 0x45766c6572, 1);
INSERT INTO `category` VALUES (3, 0x657374617465, 1, 0xc4b0c59f7965726c657269, 1);
INSERT INTO `category` VALUES (4, 0x657374617465, 1, 0x417273616c6172, 1);
INSERT INTO `category` VALUES (5, 0x657374617465, 0, 0x4b6972616cc4b16b20456d6c616b, 1);
INSERT INTO `category` VALUES (6, 0x657374617465, 5, 0x45766c6572, 1);
INSERT INTO `category` VALUES (7, 0x657374617465, 5, 0xc4b0c59f7965726c657269, 1);
INSERT INTO `category` VALUES (8, 0x657374617465, 5, 0x417273616c6172, 1);
INSERT INTO `category` VALUES (9, 0x63617273, 0, 0x536174c4b16cc4b16b20417261c3a76c6172, 1);
INSERT INTO `category` VALUES (10, 0x63617273, 9, 0x54696361726920417261c3a76c6172, 1);
INSERT INTO `category` VALUES (11, 0x63617273, 9, 0x42696e656b204f746f6d6f62696c6c6572, 1);
INSERT INTO `category` VALUES (12, 0x657374617465, 2, 0x536174c4b16cc4b16b2065766c657220616c74202d32, 2);
INSERT INTO `category` VALUES (13, 0x657374617465, 2, 0x536174c4b16cc4b16b2065766c657220616c74, 2);
PHP Script'i
<?php
$dbLink = mysql_connect( "localhost", "root", "root" );
mysql_select_db( "veritabani", $dbLink );
function treeMenu( $Parent = 0 )
{
global $Menu;
$Buff = '<ul>';
foreach( $Menu[$Parent] as $Item )
{
$Buff .= '<li>';
$Buff .= '<a href="category.php?parentId='
. $Parent . '&categoryId=' . $Item['sid']
. '">' . $Item['title'] . '</a>';
if ( isset( $Menu[$Item['sid']] ) )
$Buff .= treeMenu( $Item['sid'] );
$Buff .= '</li>';
}
$Buff .= '</ul>';
return $Buff;
}
$result = mysql_query( "SELECT * FROM category" );
if ( mysql_num_rows( $result ) )
{
$Menu = array();
while ( $ROW = mysql_fetch_assoc( $result ) )
{
$Menu[$ROW['parentId']][] = $ROW;
}
echo treeMenu();
}
?>
Yorumlar