如何为网页上的MYSQL提取的数据生成链接

时间:2014-11-15 14:14:39

标签: php

对于测验我正在从表格中获取一些随机问题并在网页上显示问题(一次一个问题)。 现在,通过计算所提取的问题,我将从1开始显示数字作为标签。

即。假设我已经提取了10个问题,那么它将显示标签为1 2 3 4 5 6 7 8 9 10.并且当前在网页上显示的问题将突出显示选项卡。

我的问题是如何将这些标签设为链接,这样当我点击该标签时,它会在网页上显示该问题。

查看:

<div id="container">
    <?php echo "<br />" ?>
<?php 
$attributes=array('id'=>'testForm');
echo form_open('attemptTest/getquestion/',$attributes);
?>
    <div id="body">
    <?php
    $totq=explode(",",$test['random_question_no']);
    $totq=array_sum($totq);
    $totalQuestions=array_sum(explode(",",$test['random_question_no']));
    $selected_answers=explode(',',$result['selected_answers']);
    $selected_answers=$selected_answers[$qno-1];
    ?>
    <input type="hidden" name="qno" value="<?=$qno?>" id="qno">
    <input type="hidden" name="direction" value="N" id="direction">
    <input type="hidden" name="submitTest" value="0" id="submitTest">
    <input type="hidden" name="time1" value="<?php $time_taken=explode(',',$result['time_taken']); echo (array_sum($time_taken)+$result['iniTime']);?>" >
    <code><table><tr><td valign="top" >Q<?=$qno?>) &nbsp;</td><td style="word-break: break-word;"><?=$question['question']?></td></tr></table></code>
    <?php $option=explode(',',$question['options']); 
    foreach($option as $option){ 
    $ans="ans".$qno;
    ?>
    <div id="qoption">
    <table><tr><td style="width:18px;" valign=top ><input type="radio" value="<?=$option?>" name="answer" <?php if($selected_answers==$option){ echo "checked";} ?>  >  </td><td><?=$option?></td></tr></table>
     </div>
    <?php 
     }
    ?>
    <br/>
    <table><tr><td><?php if($qno>="2"){ ?><div class="big_button" style="width:70px;"><center><a href="javascript:document.getElementById('direction').value='B';document.getElementById('testForm').submit();" id="big_button" >Back</a></center></div><?php } ?></td><td> &nbsp;&nbsp;</td><td>
    <?php if($qno<$totalQuestions){ ?><div class="big_button" style="width:70px;"><center><a href="javascript:document.getElementById('testForm').submit();" id="big_button">Next</a></center></div> <?php } ?>
    </td><td> &nbsp;&nbsp;</td>
    <td>
    <div class="big_button" style="width:120px;"><center>
    <a href="javascript:document.getElementById('submitTest').value='1';document.getElementById('testForm').submit();" id="big_button">Submit Test</a>
    </center></div>
    </td></tr></table>
    <br/>
    </div></div>

    <table id="all_questions">
            <tr>
                <?php $i=0; foreach($questions_all as $key=>$value){$i++;?>
                    <td class="<?php if($qno==$key+1){echo "active ";}?> <?php if(isset($_COOKIE['answered_'.$value])){if($_COOKIE['answered_'.$value]!=0){ echo "green";}} ?>" ><?=$key+1?></td><?php if($i==4){$i=0; echo "</tr><tr>";}?>
                <?php }?>
            </tr>
        </table>



        <style>
            #all_questions td{

                width:40px;
                height:40px;
                border-radius:3px;
                margin:5px;
                text-align:center;
                vertical-align:middle;
            }
            .active {
                background-color:red;   
            }

            .green {
                background-color:green;
            }

        </style>

控制器:

