我正在尝试找出最好的方法,但不确定如何。有人可以帮帮我吗?
说我有4分 - 金,银,铜和In Development 其中一个标记将是db中$ gsb ['gsb']的值。
下面的代码获取课程和回声img和链接。 我正在努力的是如何对上面的4个值进行排名,这样如果返回的结果不止一个,它将获得最高或最低值,具体取决于所选的排名,并且仅回显一个结果/ img
希望这很清楚。我非常感谢任何帮助/指导。
$metacourses = mysql_query("SELECT * FROM mdl_course_meta where parent_course = $courseid");
while($parentcourse = mysql_fetch_assoc($metacourses)){
//Select GSB for course
$parentcourseid = $parentcourse['child_course'];
$gsb = mysql_fetch_array(mysql_query("SELECT * FROM mdl_gsb_content where course = $parentcourseid"));
//Used for $img name
if ($gsb['gsb']=="") {$thegsbscore = "in_development";}
else {$thegsbscore = $gsb['gsb'];}
if ($viewgsb == 'Yes'){
$img = '<div align="center"><img src="'.$CFG->wwwroot.'/blocks/gsb/images/'.strtolower($thegsbscore).'.png" width="90" height="98"></div>';
$link = '<p align="center"><b><a href="'.$CFG->wwwroot.'/blocks/gsb/gsb_explained.htm" target="_blank">How can I improve my course medal?</a></b></p>';
}
else {
$img = '';
$message = '';
$viewgsb = '';
$link = '';
}
}
$this->content = new stdClass;
$this->content->text = $message . $img . $link;
$this->content->footer = '';
return $this->content;
return $this->content->text;
答案 0 :(得分:2)
为您的值指定权重,最好是在表格中。
像这样使用:
TABLE_WEIGHT (It's a better idea to store these in a separate table, that way you can always add/delete more scores, irrespective of other tables)
ID(PRIMARY KEY) SCORE_NAME(VARCHAR) SCORE_WEIGHTAGE(INT/ENUM)
1 In Development 1
2 Bronze 2
3 Silver 3
4 Gold 4
现在,您始终可以在第二个查询中使用SQL JOIN对数据进行排序。此外,如果有多个结果,您可以使用PHP的usort来过滤结果。
希望这有帮助。
答案 1 :(得分:1)
您可以通过多种方式执行此操作,按照映射权重的字段排序,加入另一个具有权重的表并获取权重。 pushpesh是你最好的解决方案
SELECT * FROM mdl_gsb_content
where course = $parentcourseid
join table_weight
on table_weight.score_name = mdl_gsb_content.gsb
order by table_weight.SCORE_WEIGHTAGE.
(假设gsb是得分)
没有经过测试,但应该做到这一点。
在测试后按照Codded编辑:
SELECT * FROM mdl_gsb_content
JOIN mdl_gsb_scores on mdl_gsb_scores.score = mdl_gsb_content.gsb
WHERE mdl_gsb_content.courseid = $parentcourseid
ORDER by mdl_gsb_scores.rank ASC
LIMIT 1
答案 2 :(得分:1)
在实现了Pushpeshs权重表并从编码SQL查询中解决后,我终于找到了正确的方法。
$metacourses = mysql_query("SELECT * FROM mdl_course_meta where parent_course = $courseid");
//Select GSB for course
$types = array();
while(($row = mysql_fetch_assoc($metacourses))) {
$types[] = $row['child_course'];
}
$theids = implode(',',$types);
$gsb = mysql_query("
SELECT * FROM mdl_gsb_content
JOIN mdl_gsb_scores on mdl_gsb_scores.score = mdl_gsb_content.gsb
WHERE mdl_gsb_content.courseid IN ($theids)
ORDER by mdl_gsb_scores.rank ASC
LIMIT 1");
while($score = mysql_fetch_assoc($gsb))
//Work out img
$thegsbscore = $score['gsb'];
if ($viewgsb == 'Yes'){
$img = '<div align="center"><img src="'.$CFG->wwwroot.'/blocks/gsb/images/'.strtolower($thegsbscore).'.png" width="90" height="98"></div>';
$link = '<p align="center"><b><a href="'.$CFG->wwwroot.'/blocks/gsb/gsb_explained.htm" target="_blank">How can I improve my course medal?</a></b></p>';
$message = '<div align="center">Your Unit VLE is:'.print_r($types).'</div><br />';
}
else {
$img = '';
$message = '';
$viewgsb = '';
$link = '';
}