php foreach函数里面的另一个foreach函数

时间:2014-10-31 07:23:52

标签: php function foreach

<?php
        echo'<table border="1">';
        foreach (RetrieveSpecificSubMenu() as $value){
            echo "<tr>";
            echo'<td colspan="2">';
            echo "Sub Menu Details";
            echo'</td>';
            echo "</tr>";
            echo "<tr>";
            echo'<td>';
            echo "Sub Menu ID:";
            echo'</td>';
            echo'<td>';
            echo "<input readonly type='text' name='submenuid' value=" .$value['menuid']. " class='box required'/>";
            echo'</td>';
            echo "</tr>";
            echo "<tr>";
            echo'<td>';
            echo "Sub Menu Name:";
            echo'</td>';
            echo'<td>';
            echo "<input type='text' name='submenuname' value='" .htmlspecialchars($value['menuname'], ENT_QUOTES). "' class='box required'/>";
            echo'</td>';
            echo "</tr>";
            echo "<tr>";
            echo'<td>';
            echo "Parent Name:";
            echo'</td>';
            echo'<td>';
            echo "<input type='text' name='parentid' value='" .htmlspecialchars($value['parentid'], ENT_QUOTES). "' class='box required'/>";
            echo'<select name="submenuurl">';
                    echo"<option value='" .htmlspecialchars($value['parentid'], ENT_QUOTES). "'>".$value['menuurl']."</option>";
                    foreach (RetrieveAllMenuinDatabase() as $value){
                        echo "<option value='".$value['menuname']."'>".$value['menuname']."</option>";
                    }   
        echo'</select>';
            echo'</td>';
            echo "</tr>";
            echo "<tr>";
            echo'<td>';
            echo "Sub Menu Description:";
            echo'</td>';
            echo'<td>';
            echo "<input type='text' name='submenudescription' value='" .htmlspecialchars($value['menudescription'], ENT_QUOTES). "' class='box required'/>";
            echo'</td>';
            echo "</tr>";
            echo "<tr>";
            echo'<td>';
            echo "Sub Menu(Second Level):";
            echo'</td>';
            echo'<td>';
                if($value['menuflag']==="1"){
                    echo "<input id='remember' class='submenumanagementmenuflag' type='checkbox' name='submenuflag' value='" .$value['menuflag']. "' class='box required' checked='checked'/>"."Yes";
                }else{
                    echo "<input id='remember' class='submenumanagementmenuflag' type='checkbox' name='submenuflag' value='" .$value['menuflag']. "' class='box required' check''/>"."Yes";
                }
            echo'</td>';
            echo "</tr>";
            echo "<tr>";
            echo'<td>';
            echo "Display Order:";
            echo'</td>';
            echo'<td>';
            echo "<input  type='text' name='submenudisplayorder' value=" .$value['menudisplayorder']. " class='box required'/>";
            echo'</td>';
            echo "</tr>";
            echo "<tr class='submenumanagementurl'>";
            echo'<td>';
            echo "Menu Url:";
            echo'</td>';
            echo'<td>';
                echo'<select name="submenuurl">';
                    echo"<option value=" .$value['menuurl']. ">".$value['menuurl']."</option>";
                    foreach (RetrieveAllPages() as $value){
                        echo "<option value='".$value['Filename']."'>".$value['Filename']."</option>";
                    }   
        echo'</select>';
            echo'</td>';
            echo "</tr>";
        }   
        echo'</table>';
    ?>

这是一个有效的代码,它从数据库中获取数据,并向他们展示了一个回声我有一个想法,将另一个函数放在其中

foreach (RetrieveAllMenuinDatabase() as $value){ echo "<option value='".$value['menuname']."'>".$value['menuname']."</option>"; }

将此代码放入代码之后,未找到其余值,为什么会发生这种情况我只想知道为什么这样我就能找到解决方法...任何建议都表示赞赏

FYI内部foreach的输出是问题是foreach之后的值都消失了它得到错误它说它未定义

2 个答案:

答案 0 :(得分:1)

你为什么要和PHP调情?

试试这段代码......

<?php

echo'<table border="1">';
foreach (RetrieveSpecificSubMenu() as $value) {
    echo "<tr>"
    . '<td colspan="2">'
    . "Sub Menu Details"
    . '</td>'
    . "</tr>"
    . "<tr>"
    . '<td>'
    . "Sub Menu ID:"
    . '</td>'
    . '<td>'
    . "<input readonly type='text' name='submenuid' value=" . $value['menuid'] . " class='box required'/>"
    . '</td>'
    . "</tr>"
    . "<tr>"
    . '<td>'
    . "Sub Menu Name:"
    . '</td>'
    . '<td>'
    . "<input type='text' name='submenuname' value='" . htmlspecialchars($value['menuname'], ENT_QUOTES) . "' class='box required'/>"
    . '</td>'
    . "</tr>"
    . "<tr>"
    . '<td>'
    . "Parent Name:"
    . '</td>'
    . '<td>'
    . "<input type='text' name='parentid' value='" . htmlspecialchars($value['parentid'], ENT_QUOTES) . "' class='box required'/>"
    . '<select name="submenuurl">'
    . "<option value='" . htmlspecialchars($value['parentid'], ENT_QUOTES) . "'>" . $value['menuurl'] . "</option>";
            foreach (RetrieveAllMenuinDatabase() as $AllMenu) {
                echo "<option value='" . $AllMenu['menuname'] . "'>" . $AllMenu['menuname'] . "</option>";
            }
    echo '</select>'
    . '</td>'
    . "</tr>"
    . "<tr>"
    . '<td>'
    . "Sub Menu Description:"
    . '</td>'
    . '<td>'
    . "<input type='text' name='submenudescription' value='" . htmlspecialchars($value['menudescription'], ENT_QUOTES) . "' class='box required'/>"
    . '</td>'
    . "</tr>"
    . "<tr>"
    . '<td>'
    . "Sub Menu(Second Level):"
    . '</td>'
    . '<td>';
    if ($value['menuflag'] === "1") {
        echo "<input id='remember' class='submenumanagementmenuflag' type='checkbox' name='submenuflag' value='" . $value['menuflag'] . "' class='box required' checked='checked'/>" . "Yes";
    } else {
        echo "<input id='remember' class='submenumanagementmenuflag' type='checkbox' name='submenuflag' value='" . $value['menuflag'] . "' class='box required' check''/>" . "Yes";
    }
    echo '</td>'
    . "</tr>"
    . "<tr>"
    . '<td>'
    . "Display Order:"
    . '</td>'
    . '<td>'
    . "<input  type='text' name='submenudisplayorder' value=" . $value['menudisplayorder'] . " class='box required'/>"
    . '</td>'
    . "</tr>"
    . "<tr class='submenumanagementurl'>"
    . '<td>'
    . "Menu Url:"
    . '</td>'
    . '<td>'
    . '<select name="submenuurl">'
    . "<option value=" . $value['menuurl'] . ">" . $value['menuurl'] . "</option>";
    foreach (RetrieveAllPages() as $AllPages) {
        echo "<option value='" . $AllPages['Filename'] . "'>" . $AllPages['Filename'] . "</option>";
    }
    echo'</select>'
    . '</td>'
    . "</tr>";
}
echo'</table>';
?>

答案 1 :(得分:0)

在你内心的foreach中,你会覆盖外部foreach的$value变量:)