Level Count Result

I have an Adjacency list mode structure and I want to count the entire parent level header, e.g. Food = (2,4,3), Fruit = (3,3)

Tree table structure

enter image description here

after that make a tree like this

enter image description here

with this code I get the right general, as for Food = 9, Fruit = 6

function display_children($parent, $level) 
{

 $result = mysql_query('SELECT title FROM tree '.'WHERE parent="'.$parent.'"');
 $count = 0;
  while ($row = mysql_fetch_array($result))
   {
    $data=  str_repeat(' ',$level).$row['title']."\n";
    echo $data;
    $count += 1 + $this->display_children($row['title'], $level+1);
   }  
    return $count; 
 }

call function

 display_children(Food, 0) 

Result: 9 // but I want to get the result, for example, 2,4,3

But I want to get a final final result, for example, for food 2,4,3 and for fruit 3,3 by level

so plz guide how to get the general level

+5
source share
6 answers

If you want to get the amounts by level, then force the function to return them by level.

function display_children($parent, $level) 
{

 $result = mysql_query('SELECT title FROM tree WHERE parent="'.$parent.'"');
 $count = array(0=>0);
  while ($row = mysql_fetch_array($result))
   {
    $data=  str_repeat(' ',$level).$row['title']."\n";
    echo $data;
    $count[0]++;
    $children= $this->display_children($row['title'], $level+1);
    $index=1;
    foreach ($children as $child)
    {
     if ($child==0)
      continue;
     if (isset($count[$index]))
      $count[$index] += $child;
     else    
      $count[$index] = $child;
     $index++;
    }
   }  
    return $count; 
 }

, , . - , , . , :

$result=display_children("Food", 0) ;
var_export($result);//For exact info on all levels 
echo $result[0];//First level, will output 2
echo $result[1];//Second level, will output 4
echo $result[2];//Third level, will output 3

, , , id 10 (Beef) "Meat" "Beat", .

, .

+2
function display_children($parent, $level) 
{

 $result = mysql_query('SELECT title FROM tree '.'WHERE parent="'.$parent.'"');
 $count = "";
  while ($row = mysql_fetch_array($result))
   {
    $data=  str_repeat(' ',$level).$row['title']."\n";
    echo $data;
    if($count!="")   
        $count .= (1 + $this->display_children($row['title'], $level+1));
    else
        $count = ", ".(1 + $this->display_children($row['title'], $level+1));
   }  
    return $count; 
 }

..

+3

article , , mysql

+1

, , .

, ...

item             id
-------------+------
Food         |  1
Fruit        |  1.1
Meat         |  1.2
Red Fruit    |  1.1.1
Green Fruit  |  1.1.2
Yellow Fruit |  1.1.3
Pork         |  1.2.1

, . , - - .

, 2- , .

select count(*) from items
where id regexp '^[0-9]+.[0-9]+$'

-

select count(*) from items
where id regexp '^[0-9]+.[0-9]+.[0-9]+$'

2

select count(*) from items
where id regexp '^[0-9]+.[0-9]+$'
and id like "1.%"

, , , .

, " ", . , DB, ? , - , , , , , , , t , .

+1

php:

<?php

class LevelDepCount{

    private $level_count=array();

    /**
     * Display all child of an element
     * @return int Count of element
     */
    public function display_children($parent, $level, $isStarted=true) 
    {
    if($isStarted)
            $this->level_count=array(); // Reset for new ask
     $result = mysql_query('SELECT title FROM tree '.'WHERE parent="'.$parent.'"');
     $count = 0; // For the level in the section
      while ($row = mysql_fetch_array($result))
       {
        $data=  str_repeat(' ',$level).$row['title']."\n";
        echo $data;
        $count += 1 + $this->display_children($row['title'], $level+1,false);
       }
        if(array_key_exists($level, $this->level_count))
            $this->level_count[$level]+=$count;
        else
            $this->level_count[$level]=$count;
            return $count; 
    }

    /** Return the count by level.*/
    public function getCountByLevel(){
        return $this->level_count;
    }

}

$counter=new LevelDepCount();
$counter->display_children("Food",0);
var_dump($counter->getCountByLevel());

?>
0

, ( ):

/* Get all the data in one swoop and arrange it for easy mangling later */
function populate_data() {
    $result = mysql_query('SELECT parent, COUNT(*) AS amount, GROUP_CONCAT(title) AS children FROM tree GROUP BY parent');
    $data = array();
    while ($row = mysql_fetch_assoc($result)) {
       /* Each node has the amount of children and their names */
       $data[$row['parent']] = array($row['children'], int($row['amount']));
    }
    return $data;
}

/* The function that does the whole work */
function get_children_per_level($data, $root) {
    $current_children = array($root);
    $next_children = array();
    $ret = array();

    while(!empty($current_children) && !empty($next_children)) {
        $count = 0;
        foreach ($current_children as $node) {
            $count += $data[$node][0]; /* add the amount */
            $next_children = array_merge($next_children, explode($data[$node][1])); /* and its children to the queue */
        }
        ret[] = $count;
        $current_children = $next_children;
        $next_children = array();
    }

    return $ret;
}

$data = populate_data();
get_children_per_level($data, 'Food');

, , , . , , . , .

, , . , populate_data , , , get_children_per_level , .

, "" . , - : P. , . , Vegetables -> Red -> Pepper, Red Fruit Red.

One more note - my code will go into an infinite loop if the database data is not a tree. If there is any cycle on the chart, it will never end. This error can be easily resolved by storing the array $visitedwith all the nodes that have already been visited, and not pushing them into the array $next_childrenin a loop (possibly with array_diff($data[$node][1], $visited).

0
source

All Articles