Jump to content

helllpppppppppppp!Array with rand function.


bumdeal2

Recommended Posts

Hey guys im a bit stuck here, i was wondering how youd put the answer,ranswer,ranswer1,ranswer2 into an array, and use the rand function to switch the postions of the answer....so that there not in the same position.

 

 

<?php



include('database_access_param.php');
include('auth.php');

$qid=$_POST['qid'];
$quesid=$_POST['quesid'];
$totalCorrect=$_POST['totalCorrect'];
$score=$_POST['score'];
$taken=$_POST['taken'];
import_request_variables("pgc","");


if(isset($_GET['quesid']))
{

   $quesid=$_GET['quesid'];
}


if(isset($_GET['qid']))
{

   $qid=$_GET['qid'];

}
if(isset($_GET['totalCorrect']))
{

   $totalCorrect=$_GET['totalCorrect'];

}
if(isset($_GET['score']))
{

   $score=$_GET['score'];

}
if(isset($_GET['taken']))
{

   $taken=$_GET['taken'];

}

    mysql_connect( $hostname, $dbuser, $dbpassword)
        or die ( 'Unable to connect to server' );

    

    mysql_select_db($dbname )
        or die ( 'Unable to select database' );


    $sql = "SELECT * FROM questiona WHERE qid = '$qid' && quesid = '$quesid' ";




                print("</head>");
                print('<BODY onLoad="goNewWin()">');
                print('<form action="grad.php?qid=$qid&quesid=$quesid&totalCorrect=$totalCorrect&taken=$taken" method="post" id="quiz">');
                print('<table class="report" align=left width=100%>');
                
                print('<tr><td> </td></tr>');


                $result=mysql_query($sql) or die ('Unable execute the query');
;



                if(mysql_numrows($result))
                {
                                $row = mysql_fetch_row($result);
                                {

                                $questid=$row[0];
                                $quesid=$row[1];
                                $question=$row[2];
                                $ranswer=$row[3];
                                $ranswer1=$row[4];
                                $ranswer2=$row[5];
                                $answer=$row[6];
                                $qid=$row[7];


print('<body>');

                                

        print('<tr><td >'.$quesid.'   '.$question.'</td>'.$Quiz_Topic.'</tr>');

        print('<input type= hidden name=qid value="'.$qid.'">');

        print('<input type= hidden name=totalCorrect value="'.$totalCorrect.'">');

        print('<input type= hidden name=quesid value="'.$quesid.'">');

        print('<input type= hidden name=score value="'.$score.'">');

        print('<input type= hidden name=taken value="'.$taken.'">');
      
        print('<tr><td><input type="radio" name="answer" id="answer" value="'.$ranswer .'">   '.$ranswer.'</td></tr>');

        print('<tr><td><input type="radio" name="answer" id="answer" value="'.$ranswer1 .'">   '.$ranswer1 .'</td></tr>');

        print('<tr><td><input type="radio" name="answer" id="answer" value="'.$ranswer2 .'">   '.$ranswer2 .'</td></tr>');

        print('<tr><td><input type="radio" name="answer" id="answer" value="answer">   '.$answer .'</td></tr>');

        print('<tr><td> <input type=hidden name=rightanswer value="'.$answer.'"><input type= hidden name=qnumber value="'.$question.'"></tr>');


        }
        print('<tr><td colspan=2><input type=submit name=submit value="Submit" ><input type=reset name=reset value="Clear" ></td></tr></table>');
        
         
        print('</form>');

        }
else
        {

         echo "<div id='results'>Your result is:  $score%</div>";
        print('<input type =hidden name=score value="'.$score.'">');
        print('</form>');

        }


?>

Hey I did as u said, Which shuffles all the values around but the answer stays in the same position

<?php



include('database_access_param.php');
include('auth.php');


$right=0;

$qid=$_POST['qid'];
$quesid=$_POST['quesid'];
$totalCorrect=$_POST['totalCorrect'];
$score=$_POST['score'];
$taken=$_POST['taken'];
import_request_variables("pgc","");


if(isset($_GET['quesid']))
{

   $quesid=$_GET['quesid'];
}


if(isset($_GET['qid']))
{

   $qid=$_GET['qid'];

}
if(isset($_GET['totalCorrect']))
{

   $totalCorrect=$_GET['totalCorrect'];

}
if(isset($_GET['score']))
{

   $score=$_GET['score'];

}
if(isset($_GET['taken']))
{

   $taken=$_GET['taken'];

}

    mysql_connect( $hostname, $dbuser, $dbpassword)
        or die ( 'Unable to connect to server' );

    

    mysql_select_db($dbname )
        or die ( 'Unable to select database' );


    $sql = "SELECT * FROM questiona WHERE qid = '$qid' && quesid = '$quesid' ";




	print("</head>");
	print('<BODY onLoad="goNewWin()">');
	print('<form action="grad.php?qid=$qid&quesid=$quesid&totalCorrect=$totalCorrect&taken=$taken" method="post" id="quiz">');
	print('<table class="report" align=left width=100%>');

	print('<tr><td> </td></tr>');


	$result=mysql_query($sql) or die ('Unable execute the query');
;



	if(mysql_numrows($result))
	{
			$row = mysql_fetch_row($result);
			{

			$questid=$row[0];
			$quesid=$row[1];
			$question=$row[2];
			$ranswer=$row[3];
			$ranswer1=$row[4];
			$ranswer2=$row[5];
			$answer=$row[6];
			$qid=$row[7];

$answer = array($ranswer,$ranswer1,$ranswer2,$answer);
shuffle ( $answer );





print('<body>');



print('<tr><td >'.$quesid.'   '.$question.'</td>'.$Quiz_Topic.'</tr>');

print('<input type= hidden name=qid value="'.$qid.'">');

print('<input type= hidden name=totalCorrect value="'.$totalCorrect.'">');

print('<input type= hidden name=quesid value="'.$quesid.'">');

print('<input type= hidden name=score value="'.$score.'">');

print('<input type= hidden name=taken value="'.$taken.'">');
      
print('<tr><td><input type="radio" name="answer" id="answer" value="'.$ranswer0.'">   '.$ranswer.'</td></tr>');

print('<tr><td><input type="radio" name="answer" id="answer" value="'.$ranswer1 .'">   '.$ranswer1 .'</td></tr>');

print('<tr><td><input type="radio" name="answer" id="answer" value="'.$ranswer2 .'">   '.$ranswer2 .'</td></tr>');

print('<tr><td><input type="radio" name="answer" id="answer" value="answer">   '.$answer .'</td></tr>');

print('<tr><td> <input type=hidden name=rightanswer value="'.$answer.'"><input type= hidden name=qnumber value="'.$question.'"></tr>');


}
print('<tr><td colspan=2><input type=submit name=submit value="Submit" ><input type=reset name=reset value="Clear" ></td></tr></table>');


print('</form>');

}
else
{

 echo "<div id='results'>Your result is:  $score%</div>";
print('<input type =hidden name=score value="'.$score.'">');
print('</form>');

}


?>

 

 

That's because instead of

print('<tr><td><input type="radio" name="answer" id="answer" value="'.$ranswer0.'">   '.$ranswer.'</td></tr>');

 

you should do

 

print('<tr><td><input type="radio" name="answer" id="answer" value="'.$answer[0].'">   '.$answer[0].'</td></tr>');

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.