学习进度16

编程时间:12小时左右

代码行数:约1200行

博客园发表量:3篇

这周最大的收获就是一个随机答案显示的php程序

<?php

header("Content-Type:text/html;charset=utf-8");

//定义一组数组,作为备选项
include_once("conn.php");
if(empty($_POST))
{
    $sqlstr = "select * from answer where id=1";//定义查询语句
    $result = mysqli_query($conn, $sqlstr);//执行查询语句
}
else {
    $id = $_POST['id'] + 1;
    $sqlstr = "select * from answer where id=$id";//定义查询语句
    $result = mysqli_query($conn, $sqlstr);//执行查询语句
}

$flag=true;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>答案判断</title>
    <style>
        .jb_lg_btn{
            background: #d50f1f;
             200px;
            height: 20px;
            border: none;
            margin-left: 0px;
            margin-top:0px;
            color: #fff;
            font-size: 13px;
            font-family: "微软雅黑";
        }
        .jb_lg_int{
            width: 349px;
            height: 53px;
            margin-top: 8px;
            border: none;
        }
        .jb_lg_int input{
            width: 311px;
            height: 53px;
            padding-left: 38px;
        }
    </style>
</head>
<body>
<form action="" method="post">
<?php
    while($flag) {
        $rows = mysqli_fetch_row($result);
$m=$rows[0]-1;

if(empty($rows))
{
    $n=$_POST['id'];
    if ($_POST['choice'.$n] == $_POST['locationNum'.$n]) {
        echo "回答正确";
    } else {
        echo "回答错误";
    }
    echo '<br>';
}
if(!isset($_POST['submit'.$m])){

    $flag=false;
}
else if (!empty($_POST)) {
    if ($_POST['choice'.$m] == $_POST['locationNum'.$m]) {
        echo "回答正确";
    } else {
        echo "回答错误";
    }
    echo '<br>';

}
        $select = array("$rows[2]", "$rows[3]", "$rows[4]", "$rows[5]");

//定义第一个是正确答案

        $true_no = 1;
//正确答案
        $true = $select[$true_no - 1];
//打乱顺序,用PHP自带的shuffle函数
        shuffle($select);

//找到正确答案在数组的位置

        $locationNum = array_search($true, $select);
//检查正确答案

        ?>
    <input class="jb_lg_btn"type="text" value=<?php echo $rows[0]?> name="id" style=" 10px;float:left">
    <input class="jb_lg_btn"type="text" value="<?php echo $rows[1]?>" name="title">
<!--计算器--><br>
    A <input type="text" value="<?php echo $select[0]?>" name="answer1" style="margin-top: 20px;background:transparent;border:1px solid #ffffff">
    <br>
    B<input type="text" value="<?php echo $select[1]?>" name="answer2" style="margin-top: 20px;background:transparent;border:1px solid #ffffff">
    <br>
   C <input type="text" value="<?php echo $select[2]?>" name="answer3" style="margin-top: 20px;background:transparent;border:1px solid #ffffff">
    <br>
    D<input type="text" value="<?php echo $select[3]?>" name="answer4" style="margin-top: 20px;background:transparent;border:1px solid #ffffff">
    <br>
            <input type="radio" value=0 name="choice<?php echo $rows[0]?>">A
            <input type="radio" value=1 name="choice<?php echo $rows[0]?>">B
            <input type="radio" value=2 name="choice<?php echo $rows[0]?>">C
            <input type="radio" value=3 name="choice<?php echo $rows[0]?>">D
    <br>
            <input type="hidden" value="<?php echo $locationNum ?>" name="locationNum<?php echo $rows[0]?>">
            <input class="jb_lg_btn" type="submit" value="submit" name="submit<?php echo $rows[0]?>">
        </form>
        </body>
        </html>
        <?php
        $flag=false;
    }
?>
原文地址:https://www.cnblogs.com/Evak/p/11062309.html