简体   繁体   中英

If more than one result, rank values from one field in db and return only the highest/lowest ranked value

I am trying to work out the best way of doing this but not sure how. Could somebody help me please?

Say i have 4 marks - Gold, silver, Bronze and In Development One of these marks will be a value for $gsb['gsb'] from the db.

The code below gets the courses and echos back the img and link. The bit i am struggling with is how to rank the 4 values above, so that if there are more than one result returned it will get the highest or lowest value depending on the chosen ranking and only echo back that one result/img

Hope this is clear. I would be grealy appreciative of any help/guidance.

        $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;

Assign weightage to your values, preferably in a table.

Use like this:

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

Now, you can always sort your data by using a SQL JOIN in your second query. Also, in case of multiple results, you can use PHP's usort to filter your results.

Hope this helps.

you can do this multiple ways, order by a filed that maps weight, join to another tables that has weight and get the weights. pushpesh is the best solution you would then do

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.

(assuming gsb is he score)

not tested but should do the trick.

Edit By Codded after testing:

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

After implementing Pushpeshs weightage table and a work around from encodes SQL query i finally came up with the correct way.

    $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 = '';
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM