-2

I am getting $final value from database and displaying in a table according to user. Now I have a value i.e., this is I was showing in the frontend

Userid   Total   Letter
  36       45
  5        67
  78       90
  42       82
  12       57

Back End Code

$sql = mysql_query('SELECT userid, total FROM history');
echo '<table><th>Userid</th>
             <th>Total</th>
             <th>Grade</th>
while($row = mysql_fetch_array($sql))
{
  echo '<tr><td>'.$row['userid'].'</td>  
            <td>'.$row['$total'].'</td> 
             $letter = mysql_query('SELECT score,letter FROM letter');               
             while($row = mysql_fetch_array($letter))
             {
               $score= $row['score'];
               $letterp= $row['letter']; 
               switch($row['$total']) 
              {
                case $row['$total'] == $score;
                echo '<td>'.$letterp.'</td>';
                break;
                case $row['$total'] >= $score;
                echo '<td>'.$letterp.'</td>';
                break;
              }                
        </tr>    
}
I have a letter table 
 ID  SCORE    letter
  1   100        A+
  2   90         A-
  3   80         B+
  4   73         B-
  5   65         C
  6   55         D
  7   45         E
  8   0          F

Switch case is a sample I wrote here. I need to give condition like if userid scores 45 then I need to show letter E and if userid scores 67 then I need to show B-(b'coz the scorefield defines 65 to 54 is B-) . This is the exact requirement.

4

1 回答 1

1

这样的事情应该做:

SELECT 
    CASE score 
        WHEN = 100 THEN 'A+'
        WHEN >= 90 and < 100 THEN 'A-' 
        WHEN >= 80 and < 89 THEN 'B+'
        WHEN >= 73 and < 80 THEN 'B-'
        // and so one...
        ELSE 'F' 
    END as myGrade,
    studentID
from 
    table1

编辑:对不起,我以为你正在尝试在数据库中执行此操作(如果我只想要成绩而不是分数,我可能会这样做。无论如何,这是 PHP 代码:

$myScore=90; // for example

function getMyScore($myScore)
{
    if($myScore==100)
    {
        return 'A+';
    }
    elseif ($myScore >= 90 && $myScore <100)
    {
        return 'A-';
    }
    elseif ($myScore >= 80 && $myScore <90)
    {
        return 'B+';
    }
    // .....
    else
    {
        return 'F';
    }
}

$myGrade=getMyGrade($myScore);
echo $myGrade; // output: A-

基于我写的函数:

function getMyScore($myScore)
{
    if($myScore==100)
    {
        return 'A+';
    }
    elseif ($myScore >= 90 && $myScore <100)
    {
        return 'A-';
    }
    elseif ($myScore >= 80 && $myScore <90)
    {
        return 'B+';
    }
    // .....
    else
    {
        return 'F';
    }
}

while($row = mysql_fetch_array($sql))
{
    echo '<tr><td>'.$row['userid'].'</td><td>'.$row['$total'].'</td>';

    // In your code:
    $letter = mysql_query('SELECT score,letter FROM letter');  
    while($row = mysql_fetch_array($letter))
    {
        $score= $row['score'];
        echo '<td>'.getMyGrade($row['letter']).'</td>';
    }
    echo'</tr>';    
}
于 2012-08-06T10:22:51.907 回答