一款簡單的php教程 mysql教程考試系統,執行個體方法是判斷題目數組是否為空白,為空白則從題庫中隨機取出12道題 id
<?php
include './inc/header.php';
if($_session['arr'] == ''){ #判斷題目數組是否為空白,為空白則從題庫中隨機取出12道題 id
$sql = 'select * from an';
$select = $db->select($sql);
for($i=0;$i<count($select);$i++){
$aid .= $select[$i]['id'].',';
}
$arr = explode(',',substr($aid,0,strlen($aid)-1));
shuffle($arr);
$_session['arr'] = array_slice($arr,0,12);
}
$array = $_session['arr'];
if($_server['request_method'] == "post"){
if($_session['arr1'] == ""){ #arr1存的是已答題的id
$ary = $array;
array_shift($ary); #每答完一道題後,刪除數組第一位
$_session['arr1'] = $ary;
}else{
$ary = $_session['arr1'];
array_shift($ary);
$_session['arr1'] = $ary;
}
$id = $_post['id'];
$answer = $_post['answer'];
$sqq = 'select * from an where id='.$id.' and answer="'.$answer.'"';
$qus = mysql_query($sqq);
$rows = @mysql_num_rows($qus);
if($rows){
$_session['right'] .= $id.','; #將答對題id存到session['right']
$rightarr = explode(',',substr($_session['right'],0,strlen($_session['right'])-1)); #判斷對題數
if(count($rightarr) < 10){ #如果小於10則繼續答下一題
$sql2 = 'select * from an where id='.$ary[0];
$sel = $db->select($sql2);
}elseif(count($rightarr) == 10){ #如果等於10則跳出此次問答
$time = time();
$_session['jiang'] = md5($time);
#echo '<script>location.href="right.php?uid='.$time.'";</script>';
$success = array('second'=>'3','url'=>'right.php?uid='.$time,'title'=>'答對十題','right'=>'恭喜你,你已連續答對十道題,將跳轉到兌獎頁面。');
$smarty->assign('msg',$success);
$smarty->display('success.html');
exit();
}
}else{
$_session['error'] .= $id.','; #將答錯題id存到session['error']
$errorarr = explode(',',substr($_session['error'],0,strlen($_session['error'])-1)); #判斷錯題數
if(count($errorarr) < 2){
$sql2 = 'select * from an where id='.$ary[0];
$sel = $db->select($sql2);
}elseif(count($errorarr) == 2){ #如果答錯兩題,則重新計數
unset($_session['right']);
unset($_session['error']);
unset($_session['arr1']);
unset($_session['arr']);
unset($_session['num']);
session_destroy();
#echo '<script>location.href="index.php";</script>';
$success = array('second'=>'3','url'=>'index.php','title'=>'答錯兩題','error'=>'對不起,你已連續答錯兩題,將重新計算');
$smarty->assign('msg',$success);
$smarty->display('success.html');
exit();
}
}
echo 'right:'.$_session['right'].'<br />';
echo 'error:'.$_session['error'].'<br />';
}else{
$sqll = 'select * from an where id='.$array[0];
$sel = $db->select($sqll);
}
if(isset($_session['num'])){ #目前答第幾題
$num = $_session['num'] + 1;
$_session['num'] = $num;
#echo $_session['num'].'<br />';
#echo $num;
}else{
$num = 1;
$_session['num'] = $num;
}
$smarty->assign('num',$num);
$smarty->assign('ob',$sel);
$smarty->display('index.html');