0

我已经尝试在网站上实现树视图,但我现在遇到了一些错误,我无法指出是什么导致了它们,也许有人可以为我解释一下情况。

我有下表:

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 -> 类别 rubriek 的 id
-> 告诉我们类别是否是子类别,如果不是,则值为 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(); ?>

我收到以下错误:
页面现在一直在加载,但屏幕上没有出现任何内容。

我可能会犯一些新手错误,但非常感谢一些帮助

4

1 回答 1

0

变量 $conn 在函数外部定义。在函数内部它是不可见的,即使它是之前定义的。您可以执行以下任何选项:

  • 将 $conn 作为参数传递给函数。

    函数 CategoryList($conn) { .... }

  • 将 $conn 定义为您需要的函数内的全局变量

    功能类别列表(){

    全球$conn;...

    }

于 2013-06-05T14:11:24.987 回答