質問編集履歴

4

質問の大幅な簡素化

2015/10/27 12:42

投稿

phphone
phphone

スコア28

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,181 @@
1
+ 質問を要約し、簡潔にするために、質問文を大幅に変更します。
2
+
3
+ 以下のプログラムで、最初に表示される数字と「right」ボタンを押したあとに(リダイレクトされた後に)同じ数字が表示されるようにしたいのです。
4
+
5
+
6
+
7
+ ```php
8
+
9
+ <?php
10
+
11
+ if ($_GET["redirected"] === '1') {
12
+
13
+ $k = $_GET["for_alert"];
14
+
15
+ print $k."さっきの数字と違うことがある";
16
+
17
+ }else{
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+ $c = range(0,3);
26
+
27
+ //{0,1,2,3}
28
+
29
+
30
+
31
+ shuffle($c);
32
+
33
+ //配列を無作為に並び替え
34
+
35
+
36
+
37
+ $k=4*$c[0];
38
+
39
+ //配列cの最初の要素をkとする
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+ print $k."<form method='post' action=''>";
50
+
51
+ if($c[0] == 0){
52
+
53
+ print "<input type='submit' name='right' value='right' />";
54
+
55
+ }else{
56
+
57
+ print "<input type='submit' name='wrong' value='wrong' />";
58
+
59
+ }
60
+
61
+ if($c[1] == 0){
62
+
63
+ print "<input type='submit' name='right' value='right' />";
64
+
65
+ }else{
66
+
67
+ print "<input type='submit' name='wrong' value='wrong' />";
68
+
69
+ }
70
+
71
+ if($c[2] == 0){
72
+
73
+ print "<input type='submit' name='right' value='right' />";
74
+
75
+ }else{
76
+
77
+ print "<input type='submit' name='wrong' value='wrong' />";
78
+
79
+ }
80
+
81
+ if($c[3] == 0){
82
+
83
+ print "<input type='submit' name='right' value='right' />";
84
+
85
+ }else{
86
+
87
+ print "<input type='submit' name='wrong' value='wrong' />";
88
+
89
+ }
90
+
91
+ print "</form>";
92
+
93
+ //formを作り、ボタンを4つ表示。rightひとつにwrongが3つ。当然ボタンの並び方もランダムになる。
94
+
95
+
96
+
97
+
98
+
99
+ if($_POST['right']) {
100
+
101
+ $u_p= "a.php?redirected=1&arrayc=".implode('_',$c)."&for_alert=".$k;
102
+
103
+
104
+
105
+ header("Location: $u_p");
106
+
107
+ exit();
108
+
109
+ }
110
+
111
+ if ($_POST['wrong']) {
112
+
113
+ $u_p= "a.php?arrayc=".implode("_",$c);
114
+
115
+ header("Location: $u_p");
116
+
117
+ }
118
+
119
+
120
+
121
+ }
122
+
123
+
124
+
125
+ ?>
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+ ```
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+ ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
170
+
171
+ 以下過去ログ
172
+
173
+ ---------------------------------
174
+
175
+
176
+
177
+
178
+
1
179
  index.phpで、
2
180
 
3
181
  配列$c(要素はすべて自然数)を用意して、

3

蛇足のコメントアウトの消去

2015/10/27 12:42

投稿

phphone
phphone

スコア28

test CHANGED
File without changes
test CHANGED
@@ -72,37 +72,9 @@
72
72
 
73
73
  $file = "ques.txt";
74
74
 
75
- // ファイルを全て配列に入れる
75
+
76
-
77
- $ret_array = file( $file );
76
+
78
-
79
-
80
-
81
- /*
77
+
82
-
83
- //0行目アラート表示
84
-
85
- print "<script>
86
-
87
- alert('". $ret_array[0] . "');
88
-
89
- </script>";
90
-
91
- */
92
-
93
-
94
-
95
- /*
96
-
97
- //ques総行数確認
98
-
99
- print "<script>
100
-
101
- alert('". count($ret_array) . "');
102
-
103
- </script>";
104
-
105
- */
106
78
 
107
79
 
108
80
 
@@ -122,13 +94,13 @@
122
94
 
123
95
  }
124
96
 
125
-
97
+ //タグの数を取得
126
98
 
127
99
 
128
100
 
129
101
  }
130
102
 
131
- //↑これで、$tagは、「<」の存在する行の総数を示してくれる。
103
+
132
104
 
133
105
 
134
106
 
@@ -144,14 +116,6 @@
144
116
 
145
117
 
146
118
 
147
- /*
148
-
149
- for($a = 1; $a < ($tag / 2); $aaa++){
150
-
151
- */
152
-
153
-
154
-
155
119
 
156
120
 
157
121
 
@@ -396,35 +360,19 @@
396
360
 
397
361
 
398
362
 
399
- $countc = 0;
363
+
400
-
401
- //$count = 0はfor文無限ループエラーをとりあえず黙らせるためだけに仮設置
364
+
402
-
403
-
404
-
405
-
406
-
365
+
366
+
367
+
368
+
407
- }
369
+ }
408
-
409
-
410
-
411
-
412
-
413
- /*
370
+
414
-
415
- $a++;
371
+
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
- }*/
372
+
373
+
374
+
375
+
428
376
 
429
377
  }
430
378
 
@@ -442,7 +390,7 @@
442
390
 
443
391
  $a = $_GET['a'];
444
392
 
445
- //↑aは、cが空になった時は1足されるべきであるので注意
393
+
446
394
 
447
395
 
448
396
 

2

ソースがぐちゃぐちゃなので最終的な目標を乗せさせていただきました。

2015/10/25 14:22

投稿

phphone
phphone

スコア28

test CHANGED
File without changes
test CHANGED
@@ -470,6 +470,54 @@
470
470
 
471
471
  プログラムを作る途中でこの問題に直面したため、著しくぐちゃぐちゃです。
472
472
 
473
+ (プログラムの最終目標は、ques.txtに用意された問題集をランダムに表示し、正解したらその問題を出題範囲から除き、不正解なら後でまたもう一度とく・・・というものです。
474
+
475
+ ランダムといっても、「タグ」の範囲内での並び替えです。
476
+
477
+
478
+
479
+ ques.txt
480
+
481
+
482
+
483
+ 行目|内容
484
+
485
+
486
+
487
+ 0|<単元1>問題1
488
+
489
+ 1|選択肢1\t選択肢2\t選択肢3\t選択肢4
490
+
491
+ 2| コメント行(4n行目が問題名、および単元名を指定する場所。4n+1行目では、選択肢を4択で。但し、1番最初の選択肢が正解)
492
+
493
+ 3|解説文
494
+
495
+ 4|問題2
496
+
497
+ 5|選択肢1\t選択肢2\t選択肢3\t選択肢4
498
+
499
+ 6|コメント行(タグというのは<>のようなもので、<>の中身は単元名。これを単元の開始タグとし、終了タグは</end>。開始タグは4n行目一番左に、終了タグは4m+3行目の一番右に記述)
500
+
501
+ 7|解説文</end>
502
+
503
+ 8|<単元2>問題3
504
+
505
+ 9|選択肢1\t選択肢2\t選択肢3\t選択肢4
506
+
507
+ 10|コメント(各問題は、必ず1つの単元に属さなければいけない。そうでないと、ただでさえごちゃごちゃなのに、さらにif分岐をつくって複雑なプログラムになる)
508
+
509
+ 11|解説文<end>
510
+
511
+
512
+
513
+ ques.txt 終
514
+
515
+
516
+
517
+ この例では、単元1である問題1と問題2を、何らかの順番で解き、全問正解できるまで単元1をとき、その後単元2に移り、問題3を正解できるまで解くということになります。
518
+
519
+ )
520
+
473
521
 
474
522
 
475
523
  現時点で、printした$fと、headerの、"・・・&for_alert=".$f の$fが一致しないので、一致させたいです。よろしくお願いいたします。

1

ソースコードを「----------」以降に追加します

2015/10/25 14:16

投稿

phphone
phphone

スコア28

test CHANGED
File without changes
test CHANGED
@@ -35,3 +35,441 @@
35
35
 
36
36
 
37
37
  どうすれば、print $kと、urlパラメータの$kを同じにできるでしょうか?
38
+
39
+
40
+
41
+ ----------
42
+
43
+ 以下、ソースコードの追加です
44
+
45
+
46
+
47
+
48
+
49
+ ```php
50
+
51
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+ <?php
60
+
61
+
62
+
63
+ if(isset($_GET['a']) === FALSE ) {
64
+
65
+ $a = 1;
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+ $file = "ques.txt";
74
+
75
+ // ファイルを全て配列に入れる
76
+
77
+ $ret_array = file( $file );
78
+
79
+
80
+
81
+ /*
82
+
83
+ //0行目アラート表示
84
+
85
+ print "<script>
86
+
87
+ alert('". $ret_array[0] . "');
88
+
89
+ </script>";
90
+
91
+ */
92
+
93
+
94
+
95
+ /*
96
+
97
+ //ques総行数確認
98
+
99
+ print "<script>
100
+
101
+ alert('". count($ret_array) . "');
102
+
103
+ </script>";
104
+
105
+ */
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+ $tag = 0;
116
+
117
+ for($i = 0; $i <= count($ret_array) - 1; $i++){
118
+
119
+ if (strstr($ret_array[$i], '<')) {
120
+
121
+ $tag = $tag + 1;
122
+
123
+ }
124
+
125
+
126
+
127
+
128
+
129
+ }
130
+
131
+ //↑これで、$tagは、「<」の存在する行の総数を示してくれる。
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+ if ($tag > 0){
144
+
145
+
146
+
147
+ /*
148
+
149
+ for($a = 1; $a < ($tag / 2); $aaa++){
150
+
151
+ */
152
+
153
+
154
+
155
+
156
+
157
+
158
+
159
+ for($i = 0; $j <= 2*($a-1); $i++){
160
+
161
+ if (strstr($ret_array[$i], '<')) {
162
+
163
+ $j++;
164
+
165
+ }
166
+
167
+ }
168
+
169
+ //$i-1は、a番目のタグの開始行
170
+
171
+
172
+
173
+ $j=0;
174
+
175
+ for($h = 0; $j <= 2*($a); $h++){
176
+
177
+ if (strstr($ret_array[$h], '<')) {
178
+
179
+ $j++;
180
+
181
+ }
182
+
183
+ }
184
+
185
+ //$h-2は、a番目のタグの終了行
186
+
187
+
188
+
189
+ $b = (($h-2+1)-($i-1))/4;
190
+
191
+ //$bは、a番目のタグの問題数
192
+
193
+ print $b;
194
+
195
+ //bの取得完了
196
+
197
+ $c = (range(1, $b));
198
+
199
+
200
+
201
+ //$i-1行目(a番目のタグの開始行)の第二引数はTか?
202
+
203
+ //→$i-1行目に ,T または ,t があるかない時に、シャッフルする
204
+
205
+
206
+
207
+
208
+
209
+ if ( strstr($ret_array[$i-1], ',T') or strstr($ret_array[$i-1], ',t') ) {
210
+
211
+
212
+
213
+ } else {
214
+
215
+ shuffle($c);
216
+
217
+ shuffle($c);
218
+
219
+ shuffle($c);
220
+
221
+ shuffle($c);
222
+
223
+ shuffle($c);
224
+
225
+ shuffle($c);
226
+
227
+ shuffle($c);
228
+
229
+ }
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+ print_r ($c);
242
+
243
+ print "<br>";
244
+
245
+
246
+
247
+
248
+
249
+ $countc = count($c);
250
+
251
+ for($d = 0; $countc >= 1 ; $d++){
252
+
253
+
254
+
255
+ $e = $c[$d];
256
+
257
+ //$eは、配列cのd番目の数字
258
+
259
+ $f = ($i-1)+4*($e-1);
260
+
261
+
262
+
263
+
264
+
265
+ print "$f<br><br>";
266
+
267
+
268
+
269
+
270
+
271
+
272
+
273
+ if(strpos($ret_array[$f],'>') !== false){
274
+
275
+ $quest = strstr($ret_array[$f], ">");
276
+
277
+ $quest = str_replace(">","",$quest);
278
+
279
+ }else{
280
+
281
+ $quest = str_replace(">","",$ret_array[$f]);
282
+
283
+ }
284
+
285
+
286
+
287
+ $tan_name = strstr($ret_array[$i-1],'>',true);
288
+
289
+ $tan_name = str_replace(",t","",$tan_name);
290
+
291
+ $tan_name = str_replace(",T","",$tan_name);
292
+
293
+ print "<strong><p class='tan'>".$tan_name."&gt;"."</p><p class='question'>".$quest."</p></strong>";
294
+
295
+
296
+
297
+ $choice_array = explode("\t",$ret_array[$f+1]);
298
+
299
+ $one_to_four = (range(0, 3));
300
+
301
+ shuffle($one_to_four);
302
+
303
+
304
+
305
+ print "<form method='post' action=''>";
306
+
307
+
308
+
309
+ if($one_to_four[0] == 0){
310
+
311
+ print "<input type='submit' name='right' value='" .$choice_array[0]. "' />";
312
+
313
+ }else{
314
+
315
+ print "<input type='submit' name='wrong' value='" .$choice_array[$one_to_four[0] ]. "' />";
316
+
317
+ }
318
+
319
+
320
+
321
+ if($one_to_four[1] == 0){
322
+
323
+ print "<input type='submit' name='right' value='" .$choice_array[0]. "' />";
324
+
325
+ }else{
326
+
327
+ print "<input type='submit' name='wrong' value='" .$choice_array[$one_to_four[1] ]. "' />";
328
+
329
+ }
330
+
331
+
332
+
333
+ if($one_to_four[2] == 0){
334
+
335
+ print "<input type='submit' name='right' value='" .$choice_array[0]. "' />";
336
+
337
+ }else{
338
+
339
+ print "<input type='submit' name='wrong' value='" .$choice_array[$one_to_four[2] ]. "' />";
340
+
341
+ }
342
+
343
+
344
+
345
+ if($one_to_four[3] == 0){
346
+
347
+ print "<input type='submit' name='right' value='" .$choice_array[0]. "' />";
348
+
349
+ }else{
350
+
351
+ print "<input type='submit' name='wrong' value='" .$choice_array[$one_to_four[3] ]. "' />";
352
+
353
+ }
354
+
355
+
356
+
357
+ print "</form>";
358
+
359
+ print "$f<br><br>";
360
+
361
+ if ($_POST['right']) {
362
+
363
+
364
+
365
+
366
+
367
+ unset($c[$d]);
368
+
369
+
370
+
371
+ $f = ($i-1)+4*($e-1);
372
+
373
+ $u_p= "index.php?arrayc=".implode('_',$c)."&d=".$d."&a=".$a."&for_alert=".$f
374
+
375
+
376
+
377
+ header("Location: $u_p");
378
+
379
+ }
380
+
381
+
382
+
383
+ if ($_POST['wrong']) {
384
+
385
+
386
+
387
+
388
+
389
+
390
+
391
+ $u_p= "index.php?arrayc=".implode("_",$c)."&d=".$d."&a=".$a;
392
+
393
+ header("Location: $u_p");
394
+
395
+ }
396
+
397
+
398
+
399
+ $countc = 0;
400
+
401
+ //$count = 0はfor文無限ループエラーをとりあえず黙らせるためだけに仮設置
402
+
403
+
404
+
405
+
406
+
407
+ }
408
+
409
+
410
+
411
+
412
+
413
+ /*
414
+
415
+ $a++;
416
+
417
+
418
+
419
+
420
+
421
+
422
+
423
+
424
+
425
+
426
+
427
+ }*/
428
+
429
+ }
430
+
431
+
432
+
433
+ }//以上、パラメータが存在しないとき(1問目)、以下、2問目以降
434
+
435
+ else
436
+
437
+ {
438
+
439
+
440
+
441
+
442
+
443
+ $a = $_GET['a'];
444
+
445
+ //↑aは、cが空になった時は1足されるべきであるので注意
446
+
447
+
448
+
449
+ $d = $_GET['d']+1;
450
+
451
+
452
+
453
+ $c = explode("_",$_GET[arrayc]);
454
+
455
+ print_r ($c);
456
+
457
+
458
+
459
+ }
460
+
461
+
462
+
463
+
464
+
465
+ ?>
466
+
467
+ ```
468
+
469
+ あと、問題となっているのは$kでなく、$fでした。
470
+
471
+ プログラムを作る途中でこの問題に直面したため、著しくぐちゃぐちゃです。
472
+
473
+
474
+
475
+ 現時点で、printした$fと、headerの、"・・・&for_alert=".$f の$fが一致しないので、一致させたいです。よろしくお願いいたします。