使用php创建一个测试,将数组数据存储在数据库的字段中

Creating a quiz using php, To store array data in a field in the database

本文关键字:存储 数据 数组 数据库 字段 创建 php 测试 一个 使用      更新时间:2023-09-26

我正在尝试创建一个动态测试并将其存储在数据库中,输入由1个问题和4个答案组成
我使用jquery动态创建了文本框和单选按钮。1个文本框用于提问,另4个文本框则用于回答,我还有4个单选按钮用于正确回答。

var ctr = 2;
      $("#addTextBox").click(function() {
      var newTBdiv = $(document.createElement('div'))
          .attr("id", 'QuestionTBDiv' +ctr);
          newTBdiv.css({"margin-top":"20px" });
          newTBdiv.css({"width":"500px"});
          newTBdiv.css({"padding":"5px"});
          newTBdiv.after().html('<label class="mlabel">Question</label><br/><input type="text"  name="quiztxtBox'+ctr+'" size="57" id="quiztxtBox'+ctr+'" placeholder="Question #'+ctr+'"><br/><label class="mlabel">Answer</label><br/><input type="text" name="quiztxtBox[]" size="24" id="answerswer[]" placeholder="Choice A">&nbsp;<input type="radio" name="correct_answer'+ctr+'" value="A">&nbsp;<input type="text" name="answerswer'+ctr+'" size="24" id="answerswer'+ctr+'" placeholder="Choice B">&nbsp<input type="radio" name="correct_answer'+ctr+'" value="B"><br/><input type="text" name="answerswer'+ctr+'" size="24" id="answerswer'+ctr+'" placeholder="Choice C">&nbsp;<input type="radio" class = "choiceC" name="correct_answer'+ctr+'" value="C">&nbsp;<input type="text" name="answerswer'+ctr+'"size="24" id="answerswer'+ctr+'" placeholder="Choice D">&nbsp;<input type="radio" name="correct_answer'+ctr+'" value="D"><br><span name="errMchoice" class="errorMsg"></span>')
          newTBdiv.appendTo("#exam_container");
          ctr++;
    });

现在的问题是,我需要将问题和答案的数据存储在数据库中的一个字段中。。我发现使用serialize()函数可以将数组存储在字段中,但我不太确定我在做什么。我在想这样的事情。这是我想序列化并存储在数据库中的数组

$array = array(
             array(question1, answer1, answer2, answer3, answer4, correctanswer);
             array(question1, answer1, answer2, answer3, answer4, correctanswer);
);

或者还有其他方法可以实现这一点?

您所做的是更新您将使用2个表如下所示:

表:测验

id, title, creator, etc

表:测验_设置

id //(simply for managing rows and edits/updates)
"1", "2", "3" // etc
quiz_id
"55912" // or so on
type
"question", "answerswer"
ref_number
"1", "2" // This is basically Question 1, Question 2, Answer 1 etc.
value
"What is ...?" or "21" // The question or the answer

然后,您将获得id=55912的测验,并使用"quiz_id"==55912检查所有测验设置,使用"问题"字段和"ref_number"字段,生成一个包含问题的表或页面,然后生成一个必须等于"answerswer"1"的字段

如果有多个答案,只需循环遍历每个"答案"1",如果正确,+1,如果不正确,0。

//示例JS:

