Php 如何计算Laravel 5.1中的分数
我对拉威尔很陌生 我有三个表格,分别是申请人、问题和答案: 申请人Php 如何计算Laravel 5.1中的分数,php,laravel-5.1,Php,Laravel 5.1,我对拉威尔很陌生 我有三个表格,分别是申请人、问题和答案: 申请人 id jobid name 1 151 ABC 2 151 DEF 答案 id qid jobid aaplicantid answer 1 21 151 1 1 2 22 151 1 0 3 21 151 2 1 4 22 151 2 1 id jobid answer 21 151 1
id jobid name
1 151 ABC
2 151 DEF
答案
id qid jobid aaplicantid answer
1 21 151 1 1
2 22 151 1 0
3 21 151 2 1
4 22 151 2 1
id jobid answer
21 151 1
22 151 1
问题
id qid jobid aaplicantid answer
1 21 151 1 1
2 22 151 1 0
3 21 151 2 1
4 22 151 2 1
id jobid answer
21 151 1
22 151 1
在这里,我想根据问题和答案表计算申请者分数的百分比,假设一份工作有两个问题,如果申请者的答案与问题表的答案匹配,那么他将得到100%的分数,如果任何一个不匹配,那么将从分数中减去很多
我想分数应该像这样分开
e、 g:一份工作有5个问题
score=>100/count(questions)=20 for each matched answer
对于Jobid151,申请人名单应如下所示
id name score joid
1 ABC 50% 151
2 DEF 100% 151
我正在使用下面的查询,但它没有给出预期的结果
$apps=applicants::where('jobid','=',$jobId)->get();
foreach($apps as $apo){
$appid[]=$apo->id;
}
$score=answers::whereIn('appid',$appid)->where('jobid','=',$jobId)->get();
$qid=array();
foreach($score as $s){
$qid[]=$s->qid;
}
$queid=questions::whereIn('id',$qid)->where('jobid','=',$jobId)->get();
$calscore=100/(count($queid));
$key = 0 ; $finalscore = array();$sss = 0;
for($n = 0; $n< count($queid); $n++) {
foreach($queid as $que){
$quei[]=$que->id;
$weight[]=$que->weight;
foreach ($score as $s){
$qid[]=$s->qid;
$sc[]=$s->answer;
if($que->id==$s->qid){
if($weight==$sc){
$finalscore=$sss+$calscore;
$sss++;
}
}
}
$key++;
}
}
dd($finalscore);
$apps=applicators::where('jobid','=',$jobid)->get();
foreach($apo形式的应用程序){
$appid[]=$apo->id;
}
$score=answers::where('appid',$appid)->where('jobid','=',$jobid)->get();
$qid=array();
foreach($s分数){
$qid[]=$s->qid;
}
$queid=questions::where('id',$qid)->where('jobid','=',$jobid)->get();
$calscore=100/(计数($queid));
$key=0$finalscore=array()$sss=0;
对于($n=0;$nid;
$weight[]=$que->weight;
foreach($s分数){
$qid[]=$s->qid;
$sc[]=$s->答案;
如果($que->id=$s->qid){
如果($weight==$sc){
$finalscore=$sss+$calscore;
$sss++;
}
}
}
$key++;
}
}
dd(最终核心);
甚至我也尝试了连接查询,但没有给出正确的结果。有雄辩的模型吗
有人能帮我解决这个问题吗。我建议您使用MySQL语句来计算百分比 您可以在Laravel MySQL数据库中创建视图
SELECT *, (SUM(answer) / COUNT(*) * 100) AS results
FROM answers
LEFT JOIN applicants
ON answers.applicantid = applicants.id
GROUP BY answers.applicantid
也可以从Laravel将其称为:
$results = DB::select(DB::raw("
SELECT *, (SUM(answer) / COUNT(*) * 100) AS results
FROM answers
LEFT JOIN applicants
ON answers.applicantid = applicants.id
GROUP BY answers.applicantid
"))
如您所见,根据您的示例,它将返回以下50%和100%的结果:
注意:
id jobid name
1 151 ABC
2 151 DEF