[英]store more than one value into a db field to be retrieved as separate values
[英]If more than one result, rank values from one field in db and return only the highest/lowest ranked value
我正在嘗試找出執行此操作的最佳方法,但不確定如何操作。 有人可以幫我嗎?
假設我有4個標記-金,銀,銅和發展中標記之一就是來自數據庫的$ 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;
將權重分配給您的值,最好在表格中。
像這樣使用:
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過濾結果。
希望這可以幫助。
您可以通過多種方式執行此操作,通過映射權重的文件進行排序,連接到具有權重的另一個表並獲取權重。 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是他的得分)
未經測試,但應該可以解決。
測試后編輯方式:
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
在實現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 = '';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.