Advertisement
Guest User

Untitled

a guest
Apr 8th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. <?php
  2. $host = "localhost";
  3. $user = "root";
  4. $pass = "";
  5. $database_name = "simple_app";
  6. $dsn = "mysql:host=$host;dbname=$database_name";
  7. $db = new PDO($dsn, $user, $pass);
  8.  
  9. class DB{
  10. public static function query($sql, $arr){
  11. global $db;
  12. $sth = $db->prepare($sql);
  13. if($sth->execute($arr)){
  14. return 1;
  15. }
  16. return 0;
  17. }
  18. }
  19.  
  20. if ( isset($_POST['action']) ) {
  21. if ( $_POST['action'] == 'add' ) {
  22. $sql = "INSERT INTO students (second_name, first_name) VALUES (:SN, :FN)";
  23. $arr = array(
  24. ":SN" => $_POST['second_name'],
  25. ":FN" => $_POST['first_name'],
  26. );
  27. DB::query($sql, $arr);
  28. } elseif ( $_POST['action'] == 'delete' ) {
  29. $sql = "DELETE FROM students WHERE id = :ID";
  30. $arr = array(
  31. ":ID" => $_POST['id']
  32. );
  33. DB::query($sql, $arr);
  34. } elseif ( $_POST['action'] == 'update' ) {
  35. $sql = "UPDATE students SET second_name = :SN, first_name = :FN WHERE id = :ID";
  36. $arr = array(
  37. ":SN" => $_POST['second_name'],
  38. ":FN" => $_POST['first_name'],
  39. ":ID" => $_POST['id']
  40. );
  41. DB::query($sql, $arr);
  42. }
  43. }
  44. ?>
  45.  
  46. <!DOCTYPE html>
  47. <html>
  48. <head>
  49. <meta charset="utf-8">
  50. <title>Список учеников</title>
  51.  
  52. <style type="text/css">
  53. .wrapper{
  54. width: 800px;
  55. margin: 0 auto;
  56. }
  57. table {
  58. width: 100%;
  59. border: 2px solid #cccccc;
  60. border-radius: 3px;
  61. -webkit-border-radius: 3px;
  62. -moz-border-radius: 3px;
  63. -khtml-border-radius: 3px;
  64. border-collapse: collapse;
  65. }
  66. th {
  67. text-align: center;
  68. background: #cccccc;
  69. padding: 5px;
  70. border: 1px solid #cccccc;
  71. }
  72. td {
  73. text-align: center;
  74. padding: 5px;
  75. border: 1px solid #cccccc;
  76. }
  77. .update_form{
  78. display: none;
  79. }
  80. input[type="text"] {
  81. margin:5px;
  82. border: 1px solid #cccccc;
  83. border-radius: 3px;
  84. -webkit-border-radius: 3px;
  85. -moz-border-radius: 3px;
  86. -khtml-border-radius: 3px;
  87. background: #ffffff !important;
  88. outline: none;
  89. height: 30px;
  90. width: 240px;
  91. color: rgb(0, 0, 0);
  92. padding: 2px;
  93. font-size: 14px;
  94. transition:.4s;
  95. }
  96. input[type="text"]:focus {
  97. color: #000000;
  98. border: 1px solid #000000;
  99. transition:.4s;
  100. }
  101. input[type="submit"], input[type="button"]{
  102. margin:5px;
  103. border: 1px solid #cccccc;
  104. border-radius: 3px;
  105. -webkit-border-radius: 3px;
  106. -moz-border-radius: 3px;
  107. -khtml-border-radius: 3px;
  108. background: #ffffff;
  109. outline: none;
  110. height: 30px;
  111. width: 240px;
  112. color: rgb(0, 0, 0);
  113. padding: 2px;
  114. font-size: 14px;
  115. transition:.4s;
  116. }
  117. input[type="submit"]:hover{
  118. background: #cccccc;
  119. }
  120. input[type="button"]:hover{
  121. background: #cccccc;
  122. }
  123. .update_form{
  124. text-align:center
  125. margin: 5px;
  126.  
  127. }
  128. h1{
  129. width:100%;
  130. background: #cccccc;
  131. padding-top: 20px;
  132. padding-bottom: 20px;
  133. text-align: center;
  134. font-size: 24px;
  135. border-radius: 3px;
  136. -webkit-border-radius: 3px;
  137. -moz-border-radius: 3px;
  138. -khtml-border-radius: 3px;
  139. }
  140. </style>
  141. </head>
  142. <body>
  143. <div class="wrapper">
  144.  
  145. <h1>Список учащихся</h1>
  146. <?php
  147. $sql = "SELECT * FROM students WHERE 1";
  148. $statement = $db->query($sql);
  149. if($statement){
  150. $people = $statement->fetchAll();
  151. }
  152.  
  153. ?>
  154. <table>
  155. <tr>
  156. <th>№ п/п</th>
  157. <th>Фамилия</th>
  158. <th>Имя</th>
  159. <th>Действия</th>
  160. </tr>
  161. <?php
  162. for ($i = 1; $i <= count($people); $i++) {
  163. $item = $people[$i-1];
  164. ?>
  165. <tr>
  166. <td><?php echo $i; ?></td>
  167. <td><?php echo $item['second_name']; ?></td>
  168. <td><?php echo $item['first_name']; ?></td>
  169. <td>
  170. <form method="POST">
  171. <input type="hidden" name="id" value="<?php echo $item['id']; ?>">
  172. <input type="hidden" name="action" value="delete" />
  173. <input type="submit" value="Удалить" />
  174. </form>
  175. <input type = 'button' value = 'Изменить' class = 'show_edit'>
  176. </td>
  177. </tr>
  178.  
  179. <tr class="update_form">
  180. <td colspan="4">
  181. <h3>Редактирование</h3>
  182. <form method="POST">
  183. <div>
  184. Фамилия:
  185. <input type="text" name="second_name" value = '<?php echo $item['second_name']; ?>'/>
  186. </div>
  187. <div>
  188. Имя:
  189. <input type="text" name="first_name" value = '<?php echo $item['first_name']; ?>'/>
  190. </div>
  191. <input type="hidden" name="action" value="update" />
  192. <input type="hidden" name="id" value="<?php echo $item['id']; ?>">
  193. <input type="submit" value="Сохранить" />
  194. <input type="button" value="Отмена" class = 'hide_edit_block'/>
  195. </form>
  196. </td>
  197. </tr>
  198.  
  199. <?php
  200. }
  201. ?>
  202. </table>
  203.  
  204.  
  205. <h1>Добавление нового ученика</h1>
  206.  
  207. <form method="POST">
  208. <div>
  209. Фамилия
  210. <input type="text" name="second_name"/>
  211. </div>
  212. <div>
  213. Имя
  214. <input type="text" name="first_name"/>
  215. </div>
  216. <input type="hidden" name="action" value="add"/>
  217. <input type="submit" value="Сохранить" />
  218. </form>
  219. </div>
  220. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  221. <script>
  222. jQuery(function($){
  223. $('.show_edit').click(function(){
  224. var work_with = $(this).parent().parent().next();
  225. if (work_with.css('display') == 'none'){
  226. work_with.css('display', 'inline');
  227. }
  228. else {
  229. work_with.css('display', 'none');
  230. }
  231. });
  232. $('.hide_edit_block').click(function(){
  233. var work_with = $(this).parent().parent().parent();
  234. work_with.css('display', 'none');
  235. });
  236. });
  237. </script>
  238. </body>
  239. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement