Ver Mensaje Individual
  #1 (permalink)  
Antiguo 12/12/2007, 05:47
Cartilago
 
Fecha de Ingreso: noviembre-2007
Mensajes: 8
Antigüedad: 16 años, 5 meses
Puntos: 0
Pregunta arrays en php

Hola,

Genero un test de preguntas aleatorias y al generarlo voy cargando en un array las preguntas ($pA), en otro las respuestas correctas ($rA) y en otro el número de test ($tA). Lo que pasa es que cuando voy a corregir el test a estos arrays no puedo acceder. El código es el siguiente:


<?php
//////////////////////////////////////////////////

if ($_POST[correxir]==0){
$host = "localhost";
$usuario = "banban2";
$password = "banban2";
$NomeDB = "autoescola_db";
$NomeTaboa = "test_pregunta";
$NomeTaboaP = "pregunta";
$volta = 10; //Número de tests (10 tests=30 preguntas)
$opc = a; // Identificador de cada un dos 30 radio button do test.
$rA=array(); //Array que conterá as respostas das preguntas aleatorias.
$tA=array(); //Array que conterá o número de test de cada pregunta aleatoria.
$pA=array(); //Array que conterá o número de cada pregunta aleatoria.
$n = 1; // Variable para numerar as preguntas aleatorias.

$enlace = mysql_connect($host,$usuario,$password);
@mysql_select_db("$NomeDB");

while ($volta > 0){
$t = rand(1,2);
$sqlquery = "SELECT * FROM $NomeTaboa WHERE id_test = $t ORDER BY RAND()";// Selecciona un test aleatorio

$result = mysql_query($sqlquery);
$number = mysql_num_rows($result);

if ($number < 1) {
echo "<CENTER><P>Non existen resultados para esta búsqueda</CENTER>";
}
else {
$i = 0;
$number = 3; // Selecciona 3 preguntas de cada test
while ($number > $i) {
$idtest = mysql_result($result,$i,"id_test");
$idpregunta = mysql_result($result,$i,"id_pregunta");

$enlace = mysql_connect($host,$usuario,$password);
@mysql_select_db("$NomeDB");
$sqlqueryP = "SELECT * FROM $NomeTaboaP WHERE id_pregunta = '$idpregunta'";
$resultP = mysql_query($sqlqueryP);
$numberP = mysql_num_rows($resultP);
$j = 0;

while ($numberP > $j) {
$id_pregunta = mysql_result($resultP,$j,"id_pregunta");
$num_pregunta = mysql_result($resultP,$j,"num_pregunta");
$cuestion = mysql_result($resultP,$j,"cuestion");
$a = mysql_result($resultP,$j,"a");
$b = mysql_result($resultP,$j,"b");
$c = mysql_result($resultP,$j,"c");
$resposta = mysql_result($resultP,$j,"resposta");
$foto = mysql_result($resultP,$j,"foto");
?>
<table width="100%" height="178" border="0" cellpadding="11" cellspacing="-3" bordercolor="#6260E8" bgcolor="#6581D4">
<tr>
<td width="30%" rowspan="2"><img src="<?php echo $foto?>" width="175" height="150"></td>
<td width="6%"><b class="numero"><?php echo $n; echo ('.'); $n++;?></b></td>
<td width="64%" colspan="2"><div align="justify" class="Estilo25"><?php echo $cuestion?></div></td>
</tr>
<tr>
<td colspan="3">
<form name="f" method="post" action="" onSubmit="return crearArray(this)">
<p>
<label>
<b class="Estilo24">a)</b><input type="radio" name="<?php echo $opc ?>" value="a"> <?php echo $a ?></label>
<br>
<label>
<b class="Estilo24">b)</b><input type="radio" name="<?php echo $opc ?>" value="b"> <?php echo $b ?></label>
<br>
<label>
<b class="Estilo24">c)</b><input type="radio" name="<?php echo $opc ?>" value="c"> <?php echo $c ?></label>
<br>
</p>

</td>
<hr>
</tr>

</table>

<?php
$j++;
}
//echo $opc;
array_push($rA,$resposta); // Vamos introducindo no array 'rA' a resposta da pregunta aleatoria
array_push($tA,$idtest); // Vamos introducindo no array 'tA' o test da pregunta aleatoria
array_push($pA,$num_pregunta); // Vamos introducindo no array 'pA' o número da pregunta aleatoria
$i++;
$opc++;
echo $idtest; echo (','); echo $num_pregunta; echo (','); echo $resposta;
}
} //fin do else
$volta--;
}
mysql_close($enlace);

