自己记录方便使用:
注:数据量大可以拼接sql数据5000条插入一次;
<?php
class UploadsqlController extends AdminModel {
public function init(){
parent::init();
$this->public = 'http://'.$_SERVER['HTTP_HOST'].'/questionData/';
}
/**
* 导入数据库
*/
function importdataAction(){
$questions = file_get_contents($this->public."ti.txt");
$questions_array = explode("\n",$questions);
if($questions_array){
$i = 0;
foreach($questions_array as $questions_val){
$oneStr = substr($questions_val,0,1);
if(is_numeric($oneStr)){
$i++;
}
$questionsArray[$i][] = $questions_val;
}
}
$array_replace = array(".",".","。","#");
if($questionsArray){
$arr = array();
foreach($questionsArray as $k=> $questionsArray_val){
$options = array();
$answer = array();
foreach($questionsArray_val as $key=>$val){
$val = trim($val);
if(empty($val)){
unset($questionsArray_val[$key]);
}
if(preg_match ("/^[A-Za-z]/",substr($val,0,1))){
$str = substr($val,1);
//$str = str_replace(".","",$str);
//$str = str_repl
$replace = str_replace($array_replace,"",$str);
$options['choices'][] = $replace;
}
if(strpos($val,"简析")){
$analysis = str_replace("\\", "\\\\",json_encode($val));
}
if(substr($val,-1) == "#"){
$answer_str = substr($val,0,1);
switch ($answer_str)
{
case A:
$replace = "0";
break;
case B:
$replace = "1";
break;
case C:
$replace = "2";
break;
default:
$replace = "3";
}
$answer[] = $replace;
}
}
$count= count($answer);
$stem = explode(".",$questionsArray_val['0']);
$data = array(
'typeId' => ($count>1) ? 2 : 1,
'stem' => $stem['1'] ,
'answer' => str_replace("\\", "\\\\",json_encode($answer)),
'metas' => str_replace("\\", "\\\\",json_encode($options)),
'analysis' => $analysis,
'teachingMaterialId' => 1,
'categoryId'=> 1,
'difficulty'=> 1,
'subCount' => 1,
'userId' => 1,
'updatedTime' => time(),
'createdTime' => time()
);
Questionbank_QuestionModel::insert($data);
}
$this->success("导入成功");
}
}
}
格式:
A.资本主义经济的发展##
B.无产阶级反对资产阶级斗争的日趋激化
C.马克思恩格斯的革命实践
D.人类文明成果的最新发展
【简析】资本主义经济的发展为马克思主义的产生提供了经济
2.社会主义从空想发展到科学,是因为马克思恩格斯在新的历史条件下创立了
A.无产阶级革命学说
B.无产阶级政党学说
...............................