<script>
                var num_fields = <?=($num_fields > 0 ? $num_fields : 0)?>;
                var num_multi_fields = <?=($num_multi_fields > 0 ? $num_multi_fields : 0)?>;
                var cf = new Array();
                var cv = new Array();
                <?for($i=1;$i<=$num_fields;$i++) { ?>cf[<?=$i?>] = "<?=${'cf'.$i}?>"; cv[<?=$i?>] = "<?=${'cv'.$i}?>";<? } ?>
                var cmf = new Array();
                <?for($i=1;$i<=$num_multi_fields;$i++) { ?>
                    cmf[<?=$i?>] = "<?=${"cmf".$i}?>";
                    var cmv<?=$i?> = new Array();
                    <?$k = 0; for($j=1;$j<=${"cmf".$i."vt"};$j++) {?>
                        cmv<?=$i?>[<?=$k?>] = "<?=${"cmf".$i."v".$j}?>";
                    <? $k++; } ?>
                <?}  ?>
                $(document).ready(function() {
                    $("#num_fields").val(num_fields);
                    for(i=1;i<=num_fields;i++) {
                        $("#custom_fields").append(
                            '<div id="custom_'+i+'"><br/><label for="custom_field_'+i+'" class="custom-label">Custom Field '+i+': </label>&nbsp;'
                            + '<button type="button" class="btn btn-xs btn-danger delete_custom_o">Delete</button><br />'
                            + '<table width="100%"><tr><td>Field Name:</td><td>Field Value:</td></tr><tr>'
                            + '<td><input type="text" class="form-control custom_field" value="'+cf[i]+'" name="custom_field_'+i+'"/></td>'
                            + '<td><input type="text" class="form-control custom_value" value="'+cv[i]+'" name="custom_value_'+i+'"/></td></tr></table></div>'
                        );
                    };
                    $(".delete_custom_o").click(function() {
                        $(this).parent().remove();
                        var i = 0;
                        $("#custom_fields>div").each(function() {
                            i++;
                            $(this).find('label').text("Custom Field " +i+":");
                            $(this).find('.custom_field').attr("name", "custom_field_"+i);
                            $(this).find('.custom_value').attr("name", "custom_value_"+i);
                        });
                        num_fields--;
                        $("#num_fields").val(num_fields);
                    });
                });
                $("#add_field").click(function() {
                    num_fields++;
                    $("#num_fields").val(num_fields);
                    $("#custom_fields").append(
                        '<div id="custom_'+num_fields+'"><br/><label for="custom_field_'+num_fields+'">Custom Field '+num_fields+':</label>&nbsp;'
                        + '<button type="button" class="btn btn-xs btn-danger delete_custom_n">Delete</button><br />'
                        + '<table width="100%"><tr><td>Field Name:</td><td>Field Value:</td></tr><tr>'
                        + '<td><input type="text" class="form-control custom_field" name="custom_field_'+num_fields+'"/></td>'
                        + '<td><input type="text" class="form-control custom_value" name="custom_value_'+num_fields+'"/></td></tr></table></div>'
                    );
                });
                $('#custom_fields').on("click", ".delete_custom_n", function(){
                    $(this).parent().remove();
                    var i = 0;
                    $("#custom_fields>div").each(function() {
                        i++;
                        $(this).find('label').text("Custom Field " +i+":");
                        $(this).find('.custom_field').attr("name", "custom_field_"+i);
                        $(this).find('.custom_value').attr("name", "custom_value_"+i);
                    });
                    num_fields--;
                    $("#num_fields").val(num_fields);
                });
                $("#add_multi_field").click(function() {
                    num_multi_fields++;
                    $("#num_multi_fields").val(num_multi_fields);
                    $("#custom_multi_fields").append(
                        '<div id="custom_'+num_multi_fields+'" style="border-bottom: 1px solid #ddd; padding-bottom: 5px;"><br/>'
                        + '<label for="custom_field_'+num_multi_fields+'">Custom Field '+num_multi_fields+':</label>&nbsp;'
                        + '<button type="button" class="btn btn-xs em-bg-blue add_custom_n" data-id="'+num_multi_fields+'" data-target="#custom_table_'+num_multi_fields+'">Add</button>&nbsp;'
                        + '<button type="button" class="btn btn-xs btn-danger delete_custom_n">Delete</button><br />'
                        + '<div style="max-height:100px; overflow-y:auto; padding-left: 10px; padding-right: 10px;"><table width="100%" id="custom_table_'+num_multi_fields+'"><tr><td>Field Name:</td><td>Field Value:</td></tr><tr>'
                        + '<td><input type="text" class="form-control custom_field" name="custom_multi_field_'+num_multi_fields+'"/></td>'
                        + '<td><input type="text" class="form-control custom_value" name="custom_multi_value_'+num_multi_fields+'[]"/></td></tr>'
                        + '<tr><td><center><button type="button" class="btn btn-xs btn-danger delete_field">Remove</button></center></td>'
                        + '<td><input type="text" class="form-control custom_value" name="custom_multi_value_'+num_multi_fields+'[]"/></td></tr></table></div></div>'
                    );
                });
                $('#custom_multi_fields').on("click", ".add_custom_n", function(){
                    target = $(this).attr('data-target');
                    id = $(this).attr('data-id');
                    $(target).find('tbody')
                        .append($('<tr>')
                            .append($('<td>').html('<center><button type="button" class="btn btn-xs btn-danger delete_field">Remove</button></center>'))
                            .append($('<td>')
                                .append($('<input>').attr('type', 'text').attr('class', 'form-control custom_value').attr('name', 'custom_multi_value_'+id+'[]'))
                            )
                        );
                });
                $('#custom_multi_fields').on("click", ".delete_field", function(){
                    $(this).parent().parent().parent().remove(); 
                });
                $('#custom_multi_fields').on("click", ".delete_custom_n", function(){
                    $(this).parent().remove();
                    var i = 0;
                    $("#custom_multi_fields>div").each(function() {
                        i++;
                        $(this).find('label').attr('for','custom_field_'+i).text("Custom Field " +i+":");
                        $(this).attr('id','custom_'+i);
                        $(this).find('button.add_custom_n').attr('data-target', '#custom_table_'+i).attr('data-id', i);
                        $(this).find('table').attr("id","custom_table_"+i);
                        $(this).find('.custom_field').attr("name","custom_multi_field_"+i);
                        $(this).find('.custom_value').attr("name","custom_multi_value_"+i+"[]");
                    });
                    num_multi_fields--;
                    $("#num_multi_fields").val(num_multi_fields);
                });
                $(document).ready(function() {
                    $("#num_multi_fields").val(num_multi_fields);
                    for(i=1;i<=num_multi_fields;i++) {
                        var curr_array = window['cmv' + i];
                        string = "";
                        for(j=1;j<curr_array.length;j++) {
                            string += '<tr><td><center><button type="button" class="btn btn-xs btn-danger delete_field">Remove</button></center></td>'
                                + '<td><input type="text" class="form-control custom_value" value="'+curr_array[j]+'" name="custom_multi_value_'+i+'[]"/></td></tr>'
                        }
                        $("#custom_multi_fields").append(
                            '<div id="custom_'+i+'" style="border-bottom: 1px solid #ddd; padding-bottom: 5px;"><br/>'
                            + '<label for="custom_field_'+i+'">Custom Field '+i+':</label>&nbsp;'
                            + '<button type="button" class="btn btn-xs em-bg-blue add_custom_n" data-id="'+i+'" data-target="#custom_table_'+i+'">Add</button>&nbsp;'
                            + '<button type="button" class="btn btn-xs btn-danger delete_custom_n">Delete</button><br />'
                            + '<div style="max-height:100px; overflow-y:auto; padding-left: 10px; padding-right: 10px;"><table width="100%" id="custom_table_'+i+'"><tr><td>Field Name:</td><td>Field Value:</td></tr></tr>'
                            + '<td><input type="text" class="form-control custom_field" value="'+cmf[i]+'" name="custom_multi_field_'+i+'"/></td>'
                            + '<td><input type="text" class="form-control custom_value" value="'+curr_array[0]+'" name="custom_multi_value_'+i+'[]"/></td></tr>'
                            + ""+string+""
                            + '</table></div></div>'
                        );
                    };
                    $(".delete_custom_o").click(function() {
                        $(this).parent().remove();
                        var i = 0;
                        $("#custom_multi_fields>div").each(function() {
                            i++;
                            $(this).find('label').attr('for','custom_field_'+i).text("Custom Field " +i+":");
                            $(this).attr('id','custom_'+i);
                            $(this).find('button.add_custom_n').attr('data-target', '#custom_table_'+i).attr('data-id', i);
                            $(this).find('table').attr("id","custom_table_"+i);
                            $(this).find('.custom_field').attr("name","custom_multi_field_"+i);
                            $(this).find('.custom_value').attr("name","custom_multi_value_"+i+"[]");
                        });
                        num_multi_fields--;
                        $("#num_multi_fields").val(num_multi_fields);
                    });
                    $(".delete_field").click(function() {
                        $(this).parent().parent().parent().remove(); 
                    });
                });
            </script>

示例HTML:自定义字段=1对1关系(1个问题1个答案)-多字段=1对多关系(1问题多个答案)

<div class="row">
                <div class="col-lg-6">
                    <button type="button" class="btn em-bg-blue" id="add_field">Add Custom Field</button><br />
                    <div id="custom_fields" style="max-height: 500px; overflow-y: auto;"></div>
                </div>
                <div class="col-lg-6">
                    <button type="button" class="btn em-bg-blue" id="add_multi_field">Add Multi Field</button><br />
                    <div id="custom_multi_fields" style="max-height: 500px; overflow-y: auto;"></div>
                </div>
            </div>

Serialization是一个不错的方法。看看jQuery库,或者试着写一个非常简单的PHP文件,为您序列化。