?>
<input type="hidden" name="testHidden">
<input type="hidden" name="correxir" value="0">
<p></p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;
<input name="pulsado" type="submit" value="Correxir Test" onClick="javascript:document.f.correxir.value='1'; document.f.submit();" >

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;
<a href="#head" title="Ir a pregunta nº1">Ir arriba ---^</a>
</form>
<?php }
$y=0;
while ($y < 30){
echo array_shift($pA);
$y++;
}


/////////////////////////////////////
if ($_POST[correxir]==1){ // AQUÍ NO ME RECONOCE LOS ARRAYS!!!

$host = "localhost";
$usuario = "banban2";
$password = "banban2";
$NomeDB = "autoescola_db";
$NomeTaboa = "test_pregunta";
$NomeTaboaP = "pregunta";
$volta = 0; //Número de tests (10 tests=30 preguntas)
//$opc = a; // Identificador de cada un dos 30 radio button do test.
//$rA=array(); //Array que conterá as respostas das preguntas aleatorias.
//$tA=array(); //Array que conterá o número de test de cada pregunta aleatoria.
//$pA=array(); //Array que conterá o número de cada pregunta aleatoria.
$n = 1; // Variable para numerar as preguntas aleatorias.

$enlace = mysql_connect($host,$usuario,$password);
@mysql_select_db("$NomeDB");



while ($volta < 10){

$sqlquery = "SELECT * FROM $NomeTaboa WHERE id_test = '$tA[$volta]'";

$result = mysql_query($sqlquery);
$number = mysql_num_rows($result);

if ($number < 1) {
echo "<CENTER><P>Non existen resultados para esta búsqueda</CENTER>";
}
else {
$i = 0;
$number = 3;
while ($number > $i) {
$idtest = mysql_result($result,$i,"id_test");
$idpregunta = mysql_result($result,$i,"id_pregunta");

$enlace = mysql_connect($host,$usuario,$password);
@mysql_select_db("$NomeDB");
$sqlqueryP = "SELECT * FROM $NomeTaboaP WHERE id_pregunta = '$pA[$i]'";
$resultP = mysql_query($sqlqueryP);
$numberP = mysql_num_rows($resultP);
$j = 0;

while ($numberP > $j) {
$id_pregunta = mysql_result($resultP,$j,"id_pregunta");
$num_pregunta = mysql_result($resultP,$j,"num_pregunta");
$cuestion = mysql_result($resultP,$j,"cuestion");
$a = mysql_result($resultP,$j,"a");
$b = mysql_result($resultP,$j,"b");
$c = mysql_result($resultP,$j,"c");
$resposta = mysql_result($resultP,$j,"resposta");
$foto = mysql_result($resultP,$j,"foto");
?>
<table width="100%" height="178" border="0" cellpadding="11" cellspacing="-3" bordercolor="#6260E8" bgcolor="#6581D4">
<tr>
<td width="30%" rowspan="2"><img src="<?php echo $foto?>" width="175" height="150"></td>
<td width="6%"><b class="numero"><?php echo $n; echo ('.'); $n++;?></b></td>
<td width="64%" colspan="2"><div align="justify" class="Estilo25"><?php echo $cuestion?></div></td>
</tr>
<tr>
<td colspan="3">
<form name="f" method="post" action="" onSubmit="return crearArray(this)">
<p>
<label>
<b class="Estilo24">a)</b><input type="radio" name="<?php echo $opc ?>" value="a"> <?php echo $a ?></label>
<br>
<label>
<b class="Estilo24">b)</b><input type="radio" name="<?php echo $opc ?>" value="b"> <?php echo $b ?></label>
<br>
<label>
<b class="Estilo24">c)</b><input type="radio" name="<?php echo $opc ?>" value="c"> <?php echo $c ?></label>
<br>
</p>
</form>
</td>
<hr>
</tr>

</table>

<?php
$j++;
}
//echo $opc;

$i++;
$opc++;
echo $idtest; echo (','); echo $num_pregunta; echo (','); echo $resposta;
}
} //fin do else
$volta++;
}
mysql_close($enlace);


$ben=0;
$mal=0;
$incorrecta=array();
$correcta=array();
$numeros=array();
for ($r=0 ; $r<30 ; $r++){
$valor=$arrayPHP[$r];
if($rA[$r]==$valor) $ben++;
if( ($rA[$r]!=$valor) ||($rA[$r]=='-') ){

$mal++;
array_push($numeros,$r+1);
array_push($incorrecta,$valor);
array_push($correcta,$rA[$r]);
}
}
}

?>

Gracias de antemano, espero alguien pueda ayudarme.