|
|
|
อยากสุ่มข้อสอบจากฐานข้อมูลมาวิชาละ 3 ข้อ คือตอนนี้มันสุ่มได้แต่มันสุ่มมาแบบมั่ว ๆ ค่ะ |
|
|
|
|
|
|
|
อยากสุ่มข้อสอบจากฐานข้อมูลมาวิชาละ 3 ข้อ คือตอนนี้มันสุ่มได้แต่มันสุ่มมาแบบมั่ว ๆ ค่ะ ตอนนี้ในฐานข้อมูลมี 11 วิชา ต้องทำยังไงค่ะ
Code (PHP)
<?php require_once('../Connections/MyConnec.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;
mysql_select_db($database_MyConnec, $MyConnec);
$query_Recordset1 = "SELECT * FROM test ORDER BY Rand() ASC";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $MyConnec) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
if (isset($_GET['totalRows_Recordset1'])) {
$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
$all_Recordset1 = mysql_query($query_Recordset1);
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body bgcolor="#FFFACD">
<!--<div align="center"><img src="หัวเว็บ.png" alt="" width="1113" height="203" /></div>-->
<form id="form1" name="form1" method="post" action="get-answer.php">
<input input type ="hidden" name="St_User" value="<?=$_POST["StuID"];?>">
<input type="hidden" name="StuID" value="<?php
$StuID = $_POST["StuID"];
echo "StuID is $StuID";
?>">
<p>
<?php
$chNum=1;
do {
?>
<br/>
<?php echo $chNum; ?>)<?php echo $row_Recordset1['T_question']; ?><br/>
<br/>
<input type="radio" name="<?php echo $row_Recordset1['T_id']; ?>" id="T_c1" value="1" />
<?php echo $row_Recordset1['T_c1']; ?><br />
<input type="radio" name="<?php echo $row_Recordset1['T_id']; ?>" id="T_c2" value="2" />
<?php echo $row_Recordset1['T_c2']; ?><br />
<input type="radio" name="<?php echo $row_Recordset1['T_id']; ?>" id="T_c3" value="3" />
<?php echo $row_Recordset1['T_c3']; ?><br />
<input type="radio" name="<?php echo $row_Recordset1['T_id']; ?>" id="T_c4" value="4" />
<?php echo $row_Recordset1['T_c4']; ?><br/>
<?php
$chNum++;
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</p>
<div class="container">
<ul class="pager">
<li class="previous"><a href="#">Previous</a></li>
<li class="next"><a href="#">Next</a></li>
</ul>
</div>
<p>
<center> <input type="submit" name="button" id="button" class="btn btn-info btn-block" value="ตกลง" onclick="myFunction()" />
<script>
function myFunction() {
alert("ยืนยันการสอบ");
}
</script>
<br />
</p>
</form>
<p> </p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
Tag : PHP, MySQL
|
|
|
|
|
|
Date :
2017-04-12 10:35:58 |
By :
Tarnikar |
View :
714 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตัวอย่าง Project เก่า ลองปรับแต่งดูนะครับ Random question and answer จาก subject
Code (PHP)
<form action="?Act=checkPoint" method="post">
<table class="table-full margin">
<tr>
<td><div class="green">แบบทดสอบ</div></td>
</tr>
<tr>
<td>
<?
$selectSub=mysql_query("SELECT * FROM subject"); // select แต่ละวิชาออกมาแสดง
while($sub=mysql_fetch_array($selectSub)){
$selectQue=mysql_query("SELECT * FROM question WHERE SubjectID='".$sub['SubjectID']."' ORDER BY rand() LIMIT 3"); // select คำถามแต่ละวิชา อ้างอิงจาก SubjectID แบบ Random limit 3
$no=0;
while($que=mysql_fetch_array($selectQue)){
$no++;
?>
<table class="table-full margin">
<tr>
<td width="17%" rowspan="2"><div class="no"><?=$no;?></div></td>
<td width="83%"><b>คำถาม :</b> <? echo $que['Question']; ?><input type="hidden" name="hdAnswerTrue[]" value="<?=$que['AnswerTrue'];?>" /></td>
</tr>
<tr>
<td>
<?
$noA=0;
$selectAns=mysql_query("SELECT * FROM answer WHERE QuestionID='".$que['QuestionID']."' ORDER BY rand()"); // select คำตอบ อ้างอิงจาก QuestionID แบบ Random
while($ans=mysql_fetch_array($selectAns)){
$noA++;
?>
<b>คำตอบที่ <?=$noA;?></b><input type="radio" id="Answer<?=$no;?>_<?=$noA;?>" name="Answer<?=$no;?>" value="<?=$ans['Answer'];?>" /><? echo $ans['Answer'];?><br>
<? } ?>
</td>
</tr>
<tr>
<td colspan="2"><div class="line"></div></td>
</tr>
</table>
<? }} ?>
</td>
</tr>
<tr>
<td><input type="submit" value="ส่งคำตอบ" onclick="return checkVal();" /></td>
</tr>
</table>
</form>
|
|
|
|
|
Date :
2017-04-12 15:25:33 |
By :
dudesaranyu |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|