質問編集履歴
1
回答にある通りに書き直しを行ってみました!エラー文が出てました(-_-;)
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,9 +6,9 @@
|
|
6
6
|
|
7
7
|
### 発生している問題・エラーメッセージ
|
8
8
|
|
9
|
-
|
9
|
+
SyntaxError: Unexpected token < in JSON at position 0
|
10
|
-
|
10
|
+
|
11
|
-
|
11
|
+
…とアラートで表示されます
|
12
12
|
|
13
13
|
|
14
14
|
|
@@ -18,11 +18,101 @@
|
|
18
18
|
|
19
19
|
```PHP
|
20
20
|
|
21
|
+
//json_encode.php
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
//------------------------------------------------------------
|
26
|
+
|
27
|
+
//●改行を消す
|
28
|
+
|
29
|
+
//------------------------------------------------------------
|
30
|
+
|
31
|
+
function indention($str){
|
32
|
+
|
21
|
-
r
|
33
|
+
$str = str_replace("\n","",$str);
|
34
|
+
|
22
|
-
|
35
|
+
$str = str_replace("\r","",$str);
|
36
|
+
|
23
|
-
|
37
|
+
return $str;
|
38
|
+
|
24
|
-
|
39
|
+
}
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
//------------------------------------------------------------
|
44
|
+
|
45
|
+
//●配列の中身に該当するデータがあれば該当する件数を返す。
|
46
|
+
|
47
|
+
//1件以上ある場合「件数」。0件の場合falseを返す。
|
48
|
+
|
49
|
+
//------------------------------------------------------------
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
function searchData($array,$column_num,$serch_ward){
|
54
|
+
|
55
|
+
$hit = 0;
|
56
|
+
|
57
|
+
foreach($array as $value){
|
58
|
+
|
59
|
+
if($value[$column_num] == $serch_ward){
|
60
|
+
|
61
|
+
$hit++;
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
return $hit > 0 ? $hit : false ;
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
//------------------------------------------------------------
|
74
|
+
|
75
|
+
//●CSVファイルを読み込む
|
76
|
+
|
77
|
+
//------------------------------------------------------------
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
function open_csv($pass){
|
82
|
+
|
83
|
+
$fp = fopen($pass,'r');
|
84
|
+
|
85
|
+
$value = fgets($fp);
|
86
|
+
|
87
|
+
$member = [];
|
88
|
+
|
89
|
+
$flag = 0;
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
while($value !== false){
|
94
|
+
|
95
|
+
$flag = 1;
|
96
|
+
|
97
|
+
$member[] = explode(",",$value);
|
98
|
+
|
99
|
+
$value = fgets($fp);
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
fclose($fp);
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
return $member;
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
//※「select_num.txt」には1桁の数字が記述されています。
|
114
|
+
|
25
|
-
$fp = fopen('./data/select_num.txt','r');
|
115
|
+
$fp = fopen('./data/select_num.txt','r');
|
26
116
|
|
27
117
|
$select_num = fgets($fp);
|
28
118
|
|
@@ -40,7 +130,7 @@
|
|
40
130
|
|
41
131
|
$all_question_parent = open_csv('./data/question_parent.csv');
|
42
132
|
|
43
|
-
//該当するデータ
|
133
|
+
//該当する問題データの抽出
|
44
134
|
|
45
135
|
$question_child = get_array_by_search($all_question_child,1,$select_num); //◎第三引数
|
46
136
|
|
@@ -48,43 +138,61 @@
|
|
48
138
|
|
49
139
|
|
50
140
|
|
51
|
-
echo json_encode($question_child);
|
52
|
-
|
53
|
-
echo json_encode($question_parent);
|
141
|
+
echo json_encode([$question_parent,$question_child]);
|
54
142
|
|
55
143
|
```
|
56
144
|
|
57
145
|
|
58
146
|
|
147
|
+
ちなみに以下のような記述ならばajax通信が行われ、コンソールログに値が格納されていました。
|
148
|
+
|
149
|
+
```PHP
|
150
|
+
|
151
|
+
$data = [
|
152
|
+
|
153
|
+
[1,"ふじ山","110","./../movie/quiz_01.mp4","5","./../img/quiz_icon01.png"],
|
154
|
+
|
155
|
+
[2,"あべのハルカス","1111111","./../movie/quiz_02.mp4","10","./../img/quiz_icon02.png"],
|
156
|
+
|
157
|
+
[3,"ほうりゅう寺","111110","./../movie/quiz_03.mp4","15","./../img/quiz_icon03.png"],
|
158
|
+
|
159
|
+
[4,"かんとう平野","111100","./../movie/quiz_04.mp4","20","./../img/quiz_icon04.png"],
|
160
|
+
|
161
|
+
[5,"おきのとり島","111110","./../movie/quiz_05.mp4","40","./../img/quiz_icon05.png"],
|
162
|
+
|
163
|
+
[6,"くろべ峡谷","11100","./../movie/quiz_06.mp4","60","./../img/quiz_icon06.png"],
|
164
|
+
|
165
|
+
[7,"たざわ湖","1110","./../movie/quiz_07.mp4","100","./../img/quiz_icon07.png"]
|
166
|
+
|
167
|
+
];
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
echo json_encode($data);
|
172
|
+
|
173
|
+
```
|
174
|
+
|
175
|
+
|
176
|
+
|
59
177
|
```JavaScript
|
60
178
|
|
61
|
-
$(function()
|
179
|
+
$(function(){
|
62
|
-
|
63
|
-
//非同期通信を行う
|
64
180
|
|
65
181
|
$.ajax({
|
66
182
|
|
183
|
+
type: "POST",
|
184
|
+
|
67
|
-
url: "./json_encode.php",
|
185
|
+
url: "./../php/json_encode.php",
|
68
|
-
|
186
|
+
|
69
|
-
dataType:
|
187
|
+
dataType : "json"
|
70
188
|
|
71
189
|
}).done(function(data){
|
72
190
|
|
73
|
-
if(data.results){
|
74
|
-
|
75
|
-
|
191
|
+
console.log(data);
|
76
|
-
|
77
|
-
|
192
|
+
|
78
|
-
|
79
|
-
else{
|
80
|
-
|
81
|
-
console.log("失敗");
|
82
|
-
|
83
|
-
}
|
84
|
-
|
85
|
-
}).fail(function(
|
193
|
+
}).fail(function(XMLHttpRequest, textStatus, error){
|
86
|
-
|
194
|
+
|
87
|
-
|
195
|
+
alert(error);
|
88
196
|
|
89
197
|
});
|
90
198
|
|
@@ -94,6 +202,14 @@
|
|
94
202
|
|
95
203
|
|
96
204
|
|
205
|
+
### ファイルの階層は以下の通りです…!
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
![イメージ説明](e4b054b74846f50c45c0761396d57561.png)
|
210
|
+
|
211
|
+
|
212
|
+
|
97
213
|
### 試したこと
|
98
214
|
|
99
215
|
|