search

Home  >  Q&A  >  body text

Function output problem, output is uncontrolled.

function generateTree($items)
 {       
 $items = Db::name('tree')->column('id,pid,name');        
$tree = array();        
foreach ($items as $item) {
    if (isset($items[$item['pid']])) { 
       $items[$item['pid']]['son'][] = &$items[$item['id']];
        } else { 
        $tree[] = &$items[$item['id']]; 
       } 
       } 
       return $tree; 
       }
       $tree = generateTree(0); 
  function getTreeData($tree)
      { 
       foreach ($tree as $t) {
        echo "<li><span><i " . "class=\"icon-minus-sign\"></i>" .$t["name"]. "</span>\n<a href=\"info.php?id=".$t["id"]."\" target=\"_blank\">详细</a><ul>\n"; 
      if (isset($t['son'])) {
                getTreeData($t['son']);
             } 
             echo "</ul>\n</li>\n"; 
            } 
        } 
        $list = getTreeData($tree);
        $this->assign('list',$list);    
    return $this->view->fetch();
 }

1.jpg

The static file is like this1-3.jpg

is directly output to 1-1.jpg

and is out of control. If you use variables2.jpg

Only output one level, and there will be no more after that.2-2.jpg

Masters, please teach me how to write, thank you.

P粉318199689P粉318199689910 days ago799

reply all(4)I'll reply

  • autoload

    autoload2022-07-26 09:01:34

    You are not out of control, this function echoes, just delete the echo, and the array is returned directly with variables

    image.png

    Splice directly in the view

    reply
    0
  • autoload

    autoload2022-07-25 21:28:30

    Don’t write anything on the page, just print the variables transferred from the controller to see what they are

    reply
    1
  • P粉318199689

    is an array Array ( [0] => Array ( [id] => 1 [pid] => 0 [name] => Youlou [son] => Array ( [0] => Array ( [id] => 2 [pid ] => 1 [name] => 火[son]

    P粉318199689 · 2022-07-25 21:30:55
    P粉318199689

    array(14) { [1] => array(3) { ["id"] => int(1) ["pid"] => int(0) ["name"] => string(6) "There is a building" } [2] => array(3) { ["id"] => int(2) ["pid"] => int(1) ["name"] => string(3) "burn" } [3] => array(3) { ["id"] => int(3) ["pid"] => int(2) ["name"] => string(6) "Jingbao" This is the array read from the database

    P粉318199689 · 2022-07-25 21:33:03
  • Cancelreply