<ul>
&<li>
を再帰的な PHP 関数に配置するにはどうすればよいですか? 私は次のように作成しましたが、正しく動作しません:
<?php
echo '<ul>';
$sql = mysql_query("select * from categories where parent_id = 0");
while($rs=mysql_fetch_array($sql)){
echo '<li>'.$rs['name'];
echo show_subcategory(($rs['cat_id']));
echo '</li>';
}
echo '</ul>';
function show_subcategory($category_id){
$sql = mysql_query("select * from categories where parent_id ='$category_id'");
if(mysql_num_rows()>0){
echo '<ul>';
while($rs=mysql_fetch_array($sql)){
echo '<li>'.$rs['name'];
echo show_subcategory(($rs['cat_id']));
echo '</ul></li>';
}
}else{
echo '</li>';
}
}
?>
CREATE TABLE IF NOT EXISTS categories (
cat_id int(11) NOT NULL AUTO_INCREMENT,
parent_id int(11) NOT NULL DEFAULT '0',
name varchar(20) NOT NULL,
description text NOT NULL,
slug varchar(50) NOT NULL,
PRIMARY KEY (cat_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
INSERT INTO categories (cat_id, parent_id, name, description, slug) VALUES
(1, 0, 'programming', 'Forum ', 'programming'),
(2, 1, 'php', 'php programming', 'php'),
(3, 1, 'python', '', ''),
(4, 1, 'java', '', ''),
(5, 1, 'visual basic', '', ''),
(6, 2, 'codeigniter', '', ''),
(7, 2, 'joomla', '', '');