必要なコード出力:
ビジュアル出力:
私のSQLデータベース「テスト」
CREATE TABLE IF NOT EXISTS `menu` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`link` varchar(100) NOT NULL,
`parrent_id` int(11) NOT NULL DEFAULT '0',
`status` varchar(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
--
-- Dumping data for table `menu`
--
INSERT INTO `menu` (`id`, `name`, `link`, `parrent_id`, `status`) VALUES
(1, 'Home', '#home', 0, 'Show'),
(2, 'About', '#about', 0, 'Show'),
(3, 'Our services', '#service', 0, 'Show'),
(4, 'Get involved', '#getinvolved', 0, 'Show'),
(5, 'Donors', '#donors', 0, 'Show'),
(6, 'News', '#news', 0, 'Show'),
(7, 'Contact Us', '#contact', 0, 'Show'),
(8, 'Ourselves', '#ourselves', 2, 'Show'),
(9, 'Objectives', '#objectives', 2, 'Show'),
(10, 'Our Patrons', '#patrons', 2, 'Show');
私のPHPコードはここにあります....
<?php
$dbuser="root";
$dbpass="";
$dbname="test"; //the name of the database
$chandle = mysql_connect("localhost", $dbuser, $dbpass)
or die("Connection Failure to Database");
mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found. " . $dbuser);
function menu($parrent) {
echo "<ul>";
$sql = "SELECT * from menu where parrent_id=".$parrent;
while($res = mysql_fetch_array(mysql_query($sql)))
echo "<li><a href='".$res['link']."'>".$res['name']."</a></li>";
echo "</ul>";
}
$sql = "SELECT * from menu where parrent_id =0";
echo "<ul>";
while($resnew = mysql_fetch_array(mysql_query($sql)))
{
$sql_sub = "SELECT * from menu where parrent_id=".$resnew['id'];
$count = mysql_num_rows(mysql_query($sql_sub));
if($count>0)
{
echo "<li><a href='".$resnew['link']."'>".$resnew['name']."</a>";
menu($resnew['id']);
echo "</li>";
}
else
{
echo "<li><a href='".$resnew['link']."'>".$resnew['name']."</a></li>";
}
}
echo "</ul>";
?>