PHP函数使用echo而不是返回

时间:2015-05-24 14:44:00

标签: php function return echo

我有这个功能,它允许我创建多级菜单

我完成代码后的问题,使用" echo",我试图让它工作但是它没有给我子菜单只显示主菜单!

如果我继续使用echo方法,我会在页面顶部找到代码!

任何想法我应该做什么

回声代码:

function showMenu($level = 0,$site_url,$get_type_sec,$get_sub_type_sec) {

$result = mysql_query("SELECT * FROM pages WHERE parent_id ='$level' order by navigation_order ASC"); 
echo "<ul>";
    while ($node = mysql_fetch_array($result)) { 


            $result2 = mysql_query("SELECT * FROM pages WHERE id ='$node[parent_id]' order by navigation_order ASC"); 
             while ($node2 = mysql_fetch_array($result2)) {$node3="$node2[query_string]/";
             $noder="$node[id]"; 
             $result3 = mysql_query("SELECT * FROM pages WHERE id ='$node2[parent_id]' order by navigation_order ASC"); 
             while ($node3 = mysql_fetch_array($result3)) {$node2="$node3[query_string]/"; }
             }


            echo "<li><a href='$site_url/$node2$node3$node[query_string]/'> $node[title] </a> ";
            $hasChild = mysql_fetch_array(mysql_query("SELECT * FROM pages WHERE id ='$node[id]'")) != null;
            // If user request same page open menu 
            IF ($hasChild && $node['query_string']=="$get_type_sec") {
                showMenu($node['id'],$site_url,$get_type_sec,$get_sub_type_sec);    
            }
            elseif($hasChild &&  $node['query_string']=="$get_sub_type_sec") {

                showMenu($node['id'],$site_url,$get_type_sec,$get_sub_type_sec);


            }

            echo "</li>";


    }
echo "</ul>";

}

返回代码

function showMenu($level = 0,$site_url,$get_type_sec,$get_sub_type_sec) {

$result = mysql_query("SELECT * FROM pages WHERE parent_id ='$level' order by navigation_order ASC"); 
$echo = "<ul>";
return = $echo;
    while ($node = mysql_fetch_array($result)) { 


            $result2 = mysql_query("SELECT * FROM pages WHERE id ='$node[parent_id]' order by navigation_order ASC"); 
             while ($node2 = mysql_fetch_array($result2)) {$node3="$node2[query_string]/";
             $noder="$node[id]"; 
             $result3 = mysql_query("SELECT * FROM pages WHERE id ='$node2[parent_id]' order by navigation_order ASC"); 
             while ($node3 = mysql_fetch_array($result3)) {$node2="$node3[query_string]/"; }
             }


            $echo .= "<li><a href='$site_url/$node2$node3$node[query_string]/'> $node[title] </a> ";
return = $echo;
            $hasChild = mysql_fetch_array(mysql_query("SELECT * FROM pages WHERE id ='$node[id]'")) != null;
            // If user request same page open menu 
            IF ($hasChild && $node['query_string']=="$get_type_sec") {
                showMenu($node['id'],$site_url,$get_type_sec,$get_sub_type_sec);    
            }
            elseif($hasChild &&  $node['query_string']=="$get_sub_type_sec") {

                showMenu($node['id'],$site_url,$get_type_sec,$get_sub_type_sec);
            }

            $echo .= "</li>";
            return = $echo;
    }
$echo .= "</ul>";
return = $echo;
}

2 个答案:

答案 0 :(得分:1)

更改return = $ echo以返回$ echo;

答案 1 :(得分:1)

当我在其中调用函数时,我需要添加echo

//before
showMenu($node['id'],$site_url,$get_type_sec,$get_sub_type_sec);
//after
echo showMenu($node['id'],$site_url,$get_type_sec,$get_sub_type_sec);

最终代码为:

 function showMenu($level = 0,$site_url,$get_type_sec,$get_sub_type_sec) {

$result = mysql_query("SELECT * FROM pages WHERE parent_id ='$level' order by navigation_order ASC"); 
$menu= "<ul>";
return $menu;
    while ($node = mysql_fetch_array($result)) { 


            $result2 = mysql_query("SELECT * FROM pages WHERE id ='$node[parent_id]' order by navigation_order ASC"); 
             while ($node2 = mysql_fetch_array($result2)) {$node3="$node2[query_string]/";
             $noder="$node[id]"; 
             $result3 = mysql_query("SELECT * FROM pages WHERE id ='$node2[parent_id]' order by navigation_order ASC"); 
             while ($node3 = mysql_fetch_array($result3)) {$node2="$node3[query_string]/"; }
             }


            $menu.= "<li><a href='$site_url/$node2$node3$node[query_string]/'> $node[title] </a> ";
return $menu;
            $hasChild = mysql_fetch_array(mysql_query("SELECT * FROM pages WHERE id ='$node[id]'")) != null;
            // If user request same page open menu 
            IF ($hasChild && $node['query_string']=="$get_type_sec") {
                echo showMenu($node['id'],$site_url,$get_type_sec,$get_sub_type_sec);    
            }
            elseif($hasChild &&  $node['query_string']=="$get_sub_type_sec") {

                echo showMenu($node['id'],$site_url,$get_type_sec,$get_sub_type_sec);
            }

            $menu.= "</li>";
            return $menu;
    }
$menu.= "</ul>";
return  $menu;
}