虫言虫语 虫言虫语

使用faker\Factory生成考试题库逻辑

in 默认分类read (274387) 文章转载请注明来源!
 public function faker()
{
    $faker = Factory::create();
    $abc = 'ABCDEFG';
    for ($i = 0; $i < 500; $i++) {
        $question = new Qbank();
        $type = rand(1, 5);
        $qbank = rand(1, 6);

        $question->qbank = $qbank;
        $question->content = $faker->sentence;
        $question->qtype=$type;
        $qans=array();
        switch ($type) {
            case 1:
                $qnum = rand(3, 6);
                for ($i = 0; $i < $qnum; $i++) {
                    $qans[$abc[$i]] = $faker->sentence;
                }
                $qas = array_rand($qans, 1);
                $qa = $qas;
                $qans = json_encode($qans);

                break;
            case 2:
                $qnum = rand(3, 6);
                for ($i = 0; $i < $qnum; $i++) {
                    $qans[$abc[$i]] = $faker->sentence;
                }
                $qas = array_rand($qans, rand(2, $qnum));
                $qa = '';
                foreach ($qas as $q) {
                    $qa .= $q;
                }
                $qans = json_encode($qans);

                break;
            case 3:
                $qnum = '2';
                $qans['A']='正确';
                $qans['B']='错误';
                $qa=rand(0,1);
                $qa=$abc[$qa];
                $qans=json_encode($qans);
                break;
            case 4:
                $qnum = rand(1, 6);
                $qa='';
                for($i=0;$i<$qnum;$i++){
                    $qa.=$faker->word.',';
                }
                $qa=substr($qa,0,strlen($qa)-1);
                break;
            case 5:
                $qnum = null;
                $qa='';
                break;
        }

        $question->qnum = $qnum;
        $question->qans=$qans;
        $question->qa=$qa;
        $question->save();
        echo $i;
    }
}
jrotty WeChat Pay

微信打赏

jrotty Alipay

支付宝打赏

文章二维码

扫描二维码,在手机上阅读!

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /data/htdocs/worm.host.smartgslb.com/build/var/Typecho/Db/Adapter/Pdo.php on line 119 Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /data/sys/hostker_start_7.php on line 64