I don't have a problem posting the code, I just thought it might confuse people rather than enlighten them.
<form method="post" name="menuedit">
<?php
// include data files
$filename = 'menu.data.inc.php';
$filepath = $tbs_datafolder.'/menu/'.$l.'/'.$filename;
if ( !file_exists($filepath) )
$filepath = $filepath.'.bak';
include($filepath);
include('includes/menu.array.inc.php');
?>
<script type="text/javascript">
<!--
function me_img_swap(element_ID,img_ID) {
if ( document.images[img_ID].src == 'http://<?=$_SERVER['SERVER_NAME'];?>/images/plus.gif' ) {
document.images[img_ID].src = 'http://<?=$_SERVER['SERVER_NAME'];?>/images/minus.gif';
document.getElementById(element_ID).style.display = 'block';
} else {
document.images[img_ID].src = 'http://<?=$_SERVER['SERVER_NAME'];?>/images/plus.gif';
document.getElementById(element_ID).style.display = 'none';
}
}
function me_showHide(element_ID,img_ID) {
if (document.getElementById(element_ID).style.display == 'none') {
document.getElementById(element_ID).style.display = 'block';
me_img_swap(element_ID,img_ID);
} else {
document.getElementById(element_ID).style.display = 'none';
me_img_swap(element_ID,img_ID);
}
}
//-->
</script>
<?php
if ( !isset($_POST['page']) || ( isset($_POST['page']) && $_POST['page'] != 'editmenu' ) ) {
echo $tbs_lng['mchinstr'].'<br /><br /><br />'.$rn.$rn;
$input_width = 10;
$tbs_link_keys = array_keys($tbs_link);
$li = 0;
echo '<div id="menu_editing">'.$rn;
$menu = array_keys($menus);
for ( $mi=0; $mi<$mimax; $mi++ ) {
echo ' <div name="me_m'.$mi.'" id="me_m'.$mi.'" style="color:#FFFFFF;background-color:#000000;display:block"> <a href="#" onClick="me_showHide(\'me_s'.$mi.'\',\'me_img_m'.$mi.'\');"><img src="images/plus.gif" border="0" name="me_img_m'.$mi.'" id="me_img_m'.$mi.'" /></a> <b>Menu '.( $mi < 9 ? ' ' : '' ).($mi + 1).'</b> <input size="'.$input_width.'" type="text" name="M[]" value="'.( !empty($_POST['M'][$mi]) ? $_POST['M'][$mi] : stripslashes($menu[$mi]) ).'" /> '.$tbs_lng['fllnk'].pages( $tbs_link_keys[$li], $menu[$mi] ).'<br />'.$rn; $li++;
$sub = array_keys($menus[$menu[$mi]]);
echo ' <div name="me_s'.$mi.'" id="me_s'.$mi.'" style="margin-left:20px;color:#FFFFFF;background-color:#666666;display:none">'.$rn;
for ( $si=0; $si<$simax; $si++ ) {
echo ' <a href="#" onClick="me_showHide(\'me_m'.$mi.'_ss'.$si.'\',\'me_img_m'.$mi.'_s'.$si.'\');"><img src="images/plus.gif" border="0" name="me_img_m'.$mi.'_s'.$si.'" id="me_img_m'.$mi.'_s'.$si.'" /></a> <b>Sub-Menu '.( $si < 9 ? ' ' : '' ).($si + 1).'</b>'.$tbs_lng['of'].'Menu '.($mi + 1).' <input size="'.$input_width.'" type="text" name="M'.$mi.'S[]" value="'.( !empty($_POST['M'.$mi.'S'][$si]) ? $_POST['M'.$mi.'S'][$si] : stripslashes($sub[$si]) ).'" /> '.$tbs_lng['fllnk'].pages( $tbs_link_keys[$li], $sub[$si] ).'<br />'.$rn; $li++;
echo ' <div name="me_m'.$mi.'_ss'.$si.'" id="me_m'.$mi.'_ss'.$si.'" style="margin-left:20px;color:#FFFFFF;background-color:#999999;display:none">'.$rn;
for ( $ssi=0; $ssi<$ssimax; $ssi++ ) {
echo ' <b>Sub-Sub-Menu '.( $ssi < 9 ? ' ' : '' ).($ssi + 1).'</b>'.$tbs_lng['of'].'Sub-Menu '.($si + 1).$tbs_lng['of'].'Menu '.($mi + 1).' <input size="'.$input_width.'" type="text" name="M'.$mi.'S'.$si.'S[]" value="'.( !empty($_POST['M'.$mi.'S'.$si.'S'][$ssi]) ? $_POST['M'.$mi.'S'.$si.'S'][$ssi] : stripslashes($menus[$menu[$mi]][$sub[$si]][$ssi]) ).'" /> '.$tbs_lng['fllnk'].pages( $tbs_link_keys[$li], $menus[$menu[$mi]][$sub[$si]][$ssi] ).'<br />'.$rn; $li++;
}
echo ' </div>'.$rn;
}
echo ' </div>'.$rn;
echo ' </div>'.$rn;
}
echo $rn.'<input type="hidden" name="page" value="editmenu" />'.$rn.'<br /><br /><input type="submit" value="'.$tbs_lng['mchbutton'].'" />'.$rn;
echo '</div>'.$rn;
}
// if the menu has been changed, write the data files!!!
else if ( isset($_POST['page']) && $_POST['page'] == 'editmenu' ) {
// external data file content
$data = '<?'.$rn;
for ( $mi=0; $mi<$mimax; $mi++ ) {
$data .= ' $m'.$mi.' = "'.$_POST['M'][$mi].'"; /* links to */ $tbs_link["m'.$mi.'"] = "'.$_POST['filelink'][str_replace(' ','_',$_POST['M'][$mi])].'";'.$rn;
for ( $si=0; $si<$simax; $si++ ) {
$data .= ' $m'.$mi.'s'.$si.' = "'.$_POST['M'.$mi.'S'][$si].'"; /* links to */ $tbs_link["m'.$mi.'s'.$si.'"] = "'.$_POST['filelink'][str_replace(' ','_',$_POST['M'.$mi.'S'][$si])].'";'.$rn;
for ( $ssi=0; $ssi<$ssimax; $ssi++ ) {
$data .= ' $m'.$mi.'s'.$si.'ss'.$ssi.' = "'.$_POST['M'.$mi.'S'.$si.'S'][$ssi].'"; /* links to */ $tbs_link["m'.$mi.'s'.$si.'ss'.$ssi.'"] = "'.$_POST['filelink'][str_replace(' ','_',$_POST['M'.$mi.'S'.$si.'S'][$ssi])].'";'.$rn;
}
}
}
$data .= '?>'.$rn;
// create external data file
$file = fopen($filepath, 'w+') or die("ERROR: Can't create/edit file. Check permissions!");
fwrite($file, $data);
fclose($file);
// create external data file backup
$file = fopen($filepath.'.bak', 'w+') or die("ERROR: Can't create/edit backup file. Check permissions!");
fwrite($file, $data);
fclose($file);
echo '<span class="approved">'.$tbs_lng['mchok'].'</span>';
}
?>
</form>
The depth of this menu structure is defined in an external file.
depth = 3 would mean that there are 3 menus and for every menu 3 sub-menus and for every one of those 3 sub-menus another 3 sub-sub-menus each... in other words depth=3 would mean 3+9+27 menus = 40 lines or input text fields being sent.
I noticed that if I have menu depth set to 4 or less $POST contains what I expect and the script works, but if menu depth is > 4 then $POST is empty and since that condition isn't met (nothing is sent at all) the $data variable isn't being populated and the file with the new menu isn't being written.
The strange thing is that on one server this script works as expected and on another server it doesn't work...
I hope the code helps and doesn't add confusion