我有一个数据库表
course(id, name, duration);
subject (id, course_id, elective_id, name)
主题表的样本数据是:
1, 100, NULL, subject1
2, 100, NULL, subject2
3, 100, NULL, subject3
4, 100, 1, subject4
5, 100, 1, subject5
6, 100, 2, subject6
7, 100, 2, subject7
8, 100, 1, subject8
9, 100, 2, subject9
具有NULL的行是核心主题,而其他行是选修课。我想在html中显示格式如下......
<ul id="select">
<li>subject1</li>
<li>subject2</li>
<li>subject3</li>
<li>
<ul id="elective1">
<li>subject4</li>
<li>subject5</li>
<li>subject8</li>
</ul>
</li>
<li>
<ul id="elective2">
<li>subject6</li>
<li>subject7</li>
<li>subject9</li>
</ul>
</li>
</ul>
这是我尝试过的不起作用:
SQL = 'SELECT * FROM subject ORDER BY elective_id ASC';
逻辑:
<ul id="select">
foreach($resource as $field){
if($field->elective_id == NULL){
echo '<li>'.$field->name.'</li>';
} else {
echo '<li>';
echo '<ul id="'.$field->elective_id.'">';
echo '<li>'.$field->name.'</li>';
echo '</ul>';
echo '</li>';
}
}
</ul>