php mysql 線上考試系統

來源:互聯網
上載者:User

一款簡單的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');

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.