teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

API側のコードを追記

2017/11/15 08:42

投稿

Yashichi
Yashichi

スコア35

title CHANGED
File without changes
body CHANGED
@@ -32,6 +32,7 @@
32
32
  他にも、一度選んだタスクが最優先(?)になってしまって、別のタスクを選んでも最初に選んだタスクの内容から切り替わらないという現象が起きています。
33
33
 
34
34
  ###該当のソースコード
35
+ 表画面
35
36
  ```JavaScript
36
37
  // 編集したいところをダブルクリックしたらイベント発動
37
38
  $(document).on('dblclick', ".labeling", function(){
@@ -69,7 +70,36 @@
69
70
  });
70
71
 
71
72
  ```
73
+ API側(read.php)
74
+ ```PHP
75
+ <?php
76
+ // header関数より前にechoなどを使ってはいけない
77
+ header('content-type: application/json; charset=utf-8');
78
+ // データベースに接続
79
+ $dsn = 'mysql:host=localhost;dbname=todolist;charset=utf8';
80
+ $user = 'secret';
81
+ $passwd = 'secret';
72
82
 
83
+ try {
84
+ $db = new PDO($dsn, $user, $passwd);
85
+ $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
86
+
87
+ // プリペアドステートメント発行(最新の10件を取得)
88
+ $statement = $db->prepare("
89
+ SELECT * FROM list ORDER BY id DESC LIMIT 10;
90
+ ");
91
+ // print_r($db->errorInfo());
92
+
93
+ $statement->execute();
94
+ $rows = $statement->fetchAll();
95
+ // var_dump($rows);
96
+ echo json_encode(compact('rows'));
97
+
98
+ } catch (PDOException $e) {
99
+ echo "Error :" . $e->getMessage();
100
+ }
101
+ ```
102
+
73
103
  ###試したこと
74
104
  SELECT文でデータベースの中身を取ってくる処理を書いたPHPへアクセスするAjaxの式を書き、
75
105
  返ってきた値`response.rows`の長さ分だけforでループを回します。

1

console.logの結果の追記

2017/11/15 08:41

投稿

Yashichi
Yashichi

スコア35

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,23 @@
1
+ ###追記
2
+ `console.log(response);`の値を追記しておきます。
3
+ ```
4
+ {rows: Array(10)}
5
+ rows:Array(10)
6
+ 0:{0: "217", 1: "test", 2: "rest", 3: null, 4: null, id: "217", title: "test", content: "rest", complement: null, deadline: null}
7
+ 1:{0: "216", 1: "", 2: "", 3: null, 4: null, id: "216", title: "", content: "", complement: null, deadline: null}
8
+ 2:{0: "215", 1: "", 2: "", 3: null, 4: null, id: "215", title: "", content: "", complement: null, deadline: null}
9
+ 3:{0: "214", 1: "sentence", 2: "root", 3: null, 4: null, id: "214", title: "sentence", content: "root", complement: null, deadline: null}
10
+ 4:{0: "213", 1: "clear", 2: "content", 3: null, 4: null, id: "213", title: "clear", content: "content", complement: null, deadline: null}
11
+ 5:{0: "212", 1: "", 2: "", 3: null, 4: null, id: "212", title: "", content: "", complement: null, deadline: null}
12
+ 6:{0: "211", 1: "", 2: "", 3: null, 4: null, id: "211", title: "", content: "", complement: null, deadline: null}
13
+ 7:{0: "210", 1: "", 2: "", 3: null, 4: null, id: "210", title: "", content: "", complement: null, deadline: null}
14
+ 8:{0: "209", 1: "", 2: "", 3: null, 4: null, id: "209", title: "", content: "", complement: null, deadline: null}
15
+ 9:{0: "208", 1: "", 2: "", 3: null, 4: null, id: "208", title: "", content: "", complement: null, deadline: null}
16
+ length:10
17
+ __proto__:Array(0)
18
+ __proto__:Object
19
+ ```
20
+
1
21
  ###前提・実現したいこと
2
22
  PHPとjQueryを利用してAjaxでのToDoリストを作成しています。
3
23