Jump to content

array_rand problem


fergie223

Recommended Posts

The array seems to be taking in the data but it doesnt seem to be able to output the array random. is there another way of doing it.

 

The problems seems to be coming from here. any help much appreciated. thanks in advance ;)

 

$Question = array_rand($Avail_Questions, 1);

 

 


<?php
#include("dbVariables.php");


require("Database_link.inc");
#connect to database



$SQL = "SELECT * FROM quizgen ORDER BY qID";
$result = mysqli_query($DB_Server, $SQL);# or die("unable to query database".mysql_error);


$Avail_Questions=array();
$i=0;

while ($dbRow=mysqli_fetch_array($result)) {
$Avail_Questions[$i++] = $dbRow;
}

#get data into the array

echo $Avail_Questions[0][4];
echo $Avail_Questions[1][0];
echo $Avail_Questions[2][2];
echo $Avail_Questions[3][10];

echo '<form action = "genresult2.php" method = "POST" >\n';

$Questions_Incr = 1;
$ten_Q = 1;
while($ten_Q <= 10) {
#loop througbh 10 questions and display them
$Question = array_rand($Avail_Questions, 1);
#select a random question

if(empty($Question)) {echo "EMPTY ARRAY";}

#show that question




echo "<p>In which year did ".$Question['teama']." defeat ".$Question['teamb']."?</p>\n";
   # echo "<p>In which year did ".$Avail_Questions[$ten_Q]['teama']." defeat ".$Avail_Questions[$ten_Q]['teamb']."?</p>\n";

#echo "<input type = 'hidden' name = 'qID' value = '".$Avail_Questions[$ten_Q]['qID']."'></input>\n";

             echo "<input type = 'hidden' name = 'qID' value = '".$Question['qID']."'></input>\n";

echo "<p>a) <input type = 'radio' name = '".$Questions_Incr."' value = '".$Question['Optn_1']."'></input></p>\n";
echo "<p>b) <input type = 'radio' name = '".$Questions_Incr."' value = '".$Question['Optn_2']."'></input></p>\n";
echo "<p>c) <input type = 'radio' name = '".$Questions_Incr."' value = '".$Question['Optn_3']."'></input></p>\n";
echo "<p>d) <input type = 'radio' name = '".$Questions_Incr."' value = '".$$Question['Optn_4']."'></input></p>\n";
echo "<input type = 'hidden' name = '".$Question_Incr."answer' value = '".$Question['Answer']."'></input>\n";
#echo "<input type = 'hidden' name = 'qID' value = '".$Question['qID']."'></input>\n";

unset($Question); #stop that question from being displayed again
$ten_Q ++;

$Questions_Incr ++;
}

echo "<p>Finished? <input type = 'submit' value = 'Check!'></input></p>\n";
echo "<p>Restart? <input type = 'reset' value = 'Start Again'></input></p>\n";

echo "<br><br><br><Br><Br><Br><br>\n";

?>


</body>

</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/47570-array_rand-problem/
Share on other sites

Example 258. array_rand() example

<?php

srand((float) microtime() * 10000000);

$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");

$rand_keys = array_rand($input, 2);

echo $input[$rand_keys[0]] . "\n";

echo $input[$rand_keys[1]] . "\n";

?>

 

from www.php.net/array_rand

 

From the above it does not seem like you are using the correct logic. The array returned is simply the index of the random variable.

 

I would highly suggest looking through the user comments and reading up on that function.

Link to comment
https://forums.phpfreaks.com/topic/47570-array_rand-problem/#findComment-232255
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.