php無限級分類執行個體三

來源:互聯網
上載者:User

資料庫教程


--
-- Table structure for table `category`
--
CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catpath` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;
--
-- Dumping data for table `category`
--
INSERT INTO `category` (`id`, `catpath`, `name`) VALUES
(1, '0', '網站首頁'),
(2, '0-1', 'Linux OS'),
(3, '0-1', 'Apache伺服器'),
(4, '0-1', 'MySQL資料庫'),
(5, '0-1', 'PHP指令碼語言'),
(6, '0-1-2', 'Linux 系統教程'),
(7, '0-1-2', 'Linux 網路技術'),
(8, '0-1-2', 'Linux 安全基礎'),
(9, '0-1-2-7', 'Linux LAMP'),
(10, '0-1-3-10', 'apache Server');

php教程代碼

$conn = mysql教程_connect ( 'localhost', 'root', '' );
mysql_select_db ( 'test', $conn );
mysql_query ( 'set names UTF8' );
$sql = "select id,concat(catpath,'-',id) as abspath,name from category order by abspath";
$query = mysql_query ( $sql );
while ( $row = mysql_fetch_array ( $query ) ) {
/**
* 第一種展示方法
*/
/*$space = str_repeat ( ' ', count ( explode ( '-', $row ['abspath'] ) ) - 1 );
echo $space . $row ['name'] . '<br>';*/
/**
* 第二種展示方法
*/
$space = str_repeat ( ' ', count ( explode ( '-', $row ['abspath'] ) ) - 1 );
$option .= '<option value="' . $row ['id'] . '">' . $space . $row ['name'] . '</option>';
}
echo '<select name="opt">' . $option . '</select>';

效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.