【问题标题】:PHP mysql create tree-like hierarchy and their countPHP mysql 创建树状层次结构及其计数
【发布时间】:2014-11-09 05:44:10
【问题描述】:

我还是 PHP 和 MySQL 的新手。这个问题看起来很简单,但不知何故,我无法通过正确使用 foreach 和数组来计算递归公式来创建树状层次结构。

这是表结构

 CREATE TABLE IF NOT EXISTS `table` (
`id` int(2) NOT NULL,
  `lecturer` varchar(50) NOT NULL,
  `subject` varchar(9) NOT NULL,
  `section` int(2) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

INSERT INTO `table` (`id`, `lecturer`, `subject`, `section`) VALUES
(1, 'Prof A', 'info2222', 1),
(2, 'Prof A', 'info2222', 2),
(3, 'Prof A', 'info3333', 1),
(4, 'Prof A', 'info3333', 3),
(5, 'Prof B', 'info4444', 1);

这是我想要的示例输出:

=================================================
|  lecturer > subject > section |  count total  |
=================================================
|  Prof A                       |   4           |
|  |---info2222                 |   2           |
|  |    |---1                   |   1           |
|  |    |---2                   |   1           |
|  |                            |               |
|  |---info3333                 |   2           |
|       |---1                   |   1           |
|       |---3                   |   1           |
|                               |               |
|  Prof B                       |   1           |
|   |---info4444                |   1           |
|       |---1                   |   1           |
=================================================

我的完整代码(当前)

<html>  
<head>
<?php

mysql_select_db('testing',mysql_connect('localhost','root',''))or die(mysql_error());

function count_recursive($array)
{
    $c = 0;
    foreach($array as $value)
    {   
        if(is_array($value))
            $c += count_recursive($value);
        else
            $c++;
    return $c;
    }
}

?>

</head>

<body>

<?php

    $query = $pdo->query("Select * from table");
    $arr = [];
    while($data = $query->fetch())
    {
        $arr[$data['lecturer']][$data['subject']][] = $data['section'];
    }

    foreach($arr as $lecturer => $lvalues)
    {
        echo $query['lecturer'] ;
        foreach($lvalues as $subject => $svalues)
        {
           echo $query['subject'] ;
            foreach($svalues as $section)
            {
                echo $query['section'] ;
            }
        }
    }

?>

</body>
</html> 

【问题讨论】:

    标签: php mysql count tree


    【解决方案1】:

    这样的事情应该可以工作:

    $query = $pdo->query("Your select...");
    $arr = [];
    while($data = $query->fetch()){
        $arr[$data['lecturer']][$data['subject']][] = $data['section'];
    }
    

    之后你可以遍历 (3d) 数组:

    foreach($arr as $lecturer => $lvalues){
        //echo your lecturer here
        foreach($lvalues as $subject => $svalues){
            //echo your subject here
            foreach($svalues as $section)
                //echo sour section here
    }
    

    递归地计算所有你可以使用的东西:

    function count_recursive($array){
        $c = 0;
        foreach($array as $value)
            if(is_array($value))
                $c += count_recursive($value);
            else
                $c++;
        return $c;
    }
    

    【讨论】:

    • 帮助!我怎样才能修复这个错误--> $arr = [];?解析错误:语法错误,第 31 行出现意外的“[”
    • 您应该更新您的 PHP 版本。 $arr = 数组();适合你。
    猜你喜欢
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多