我有基本上为动态菜单生成动态链接的功能,现在我基本上想要做的是我需要在函数外声明一个变量然后在函数内部有两个部分我想要获取值并连接在递归调用该函数后,我想在函数外部使用该值,因此看看我得到了什么!这是代码
<?php
define("hostname",$this->db->hostname);
define("username",$this->db->username);
define("password",$this->db->password);
define("database",$this->db->database);
$var="";//this is the variable i want to use
?>
<ul class='nav navbar-nav'>
<li><a href="<?=base_url()?>Home" style="color: white;"><img id="navbar-home" src="<?=base_url()?>images/navbar_hover_home.png" class="img-responsive"></a></li>
<?php
function display_children($parent, $level) {
$conn=mysqli_connect(hostname,username,password,database) or die ("not connected");
$result = mysqli_query($conn,"SELECT a.id, a.title , a.page_id , a.custom_link ,a.priority, Deriv1.Count FROM `menu` a LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent ) Deriv1 ON a.id = Deriv1.parent WHERE a.parent='$parent' ORDER BY a.priority");
while ($row = mysqli_fetch_array($result)) {
if ($row['Count'] > 0) {
$link="";
if($row['page_id']){
$link=base_url().$row['page_id'];
}
else
{
$link=$row['custom_link'];
}
$title=$row['title'];
if($title=="OPERAS PHYSICIAN LOG IN"){
$title="OPERAS Physicians Log-In";
}
else
{
$title= strtoupper(str_replace('-', ' ', clean($row['title']) ));
}
echo "<script>console.log('".$title."')</script>";
echo "<li><a href='" . $link . "'>" .$title . "<span class=caret></span></a>";
echo "<ul class=\"dropdown-menu\">";
global $var;//this is where is want to manipulate the variable
$var.=$title;
display_children($row['id'], $level + 1);
echo "</ul>";
echo "</li>";
echo "<li class=\"divider\"></li>";
} elseif ($row['Count']==0) {
$link="";
if($row['page_id']){
$link=base_url().$row['page_id'];
}
else
{
$link=$row['custom_link'];
}
$title=$row['title'];
if($title=="OPERAS PHYSICIAN LOG IN"){
$title="OPERAS Physicians Log-In";
}
else
{
$title= strtoupper(str_replace('-', ' ', clean($row['title']) ));
}
global $var;//this is where is want to manipulate the variable
$var.=$title;
echo "<script>console.log('".$title."')</script>";
echo "<li><a href='" . $link . "'>" . $title . "</a></li>";
echo "<li class=\"divider\"></li>";
} else;
}
}
function clean($string) {
return preg_replace('/[^A-Za-z0-9\-]/', ' ', $string); // Removes special chars.
}
display_children(0, 1);
echo $var;//returns null
?>
</ul>
现在我查看了许多问题,但他们正在做的是在函数内部使用全局变量,因为我想在函数内部使用全局变量,并且在使用之后使用函数外部的更新值的相同变量。有什么帮助吗?