<?php
class attemptTest extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('test_model');
        $this->load->model('attempt_model');
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->library('session');
    // if not logged in redirect
        if(($this->session->userdata('logged_in'))!='TRUE'){ redirect('login'); } 
    }
    public function index($tid='',$access_token='0')
    {   


        $this->load->helper('cookie');
        $this->load->library('form_validation');

    // if test cookies exist bypass to getquestion function
        if(isset($_COOKIE['tid']) && isset($_COOKIE['qids']) && isset($_COOKIE['resultid']) && isset($_COOKIE['qno']) && isset($_COOKIE['access_token']))
        {
            $this->getquestion($_COOKIE['qno'],$_COOKIE['qids'],$_COOKIE['tid']);       
        }
        else
            {
    // else started new test so check access token cookie exist for test    if not redirect
            if(!isset($_COOKIE['access_token'])){
            redirect('test/view/'.$tid);
            }
    // getting test data
            $data['test'] = $this->test_model->get_test($tid,'0');
    // getting multi dimensonal arrays of question ids which are going to assign user
            $data['qids'] = $this->attempt_model->get_questions_id($data['test']);
    // make one dimensonal array
            $qid=array();
            foreach($data['qids'] as $question)
            {
            $qid[]=$question['qid'];    
            }
    // convert array into string
            $qids=implode(",",$qid);
    // insert result row
            $data['resultid']=$this->attempt_model->insert_result_row($data['test'],$qids); 
    // creating required cookies
            $cookie = array('name'=>'resultid','value'=>$data['resultid'],'expire'=>'86500'); 
            $this->input->set_cookie($cookie); 
            $cookie = array('name'=>'qids','value'=>$qids,'expire'=>'86500'); 
            $this->input->set_cookie($cookie); 
            $cookie = array('name'=>'tid','value'=>$data['test']['tid'],'expire'=>'86500'); 
            $this->input->set_cookie($cookie); 
            $cookie = array('name'=>'qno','value'=>'0','expire'=>'86500'); 
            $this->input->set_cookie($cookie); 
            $this->getquestion('0',$qids,$tid,$data['resultid']);
            }
    }

    public function getquestion($qno=0,$qids=0,$tid=0,$resultid=0)
    {
    if(!isset($_COOKIE['access_token'])){
            redirect('index');
            }
    $this->load->helper('cookie');
    $this->load->library('form_validation');



    //define question ids
    if(isset($_COOKIE['qids'])){
    $qids=$_COOKIE['qids'];
    }else{
    $qids=$qids;
    }
    $eqid=explode(",",$qids);

    //addition od code by vivek
    $data['questions_all'] = $eqid;

    //define test id
    if(isset($_COOKIE['tid'])){
    $tid=$_COOKIE['tid'];
    }else{
    $tid=$tid;
    }

    if(isset($_COOKIE['resultid'])){
    $resultid=$_COOKIE['resultid'];
    }
    $iniTime = $this->attempt_model->get_result($resultid);
    $test_time = $this->test_model->get_test($tid,'0');
    // check time

    if(((($test_time['test_time']*60)-(time()-$iniTime['iniTime']))<="0") || ((time())>($test_time['end_time']))){
    $this->submit('Time Over');
        return;

    }

    // define question no.
    if(isset($_POST['qno'])){ 
    $qno=$_POST['qno'];

    $pqid=$eqid[$qno-1];
    if(isset($_POST['answer'])){
    $answer=$_POST['answer'];
    $time1=$_POST['time1'];
    }else{ $answer='NULL'; $time1=$_POST['time1']; }
    $this->attempt_model->submit_answer($_COOKIE['resultid'],$qno,$_COOKIE['tid'],$pqid,$answer,$time1);
    }

    if(isset($_POST['submitTest']))
    {
        if($_POST['submitTest']=='1')
        {
        $this->submit();
        return;
        }
    }

    // next question
    if(isset($_POST['direction']))
    { 
    // if direction is for next question add one
    if($_POST['direction']=='N'){ 
    $data['qno']=$qno+1; $qid=$eqid[$qno]; 
    }
    else
    { 
    // else subtract one
    $data['qno']=$qno-1; 
    $qid=$eqid[$qno-2]; 
    }
    }
    else{
    $data['qno']=$qno+1;
    $qid=$eqid[$qno];
    }

    // getting earlier submitted answer if any
    $data['result'] = $this->attempt_model->get_result($resultid);
    // getting test data
    $data['test'] = $this->test_model->get_test($tid,'0');
    // getting question data
    $data['question'] = $this->attempt_model->get_question($qid);
    // load header page
    $this->load->view('header_attemptTest.php',$data);
    // load attempt test body page
    $this->load->view('attemptTest',$data);
    // load footer
    $this->load->view('footer');
    // update cookies with question number
    $cookie = array('name'=>'qno','value'=>$data['qno'],'expire'=>'86500'); 
    $this->input->set_cookie($cookie); 
    }


    public function submit($msg='')
    {   
        $this->load->helper('cookie');
        $this->attempt_model->submit_test($_COOKIE['resultid'],$_COOKIE['tid']);
    // page title meassage
        $data['title']="Test Submitted";
        $data['msg']=$msg;
        $data['resultid']=$_COOKIE['resultid'];
        $this->load->view('header.php',$data);
        $this->load->view('submitTest',$data);
        $this->load->view('footer');
    // unset test cookies
            $cookie = array('name'=>'resultid','value'=>'','expire'=>'1'); 
            $this->input->set_cookie($cookie); 
            $cookie = array('name'=>'qids','value'=>'','expire'=>'1'); 
            $this->input->set_cookie($cookie); 
            $cookie = array('name'=>'tid','value'=>'','expire'=>'1'); 
            $this->input->set_cookie($cookie); 
            $cookie = array('name'=>'qno','value'=>'','expire'=>'1'); 
            $this->input->set_cookie($cookie); 
            $cookie = array('name'=>'access_token','value'=>'','expire'=>'1'); 
            $this->input->set_cookie($cookie); 


    }
}
?>

