我試過在網站上實現樹視圖,但我有一些錯誤atm的時刻,我不能指出是什麼導致他們,也許有人可以爲我闡明一些情況。需要一些幫助解決PHP樹視圖中的錯誤(填充SQL)
我有如下因素表:
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)
)
rubrieknaam - >類別的名稱
rubrieknummer - >類的ID
rubriek - >告訴我們,如果一個類是子類,如果不那麼值爲null
我用下面的TESTDATA填充它:
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)
我實現下面的代碼的functions.php:然後
<?php
function connection($sql){
ini_set('display_errors',true);
$serverName = "WHS\sqlexpress";
$uid = "sa";
$pwd = "projectgroep37";
$databaseName = "EenmaalAndermaal";
//connection to SQL Database
$connectionInfo = array("UID"=>$uid,
"PWD"=>$pwd,
"Database"=>$databaseName);
//Connecting with SQL Authentication
$conn = sqlsrv_connect($serverName, $connectionInfo);
$qry = sqlsrv_query($conn, $sql);
$rs = sqlsrv_fetch_array($qry, SQLSRV_FETCH_BOTH);
return $rs;
}
function hasChild($rubriek)
{
$sql = "SELECT COUNT(*) as count FROM Rubriek WHERE rubriek = ' " . $rubriek . " ' ";
$rs = connection($sql);
return $rs['count'];
}
function CategoryTree($list,$parent,$append)
{
$list = '<li>'.$parent['rubrieknaam'].'</li>';
if (hasChild($parent['rubrieknummer'])) // check if the id has a child
{
$append++; // this is our basis on what level is the category e.g. (child1,child2,child3)
$list .= "<ul class='child child".$append." '>";
$sql = "SELECT * FROM Rubriek WHERE rubriek = ' " . $parent['rubrieknummer'] . " ' ";
$child = connection($sql);
do{
$list .= CategoryTree($list,$child,$append);
}while($child = connection($sql));
$list .= "</ul>";
}
return $list;
}
function CategoryList()
{
$list = "";
$sql = "SELECT * FROM Rubriek WHERE (rubriek = 0 OR rubriek IS NULL)";
$parent = connection($sql);
$mainlist = "<ul class='parent'>";
do{
$mainlist .= CategoryTree($list,$parent,$append = 0);
}while($parent = connection($sql));
$list .= "</ul>";
return $mainlist;
}
?>
的functions.php包含網頁上(頂部)。然後,我贊同在div下面的函數我希望它是:
<?php echo CategoryList(); ?>
,我發現了以下錯誤:
頁面現在只是不斷加載,但沒有出現在屏幕上。
我可能做一些新手的錯誤,但一些幫助,將不勝感激
那些看起來像他們所有的變量範圍的問題 - 你需要傳遞變量中的功能,否則在函數內部的代碼無法「看到」這些變量。 – andrewsi