0

i have a php code to random 1 - 50 question from database anh print it in web

$arr1=array();
	for($x=0;$x<10;$x++)
	{
		while(1)
		{
			$s=rand(0,50);
			if(ktra($arr1,$s)==1)
			{
				$arr1[$x]=$s;
				break;
			}
		}
	}	
	
		
		$y=0;$arr2=array();
		?>
        <?php
		for($x=0;$x<count($arr1);$x++)
		{
			$sql="select * from question where IDQuestion='".$arr1[$x]."'";
			$result=DataProvider::ExecuteQuery($sql);
			if($result !=false)
			{
			while($row=mysql_fetch_array($result,MYSQL_ASSOC))
				{
					$noidung=$row["Content"];
					$caua=$row["A"];
					$caub=$row["B"];
					$cauc=$row["C"];
					$caud=$row["D"];
					$id=$row["IDQuestion"];
                    
				}
			}
			?>
            	<?php echo "<b><font color=#0F0 size=5px>$noidung</font></b>"?>
                <br />
                <form id = "checkbox" action="Ketqua.php" method="post">
            	<div>A:<input type="radio" name="<?php echo $id?>" value='$caua'/> <?php echo $caua?><br /> </div>
                <div>B:<input type="radio" name="<?php echo $id?>" value='$caub'/> <?php echo $caub?><br /> </div>
                <div>C:<input type="radio" name="<?php echo $id?>" value='$cauc'/> <?php echo $cauc?><br /> </div>
                <div>D:<input type="radio" name="<?php echo $id?>" value='$caud'/> <?php echo $caud?><br /> </div>

how can i get value from check radio button and print it to screen
thanks

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by blocblue
0

When the form is submitted, you can access the input value from the POST array:

var_dump($_POST[$id]);

Also, your method for selecting random questions doesn't look too efficient. You're making a separate query for each question. An alternative would be to move the random element into the SQL query. E.g.:

$sql = "select * from `question` ORDER BY RAND() LIMIT 10";

Hope this helps,
R.

0

thanks for your alternative but i don't understand exactly your mean about var_dump($_POST[$id]);
I put it to php file and when submit this error appear :

Notice: Undefined variable: id in C:\wamp\www\GK\content\Ketqua.php on line 11

Notice: Undefined index: in C:\wamp\www\GK\content\Ketqua.php on line 11
NULL

0

Okay. Given that you're not going to know what the question ids are, because they're selected randomly, I would suggest posting all of the question answers in an array. E.g.

<div>
    A:<input type="radio" name="question[<?php echo $id?>]" value='<?php echo $caua; ?>'/>
    <?php echo $caua?>
</div>
<div>
    B:<input type="radio" name="question[<?php echo $id?>]" value='<?php echo $caub; ?>'/>
    <?php echo $caub; ?>
</div>
<div>
    C:<input type="radio" name="question[<?php echo $id?>]" value='<?php echo $cauc; ?>'/>
    <?php echo $cauc; ?>
</div>
<div>
    D:<input type="radio" name="question[<?php echo $id?>]" value='<?php echo $caud; ?>'/>
    <?php echo $caud; ?>
</div>

Then to retrieve the question responses after the form has been submitted, you can just loop through the $_POST array. E.g.:

if(isset($_POST['question']) {
    foreach($_POST['question'] as $id => $value) {
        // Output question id and answer
        echo "{$id} = {$value}<br />";
    }
}

R.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.