型号:

<?php
class attempt_model extends CI_Model {
    public function __construct()
    {   
        $this->load->database();
    }
    public function get_questions_id($test)
    {   
    $noqs=explode(',',$test['random_question_no']);
    $sids=explode(',',$test['subject_ids']);
        foreach($noqs as $key => $noq){
        $sid=$sids[$key];
        if($key==0){
        $sql="(select qid from questionBank where sid='$sid' order by rand() LIMIT $noq )";
        }else{
        $sql=$sql."UNION (select qid from questionBank where sid='$sid' order by rand() LIMIT $noq )";
        }
        }
        $query = $this->db->query($sql);
        $questions=$query->result_array();
        return $questions;
    }
    // return single question
    public function get_question($qid)
    {   
    $sql="select * from questionBank where qid='$qid' ";
    $query = $this->db->query($sql);
    $questions=$query->row_array();
    return $questions;
    }
    // return multiple questions
    public function get_questions($qids)
    {   
    $sql="select * from questionBank where qid in($qids) ORDER BY FIELD(qid, $qids)  ";
    $query = $this->db->query($sql);
    $questions=$query->result_array();
    return $questions;
    }
    public function get_result($resultid)
    {   
    $sql="select * from result where result_id='$resultid' ";
    $query = $this->db->query($sql);
    $result=$query->row_array();
    return $result;
    }
    public function submit_answer($resultid,$qno,$tid,$qid,$posted_answer,$time1)
    {
    $sql="select * from result where result_id='$resultid' ";
    $query1 = $this->db->query($sql);
    $result=$query1->row_array();
    // getting correct answer from question
    $sql="select answer from questionBank where qid='$qid' ";
    $query = $this->db->query($sql);
    $answer=$query->row_array();
    //compare posted answer with correct answer
    $correct_answer=explode(",",$result['correct_answer']);
    $selected_answers=explode(",",$result['selected_answers']);
    $time_taken=explode(",",$result['time_taken']);
    $time2=(time()-$time1);
    $time_taken[$qno-1]=($time_taken[$qno-1])+$time2;
    if($posted_answer!='NULL'){
    if($posted_answer==$answer['answer']){ $correct_answer[$qno-1]="1"; }else{ $correct_answer[$qno-1]="0"; } 
    $selected_answers[$qno-1]=$posted_answer;
    }
    $correct_answer=implode(",",$correct_answer);
    $selected_answers=implode(",",$selected_answers);
    $time_taken=implode(",",$time_taken);
    // Update result row
    $data = array('correct_answer' => $correct_answer,'selected_answers' => $selected_answers,'time_taken' => $time_taken);
    $this->db->where('result_id', $resultid);
    $this->db->update('result', $data);
    }
    public function submit_test($resultid,$tid)
    {
    // getting test data
    $sql="select * from test where tid='$tid' ";
    $query1 = $this->db->query($sql);
    $test=$query1->row_array();
    // getting result data
    $sql="select * from result where result_id='$resultid' ";
    $query2 = $this->db->query($sql);
    $result=$query2->row_array();
    // calculating result
    $correct_answer=$result['correct_answer'];
    $correct_answer=str_replace("2","0",$correct_answer);
    $correct_answer=explode(",",$correct_answer);
    $correctans=array_sum($correct_answer);
    $wrong_answer=$result['correct_answer']; $wrong_answer=explode(",",$wrong_answer); $wrong_ans=count( array_keys( $wrong_answer, "0" ));
    $total_marks=$correctans-$wrong_ans*$test['minus_mark'];
    $percentage=($total_marks/$result['total_question'])*100;
    if($percentage>=$test['reqpercentage'])
    {
    $status="1";
    }
    else
    {
    $status="0";
    }
    // update result row
    $data = array('obtained_percentage' => $percentage,'status' => $status,'submitTime'=>time());
    $this->db->where('result_id', $resultid);
    $this->db->update('result', $data); 
    }
        public function insert_result_row($test,$qids)
    {
    $totq=explode(",",$test['random_question_no']);
    $totqs=array_sum($totq);
    $temp_value=array();
    for($i=0; $i < $totqs; $i++)
    {
    $temp_value[$i]='2';
    }
    $temp_value=implode(",",$temp_value);
    $temp_time=array();
    for($j=0; $j < $totqs; $j++)
    {
        $temp_time[$j]='0';
    }
    $temp_time=implode(",",$temp_time);
    $uid=$this->session->userdata('uid');
    // insert row
    $data=array('uid'=>$uid,'tid'=>$test['tid'],'total_question'=>$totqs,'correct_answer' => $temp_value,'time_taken' => $temp_time,    'selected_answers' => $temp_value,'question_ids'=>$qids,'status'=>'2','iniTime'=>time());
    $this->db->insert('result', $data); 
        return $this->db->insert_id();
    }
}
?>

1 个答案:

答案 0 :(得分:0)

我天赐不读你的代码,但我想你会想要这个;)

在这种情况下,我们假设此代码全部位于 index.php 文件中。

这是您链接到您的问题的方式(或者您可以以任何其他方式重定向):

<?php 
    $id = -1;
    if (isset($_GET["questionid"])) {
        $id = $_GET["questionid"];
    }
?>

<a href="index.php?questionid=1" <?php if($id == 1){echo " class='active'"} ?>>Question 1</a>
<a href="index.php?questionid=2" <?php if($id == 2){echo " class='active'"} ?>>Question 2</a>
<a href="index.php?questionid=3" <?php if($id == 3){echo " class='active'"} ?>>Question 3</a>

以下是如何使用id questionid 显示问题的代码。

<?php
    if ($id > -1) {
        showQuestion($id);
        // = your code to show question with id questionid
    }
    else {
        echo "No question selected";
    }
?>