Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 选择表中的术语(类别)并在选择框中输出它们_Php_Mysql_List_Select_Taxonomy - Fatal编程技术网

Php 选择表中的术语(类别)并在选择框中输出它们

Php 选择表中的术语(类别)并在选择框中输出它们,php,mysql,list,select,taxonomy,Php,Mysql,List,Select,Taxonomy,这是我的术语表类别的数据库结构: id | name | parent_id | level | position -------------------------------------------------------------- 1 | term 1 | NULL | 0 | 1 2 | term 2 | 1 | 1 | 1

这是我的术语表类别的数据库结构:

   id   |   name   |   parent_id   |   level   |   position   
--------------------------------------------------------------
   1    |   term 1 |   NULL        |   0       |   1
   2    |   term 2 |   1           |   1       |   1
   3    |   term 3 |   1           |   1       |   2
   4    |   term 4 |   NULL        |   0       |   2
   5    |   term 5 |   4           |   1       |   1
因此,术语2和3是1的第一级子级,而术语5是4的第一级子级

这是我的疑问:这是不正确的,这应该被修正

SELECT
    `id`,
    `name`
FROM
    `terms`
ORDER BY
    `position` ASC,
    `level` ASC
这是我的php:

$terms = array();

// query part

if(!$this->_db->resultRows > 0)
  return $terms;

while($d = $this->_db->fetch($r))
{
  $terms[$d->id] = new Term($d->id);
}     

return $terms;
当前结果:

term 1
  term 2
  term 5
term 4
 term 3
但结果应该是:

term 1
  term 2
  term 3
term 4
  term 5
我不知道如何修改查询以获得正确的结果 目标是将其输出到多个选择框中
我知道如何使用嵌套列表,但不能嵌套select,所以有很多方法可以解决这类问题。我将介绍的所有方法都不要求表中有level列。我认为这是多余的数据,因为它可以从其他列中找到的信息推断出来

如果您知道最大级别仅为1,则最大深度为2,您可以使用如下查询:

SELECT
        t.`id`,
        t.`name`,
        IF(p.`position` IS NULL, t.`position`*{$row_count}, p.`position`*{$row_count}+t.`position`) AS display_order
FROM
        `terms` t
        LEFT JOIN `terms` p ON p.`id` = t.`parent_id`
ORDER BY
        display_order
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$sth = $dbh->prepare('SELECT id,name FROM terms WHERE parent_id IS NULL ORDER BY position');
$sth->execute(array(NULL));
$terms = $sth->fetchAll();

$sql = 'SELECT id,name FROM terms WHERE parent_id = ? ORDER BY position';
$terms_to_check = $terms;
$terms = array();
while(count($terms_to_check))
{
        $k = array_shift($terms_to_check);
        $terms[] = $k;
        $sth = $dbh->prepare($sql);
        $sth->execute(array($k['id']));
        $results = $sth->fetchAll();
        $terms_to_check = array_merge($results, $terms_to_check);
}
其中,计算$row_计数:

SELECT COUNT(*) FROM `terms`
有多种方法可以修改此SQL,使其能够在更高的级别和深度下工作,但查询需要在其支持的每个最大级别/深度下变得更大

如果您不确定您将拥有的级别数量,那么您可能应该执行以下操作:

SELECT
        t.`id`,
        t.`name`,
        IF(p.`position` IS NULL, t.`position`*{$row_count}, p.`position`*{$row_count}+t.`position`) AS display_order
FROM
        `terms` t
        LEFT JOIN `terms` p ON p.`id` = t.`parent_id`
ORDER BY
        display_order
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$sth = $dbh->prepare('SELECT id,name FROM terms WHERE parent_id IS NULL ORDER BY position');
$sth->execute(array(NULL));
$terms = $sth->fetchAll();

$sql = 'SELECT id,name FROM terms WHERE parent_id = ? ORDER BY position';
$terms_to_check = $terms;
$terms = array();
while(count($terms_to_check))
{
        $k = array_shift($terms_to_check);
        $terms[] = $k;
        $sth = $dbh->prepare($sql);
        $sth->execute(array($k['id']));
        $results = $sth->fetchAll();
        $terms_to_check = array_merge($results, $terms_to_check);
}

顺便说一句,我建议使用PDO。

MySQL不支持递归函数,因此它不太适合这种存储分层数据的邻接列表模型。您应该考虑使用嵌套集合或闭包表重构数据。有关更多信息,请参阅。您知道级别的限制吗?目前只有两个级别。查询是否需要处理两个以上的级别?是的,级别应该是无限的