fork(1) download
  1. <?php
  2. $filename='menu.txt';
  3. if($_POST['add']){ // Если добавляем новый пункт меню
  4. $text=trim($_POST['text']);
  5. $link=trim($_POST['link']);
  6. if(!empty($text) && !empty($link)){
  7. $f=fopen($filename, 'a+');
  8. $row=$text.'#'.$link."\n";
  9. if(!fwrite($f, $row)) $error='Ошибка записи';
  10. fclose($f);
  11. }
  12. header('location:'.$_SERVER['PHP_SELF']);
  13. }
  14. if($_POST['save']){ // если нажали кнопку сохранения редактируемого пункта меню
  15. $file=file($filename);
  16. if(!empty($_POST['newText']) && !empty($_POST['newLink']) && !empty($_GET['edit'])){
  17. $key=$_GET['edit']; // копируем ключ элемента в другую переменную
  18. $row=explode('#',$file[$key]);
  19. $row[0]=$_POST['newText'];
  20. $row[1]=$_POST['newLink'];
  21. $newRow=implode('#', $row);
  22. $newRow.="\n";
  23. $file[$key]=$newRow;
  24. if(!file_put_contents($filename, $file)) $error='При редактировании произошла ошибка';
  25. }
  26. header('location:'.$_SERVER['PHP_SELF']);
  27. }
  28. if(isset($_GET['del'])){ // если нажали значок удаления (ссылку)
  29. $file=file($filename);
  30. $key=$_GET['del'];
  31. unset($file[$key]);
  32. if(!file_put_contents($filename, $file)) $error='При удалении произошла ошибка';
  33. header('location:'.$_SERVER['PHP_SELF']);
  34. }
  35. if($_POST['cancel']) header('location:'.$_SERVER['PHP_SELF']); // если нажали кнопку отменить
  36. // формируем список пунктов меню в виде таблицы
  37. $menu_list='';
  38. if(file_exists($filename)){
  39. $file=file($filename);
  40. $menu_list='<table border><thead><th>Текст</th><th>Ссылка</th><th>Действия</th></thead><tbody>';
  41. foreach ($file as $key => $value) {
  42. $row=explode('#', $value);
  43. $menu_list.='<tr>';
  44. if(isset($_GET['edit']) && $_GET['edit']==$key){
  45. $menu_list.='<form action="'.$_SERVER['PHP_SELF'].'?element='.$key.'" method="post"><td><input name="newText" value="'.$row[0].'"></td><td><input name="newLink" value="'.$row[1].'"></td><td><input type="submit" value="Сохранить" name="save"><input type="submit" value="Отменить" name="cancel"></td></form>';
  46. }
  47. else $menu_list.='<td>'.$row[0].'</td><td>'.$row[1].'</td><td><a href="?edit='.$key.'" title="Править"><img src="http://i...content-available-to-author-only...e.com/icons/oxygen-icons.org/oxygen/16/Actions-document-edit-icon.png"></a>&nbsp;&nbsp;&nbsp;<a href="?del='.$key.'" title="Удалить"><img src="http://i...content-available-to-author-only...e.com/icons/visualpharm/must-have/16/Delete-icon.png"></a></td>';
  48. $menu_list.='</tr>';
  49. }
  50. $menu_list.='</tbody></table>';
  51. }
  52. ?>
  53. <!doctype html>
  54. <html lang="ru">
  55. <head>
  56. <meta charset="UTF-8">
  57. <title>Админка</title>
  58. </head>
  59. <body>
  60. <form action="" method="post">
  61. <fieldset>
  62. <legend>Добавить пункт меню</legend>
  63. <p><label for="text">Текст меню</label><input name="text" id="text" placeholder="Текст меню" type="text"></p>
  64. <p><label for="link">Ссылка</label><input name="link" id="link" placeholder="http://s...content-available-to-author-only...e.ru" type="text"></p>
  65. <input type="submit" value="Сохранить" name="add">
  66. </fieldset>
  67. <?=$error?>
  68. <?=$menu_list?>
  69. </form>
  70. </body>
  71. </html>
Success #stdin #stdout #stderr 0.01s 20520KB
stdin
Standard input is empty
stdout
<!doctype html>
<html lang="ru">
<head>
	<meta charset="UTF-8">
	<title>Админка</title>
</head>
<body>
	<form action="" method="post">
	<fieldset>
		<legend>Добавить пункт меню</legend>
		<p><label for="text">Текст меню</label><input name="text" id="text" placeholder="Текст меню" type="text"></p>
		<p><label for="link">Ссылка</label><input name="link" id="link" placeholder="http://s...content-available-to-author-only...e.ru" type="text"></p>
		<input type="submit" value="Сохранить" name="add">
	</fieldset>
		</form>
</body>
</html>
stderr
PHP Notice:  Undefined index: add in /home/2pXZTZ/prog.php on line 3
PHP Notice:  Undefined index: save in /home/2pXZTZ/prog.php on line 14
PHP Notice:  Undefined index: cancel in /home/2pXZTZ/prog.php on line 35
PHP Notice:  Undefined variable: error in /home/2pXZTZ/prog.php on line 67