问题 1 - 状态 = 已解决
使用的表是:
CREATE TABLE Rubriek(
rubrieknaam char(50) not null, /* char(24) */
rubrieknummer numeric(38) not null, /* numeric(3) */
rubriek numeric(38) null, /* numeric(3) */
volgnr numeric(38) not null, /* nuemric(2) */
constraint pk_rubrieknummer primary key(rubrieknummer),
constraint fk_rubriek foreign key(rubriek) references Rubriek (rubrieknummer)
)
我一直在尝试在 PHP 中制作一个垂直菜单,该菜单将填充提供的表格,但我遇到了一些困难。我尝试了一些代码,但我无法让它为我工作:(。菜单由类别(rubrieken)和子类别组成。
表格上的更多说明:
rubrieknaam -> rubrieknummer 类别的
名称 -> rubriek 类别的 id
-> 告诉我们一个类别是否是子类别,如果不是,则该值将为 null
我收到一些“注意:未定义索引”错误的那一刻。更具体地说,在以下几行:
if($parent != $menu['rubriek']){//if not seen item before
echo "<li>".$menu['rubrieknummer']."</li>";//echo item
if($parent != $menu['rubriek']){//if not seen before not then the value will be null:
我尝试了以下但无济于事。
//connection to SQL Database
$connectionInfo = array( "UID"=>$uid,"PWD"=>$pwd,"Database"=>$databaseName);
//Connecting with SQL Authentication
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$query = sqlsrv_query($conn, "SELECT * FROM Rubriek");
$parent=0;
$sub=0;
echo "<ul>";//start list
while($menu = sqlsrv_fetch_array($query)){
if($parent != $menu['rubriek']){//if not seen item before
if($sub != 0){echo "</ul>";}else{$sub++;}//if not first submenu, close submenu before. If first sub sub++.
echo "<ul>";}//open submenu
echo "<li>".$menu['rubrieknaam']."</li>";//echo item
if($parent != $menu['rubriek']){//if not seen before
$parent = $menu['rubriek']; //set to seen before so next loop will be recognised
}
}
echo "</ul>"; //end list
?>
问题 2
我已经实施了提到的修复 Royal BG 并且它有效!但现在有一个不同的问题。我还想实现子类别,并为几个类别制作了子类别。
例如我做了下一个测试数据:
Rubrieknaam | rubrieknummer | rubriek
(= Name of Category) | (=Category ID)| (=Category is a subcategory of the following category):
----------------------------------------------------------------------------------
Cars | 1 | Null
Audio | 2 | Null
Ford | 3 | 1 (subcategory of Cars)
Toyota | 4 | 1 (subcategory of Cars)
Speakers | 5 | 2 (subcategory of Audio)
Microphones | 6 | 2 (subcategory of Audio)
Instruments | 7 | Null
Guitar | 8 | 7 (subcategory of Instruments)
记录按此顺序在数据库中列出。前两个类别被列为一个类别。其余的都被列为“音频”的子类别,即使“乐器”应该是一个新类别而不是子类别。