質問編集履歴

1

プログラムを追加

2016/05/23 05:35

投稿

nnahito
nnahito

スコア2004

test CHANGED
File without changes
test CHANGED
@@ -47,3 +47,161 @@
47
47
  お分かりに方がいらっしゃいましたらご教授ください。
48
48
 
49
49
  お願い致します。
50
+
51
+
52
+
53
+
54
+
55
+ # 追記
56
+
57
+
58
+
59
+ JSONを創るPHP
60
+
61
+ ```php
62
+
63
+ <?php
64
+
65
+ // DB操作開始
66
+
67
+ $dbh = new PDO('sqlite:../db/data.db');
68
+
69
+
70
+
71
+ // DBからデータ取得
72
+
73
+ $sth = $dbh->prepare("select * from reservation");
74
+
75
+ $sth->execute( );
76
+
77
+
78
+
79
+ $result = "";
80
+
81
+
82
+
83
+ while( $rows = $sth->fetch() ){
84
+
85
+ $result .= json_encode($rows, /*JSON_PRETTY_PRINT && */JSON_UNESCAPED_UNICODE) .",";
86
+
87
+ }
88
+
89
+
90
+
91
+ //最後の一文字(余分な「,」)を削除
92
+
93
+ $result = substr($result, 0, -1);
94
+
95
+
96
+
97
+ $result = '{' ."". '"reservation":['."". $result ."]}";
98
+
99
+ echo preg_replace("/\s/", "", $result);
100
+
101
+
102
+
103
+ $dbh = null;
104
+
105
+
106
+
107
+ ?>
108
+
109
+ ```
110
+
111
+
112
+
113
+ JSONをPHPから受け取る部分
114
+
115
+ ```html
116
+
117
+ <div id="reservation"></div>
118
+
119
+ <script>
120
+
121
+ $(document).ready(function(){
122
+
123
+ $.ajax({
124
+
125
+ type: "GET",
126
+
127
+ url: "./operation/get_reservation.php",
128
+
129
+ }).done(function(data){
130
+
131
+ var template = Handlebars.compile( $('#reservation-template').html() );
132
+
133
+ $("#reservation").html(template(data));
134
+
135
+ });
136
+
137
+ });
138
+
139
+ ```
140
+
141
+
142
+
143
+
144
+
145
+ Handlebarsの記述
146
+
147
+ ```html
148
+
149
+ <!-- handlebarsの設定(ランキング用) -->
150
+
151
+ <script type="text/x-handlebars-template" id="reservation-template">
152
+
153
+ <table class="reservation">
154
+
155
+ <tr>
156
+
157
+ <td>予約番号</td>
158
+
159
+ <td>予約時間</td>
160
+
161
+ <td>お名前</td>
162
+
163
+ <td>支払方法</td>
164
+
165
+ <td>予約人数</td>
166
+
167
+ <td>備考</td>
168
+
169
+ <td>雨天</td>
170
+
171
+ </tr>
172
+
173
+
174
+
175
+
176
+
177
+ {{#each reservation}}
178
+
179
+ <tr>
180
+
181
+ <td>{{id}}</td>
182
+
183
+ <td>{{hour}}:{{minute}}</td>
184
+
185
+ <td>{{name}}</td>
186
+
187
+ <td>{{pay}}</td>
188
+
189
+ <td>{{num}}</td>
190
+
191
+ <td>{{other}}</td>
192
+
193
+ <td>{{rain}}</td>
194
+
195
+ </tr>
196
+
197
+ {{/each}}
198
+
199
+
200
+
201
+
202
+
203
+ </table>
204
+
205
+ </script>
206
+
207
+ ```