我尝试在网站上实施树视图,但我现在有一些错误,我无法指出导致它们的原因,也许有人可以解决一些问题关于我的情况。
我已经得到了以下表格:
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
我已经填写了以下测试数据:
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(); ?>
我收到以下错误:
该页面现在只是继续加载,但屏幕上没有任何内容。
我可能会犯一些新手的错误,但一些帮助会非常感激
答案 0 :(得分:0)
变量$ conn在函数外部定义。在函数内部,它是不可见的,即使它是先前定义的。您可以执行以下任一选项:
将$ conn作为参数传递给函数。
function CategoryList($ conn) { .... }
将$ conn定义为您需要的函数内的全局变量
函数CategoryList() {
全球$ conn; ...}