|
 |
|
-_-'ช่วยด้วยครับ ผมเขียน ภาษา php เพื่อที่จะแรนด้อม ข้อสอบโดย ดึงข้อสอบจากฐานข้อมูล |
|
 |
|
|
 |
 |
|
Code
CREATE TABLE `testing` (
`id` int(3) NOT NULL auto_increment,
`question` varchar(100) NOT NULL default '',
`c1` varchar(100) NOT NULL default '',
`c2` varchar(100) NOT NULL default '',
`c3` varchar(100) NOT NULL default '',
`c4` varchar(100) NOT NULL default '',
`answer` int(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=11 ;
INSERT INTO `testing` VALUES (1, 'ข้อที่ 1', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 1);
INSERT INTO `testing` VALUES (2, 'ข้อที่ 2', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 2);
INSERT INTO `testing` VALUES (3, 'ข้อที่ 3', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 3);
INSERT INTO `testing` VALUES (4, 'ข้อที่ 4', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 4);
INSERT INTO `testing` VALUES (5, 'ข้อที่ 5', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 4);
INSERT INTO `testing` VALUES (6, 'ข้อที่ 6', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 3);
INSERT INTO `testing` VALUES (7, 'ข้อที่ 7', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 2);
INSERT INTO `testing` VALUES (8, 'ข้อที่ 8', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 1);
INSERT INTO `testing` VALUES (9, 'ข้อที่ 9', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 2);
INSERT INTO `testing` VALUES (10, 'ข้อที่ 10', 'ตัวเลือกที่ 1', 'ตัวเลือกที่ 2', 'ตัวเลือกที่ 3', 'ตัวเลือกที่ 4', 3);
Code (PHP)
<html>
<body>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
<form name="form1" method="post" action="Sample2.php">
<?
$host="localhost";
$username="";
$password="";
$db="mydatabase";
$tb="testing";
mysql_connect( $host,$username,$password) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ ");
mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้");
$sql="Select * From $tb order by rand() limit 5";
$db_query=mysql_query($sql);
$i=0;
while($result=mysql_fetch_array($db_query))
{
$i++;
?>
<table width="64%" border="0" align="center">
<tr>
<td width="18%"> <div align="center">
<input name="id<?=$i;?>" type="hidden" value="<?=$result["id"];?>">
<?=$result["question"];?>
</div></td>
<td width="14%"> <input type="radio" name="c<?=$i;?>" value="1" checked>
<?=$result["c1"];?>
</td>
<td width="16%"> <input type="radio" name="c<?=$i;?>" value="2">
<?=$result["c2"];?>
</td>
<td width="16%"> <input type="radio" name="c<?=$i;?>" value="3">
<?=$result["c3"];?>
</td>
<td width="15%"> <input type="radio" name="c<?=$i;?>" value="4">
<?=$result["c4"];?>
<input name="answer<?=$i;?>" type="hidden" value="<?=$result["answer"];?>">
</td>
</tr>
</table>
<?
}
mysql_close();
?>
<div align="center"><br>
<input type="hidden" name="line" value="<?=$i;?>">
<input type="submit" name="Submit" value="ตรวจคะแนน">
</div>
</form>
</body>
</html>
Code (PHP)
<?
$score=0;
for($i=1;$i<=$_POST["line"];$i++)
{
If($_POST["c$i"] == $_POST["answer$i"])
{
$score=$score+1;
}
}
echo "True $score<br>";
?>
|
 |
 |
 |
 |
Date :
2011-09-08 23:43:25 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|