Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang ="ja">
  3. <head>
  4. <title>sample</title>
  5. <meta charset="UTF-8">
  6.  
  7. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  8. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-resource.min.js"></script>
  9. <script src="C:xampphtdocsPHPcontroller.js"></script>
  10. </head>
  11.  
  12. <body ng-app = "mainApp" ng-controller="MainCtrl">
  13. <h2></h2>
  14. <div>
  15.  
  16. <table border = "1">
  17. <tr>
  18. <th></th>
  19. <th>名前</th>
  20. <th>年齢</th>
  21. <th>ID</th>
  22. </tr>
  23. <tr ng-controller="DetailCtrl" ng-repeat="employee in employees">
  24. <td>{{employee.id}}</td>
  25. <td><input ng-model="employee.name"></td>
  26. <td><input ng-model="employee.age"></td>
  27. <td><input ng-model="employee.id"></td>
  28. <td><button ng-click="update()">更新</button></td>
  29. <td><button ng-click="delete()">削除</button></td>
  30. </tr>
  31. <tr>
  32. <td>&nbsp;</td>
  33. <td><input ng-model="new_student.name"></td>
  34. <td><input ng-model="new_student.age"></td>
  35. <td><input ng-model="new_student.id"></td>
  36. <td><button ng-click="add()">追加</button></td>
  37. <td>&nbsp;</td>
  38. </tr>
  39. </table>
  40.  
  41. </body>
  42. </html>
  43.  
  44. var mainApp = angular.module("mainApp", ["ngResource"]);
  45.  
  46. mainApp.controller("MainCtrl", function($scope, $resource, $window) {
  47.  
  48.  
  49. //呼び出すPHPを定義
  50. var Employee = $resource('dbController.php', {id: '@id'});
  51. //dbController.phpからsampleテーブルのレコードを$scope.employeesに格納
  52. $scope.employees = Employee.query();
  53. /* $scope.employees = {
  54. info:[
  55. {name:'aaa',age:'21',id:'111'},
  56. {name:'bbb',age:'21',id:'222'},
  57. {name:'ccc',age:'22',id:'333'}
  58. ]
  59. }*/
  60. //追加ボタンが押された時
  61. $scope.add = function() {
  62. //POSTでPHPを呼び出しレコードを挿入
  63. Employee.save($scope.new_employee, function() {
  64. alert("追加しました。");
  65. //画面をリロード
  66. $window.location.reload();
  67. });
  68. };
  69.  
  70. });
  71.  
  72. mainApp.controller('DetailCtrl', function($scope, $window) {
  73. $scope.update = function() {
  74. $scope.employee.$save(function() {
  75. alert("更新しました。");
  76. });
  77. };
  78. $scope.delete = function(index) {
  79. $scope.employee.$delete();
  80. alert("削除しました。");
  81. $window.location.reload();
  82. };
  83. });
  84.  
  85. <?php
  86. //phpinfo();
  87. $host = 'localhost';
  88. $dbname = 'mysql';
  89. $user = 'root';
  90. $pass = 'pass';
  91.  
  92. try{
  93. $mysqli = new mysqli($host,$user,$pass,$dbname);
  94.  
  95. if($mysqli->connect_errno){
  96. printf("Connect faild: %sn",$mysqli->connect_error);
  97. exit();
  98. }
  99.  
  100. switch($_SERVER['REQUEST_METHOD']){
  101. case 'GET':
  102. $sql = "select * from sample";
  103. $result = $mysqli->query($sql);
  104.  
  105. if(mysqli_num_rows($result)){
  106. while($row = mysqli_fetch_assoc($result)){
  107. $arr[] = $row;
  108. }
  109. }
  110.  
  111. echo $json_info = json_encode($arr);
  112. break;
  113. case 'POST':
  114. $in = json_decode(file_get_contents('php://input'),ture);
  115. if(isset($in['id'])){
  116. //if(isset($_GET['id'])) {
  117. $sql = "UPDATE emp SET name = ? , age = ? where id = ?";
  118. $stmt = $mysqli->prepare($sql);
  119. $stmt->bind_param('sss',$name, $age, $id);
  120. $name = isset($_REQUEST['name']) ? $_REQUEST['name'] : 'dummy_name';
  121. $name = isset($_REQUEST['age']) ? $_REQUEST['age'] : 1;
  122. $name = isset($_REQUEST['id']) ? $_REQUEST['id'] : 1;
  123. $stmt->execute();
  124. }else{
  125. $sql = "INSERT INTO sample(name, age) VALUES(?, ?)";
  126. $stmt = $mysqli->prepare($sql);
  127. $stmt->bind_param('ss',$name, $age);
  128. $name = isset($_REQUEST['name']) ? $_REQUEST['name'] : 'dummy_name';
  129. $name = isset($_REQUEST['age']) ? $_REQUEST['age'] : 1;
  130. $stmt->execute();
  131. }
  132.  
  133. break;
  134. case 'DELETE':
  135. $sql = "DELETE FROM sample WHERE id=?";
  136. $stmt = $mysqli->prepare($sql);
  137. $stmt->bind_param('i', $id);
  138. $id = $_GET['id'];
  139. $stmt->execute();
  140. break;
  141. }
  142. }catch(PD0Exception $e){
  143. exit('データベース接続失敗。'.$e->getMessage());
  144. }
  145. ?>
  146.  
  147. $scope.employees = Employee.query();
  148.  
  149. $scope.employees = {
  150. info:[
  151. {name:'aaa',age:'21',id:'111'},
  152. {name:'bbb',age:'21',id:'222'},
  153. {name:'ccc',age:'22',id:'333'}
  154. ]
  155. }
  156.  
  157. var mainApp = angular.module("mainApp", []);
  158.  
  159. var mainApp = angular.module("mainApp", ["ngResource"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement