質問を要約し、簡潔にするために、質問文を大幅に変更します。
以下のプログラムで、最初に表示される数字と「right」ボタンを押したあとに(リダイレクトされた後に)同じ数字が表示されるようにしたいのです。
php
1<?php 2if ($_GET["redirected"] === '1') { 3 $k = $_GET["for_alert"]; 4 print $k."さっきの数字と違うことがある"; 5}else{ 6 7 8 9 $c = range(0,3); 10 //{0,1,2,3} 11 12 shuffle($c); 13 //配列を無作為に並び替え 14 15 $k=4*$c[0]; 16 //配列cの最初の要素をkとする 17 18 19 20 21 print $k."<form method='post' action=''>"; 22 if($c[0] == 0){ 23 print "<input type='submit' name='right' value='right' />"; 24 }else{ 25 print "<input type='submit' name='wrong' value='wrong' />"; 26 } 27 if($c[1] == 0){ 28 print "<input type='submit' name='right' value='right' />"; 29 }else{ 30 print "<input type='submit' name='wrong' value='wrong' />"; 31 } 32 if($c[2] == 0){ 33 print "<input type='submit' name='right' value='right' />"; 34 }else{ 35 print "<input type='submit' name='wrong' value='wrong' />"; 36 } 37 if($c[3] == 0){ 38 print "<input type='submit' name='right' value='right' />"; 39 }else{ 40 print "<input type='submit' name='wrong' value='wrong' />"; 41 } 42 print "</form>"; 43 //formを作り、ボタンを4つ表示。rightひとつにwrongが3つ。当然ボタンの並び方もランダムになる。 44 45 46 if($_POST['right']) { 47 $u_p= "a.php?redirected=1&arrayc=".implode('_',$c)."&for_alert=".$k; 48 49 header("Location: $u_p"); 50 exit(); 51 } 52 if ($_POST['wrong']) { 53 $u_p= "a.php?arrayc=".implode("_",$c); 54 header("Location: $u_p"); 55 } 56 57} 58 59?> 60 61 62 63
ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
以下過去ログ
index.phpで、
配列$c(要素はすべて自然数)を用意して、
shuffle($c);
しました。
その後、
$k = $c[0]
としました。
そして、
print "ページ番号$kに移動します。"
と書き、OKボタンが押されたときに、header()を使って、
index.php?k= $k
に移動するというプログラムを組みました。
するとなぜか、print $kによる $kの数字と、移動先のurlについたパラメータの$kの数字が異なってしまいます。
formを使ってはいますが、methodはPOSTにしていて、actionは""にしてあります。
どうすれば、print $kと、urlパラメータの$kを同じにできるでしょうか?
以下、ソースコードの追加です
php
1<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 2 3 4 5<?php 6 7if(isset($_GET['a']) === FALSE ) { 8$a = 1; 9 10 11 12$file = "ques.txt"; 13 14 15 16 17 18 19$tag = 0; 20for($i = 0; $i <= count($ret_array) - 1; $i++){ 21if (strstr($ret_array[$i], '<')) { 22 $tag = $tag + 1; 23} 24//タグの数を取得 25 26} 27 28 29 30 31 32 33if ($tag > 0){ 34 35 36 37for($i = 0; $j <= 2*($a-1); $i++){ 38if (strstr($ret_array[$i], '<')) { 39 $j++; 40} 41} 42//$i-1は、a番目のタグの開始行 43 44$j=0; 45for($h = 0; $j <= 2*($a); $h++){ 46if (strstr($ret_array[$h], '<')) { 47 $j++; 48} 49} 50//$h-2は、a番目のタグの終了行 51 52$b = (($h-2+1)-($i-1))/4; 53//$bは、a番目のタグの問題数 54print $b; 55//bの取得完了 56$c = (range(1, $b)); 57 58//$i-1行目(a番目のタグの開始行)の第二引数はTか? 59//→$i-1行目に ,T または ,t があるかない時に、シャッフルする 60 61 62 if ( strstr($ret_array[$i-1], ',T') or strstr($ret_array[$i-1], ',t') ) { 63 64 } else { 65shuffle($c); 66shuffle($c); 67shuffle($c); 68shuffle($c); 69shuffle($c); 70shuffle($c); 71shuffle($c); 72 } 73 74 75 76 77 78print_r ($c); 79print "<br>"; 80 81 82$countc = count($c); 83for($d = 0; $countc >= 1 ; $d++){ 84 85$e = $c[$d]; 86//$eは、配列cのd番目の数字 87$f = ($i-1)+4*($e-1); 88 89 90print "$f<br><br>"; 91 92 93 94if(strpos($ret_array[$f],'>') !== false){ 95$quest = strstr($ret_array[$f], ">"); 96$quest = str_replace(">","",$quest); 97}else{ 98$quest = str_replace(">","",$ret_array[$f]); 99} 100 101$tan_name = strstr($ret_array[$i-1],'>',true); 102$tan_name = str_replace(",t","",$tan_name); 103$tan_name = str_replace(",T","",$tan_name); 104print "<strong><p class='tan'>".$tan_name.">"."</p><p class='question'>".$quest."</p></strong>"; 105 106$choice_array = explode("\t",$ret_array[$f+1]); 107$one_to_four = (range(0, 3)); 108shuffle($one_to_four); 109 110print "<form method='post' action=''>"; 111 112if($one_to_four[0] == 0){ 113print "<input type='submit' name='right' value='" .$choice_array[0]. "' />"; 114}else{ 115print "<input type='submit' name='wrong' value='" .$choice_array[$one_to_four[0] ]. "' />"; 116} 117 118if($one_to_four[1] == 0){ 119print "<input type='submit' name='right' value='" .$choice_array[0]. "' />"; 120}else{ 121print "<input type='submit' name='wrong' value='" .$choice_array[$one_to_four[1] ]. "' />"; 122} 123 124if($one_to_four[2] == 0){ 125print "<input type='submit' name='right' value='" .$choice_array[0]. "' />"; 126}else{ 127print "<input type='submit' name='wrong' value='" .$choice_array[$one_to_four[2] ]. "' />"; 128} 129 130if($one_to_four[3] == 0){ 131print "<input type='submit' name='right' value='" .$choice_array[0]. "' />"; 132}else{ 133print "<input type='submit' name='wrong' value='" .$choice_array[$one_to_four[3] ]. "' />"; 134} 135 136print "</form>"; 137print "$f<br><br>"; 138if ($_POST['right']) { 139 140 141unset($c[$d]); 142 143$f = ($i-1)+4*($e-1); 144$u_p= "index.php?arrayc=".implode('_',$c)."&d=".$d."&a=".$a."&for_alert=".$f 145 146header("Location: $u_p"); 147} 148 149if ($_POST['wrong']) { 150 151 152 153$u_p= "index.php?arrayc=".implode("_",$c)."&d=".$d."&a=".$a; 154header("Location: $u_p"); 155} 156 157 158 159 160} 161 162 163 164} 165 166}//以上、パラメータが存在しないとき(1問目)、以下、2問目以降 167else 168{ 169 170 171$a = $_GET['a']; 172 173 174$d = $_GET['d']+1; 175 176$c = explode("_",$_GET[arrayc]); 177print_r ($c); 178 179} 180 181 182?>
あと、問題となっているのは$kでなく、$fでした。
プログラムを作る途中でこの問題に直面したため、著しくぐちゃぐちゃです。
(プログラムの最終目標は、ques.txtに用意された問題集をランダムに表示し、正解したらその問題を出題範囲から除き、不正解なら後でまたもう一度とく・・・というものです。
ランダムといっても、「タグ」の範囲内での並び替えです。
例
ques.txt
行目|内容
0|<単元1>問題1
1|選択肢1\t選択肢2\t選択肢3\t選択肢4
2| コメント行(4n行目が問題名、および単元名を指定する場所。4n+1行目では、選択肢を4択で。但し、1番最初の選択肢が正解)
3|解説文
4|問題2
5|選択肢1\t選択肢2\t選択肢3\t選択肢4
6|コメント行(タグというのは<>のようなもので、<>の中身は単元名。これを単元の開始タグとし、終了タグは</end>。開始タグは4n行目一番左に、終了タグは4m+3行目の一番右に記述)
7|解説文</end>
8|<単元2>問題3
9|選択肢1\t選択肢2\t選択肢3\t選択肢4
10|コメント(各問題は、必ず1つの単元に属さなければいけない。そうでないと、ただでさえごちゃごちゃなのに、さらにif分岐をつくって複雑なプログラムになる)
11|解説文<end>
ques.txt 終
この例では、単元1である問題1と問題2を、何らかの順番で解き、全問正解できるまで単元1をとき、その後単元2に移り、問題3を正解できるまで解くということになります。
)
現時点で、printした$fと、headerの、"・・・&for_alert=".$f の$fが一致しないので、一致させたいです。よろしくお願いいたします。