how to echo the highest value out of a variable

Discussion in 'PHP' started by ingilizdili, Nov 1, 2010.

  1. #1
    I find the percentages of correct answers with the following code:

    $per = ($row['correct'] / ($row['correct'] + $row['incorrect']))*'100';

    When I echo $per, I naturally get a list. What I actually want is to echo only the highest value out of that list. How can I do that? Is it possible to make $per an array variable?
     
    ingilizdili, Nov 1, 2010 IP
  2. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #2
    
    $highest =0;
    if ($per>$highest) {
     $highest = $per;
    }
    echo $highest;
    
    PHP:
     
    themullet, Nov 1, 2010 IP
  3. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No change. It doesn't work. It results in the same way as echo $per would.
     
    ingilizdili, Nov 1, 2010 IP
  4. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #4
    could you post the full code? / the code of that bit?

    
    $highest =0;
    while ($row = mysql_fetch_array($query)) {
    $per = ($row['correct'] / ($row['correct'] + $row['incorrect']))*'100';
    if ($per>$highest) {
     $highest = $per;
    }
    }
    echo $highest;
    
    PHP:
     
    themullet, Nov 1, 2010 IP
  5. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Here it is:

    while($row = mysql_fetch_array( $result )) {
    
    if($row['incorrect'] > '0') {     //to avoid dividing by zero
    $per = ($row['correct'] / ($row['correct'] + $row['incorrect']))*'100';
    
    echo $per, "<br>";
    
    }
    }
    PHP:
     
    ingilizdili, Nov 1, 2010 IP
  6. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #6
    $highest =0;
    while($row = mysql_fetch_array( $result )) {
    
    if($row['incorrect'] > '0') {     //to avoid dividing by zero
    $per = ($row['correct'] / ($row['correct'] + $row['incorrect']))*'100';
    if ($per>$highest) {
     $highest = $per;
    }
    //echo $per, "<br>";
    
    }
    }
    echo $highest;
    
    PHP:
     
    themullet, Nov 1, 2010 IP
  7. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Perfect! Thank you very much. It worked.
     
    ingilizdili, Nov 1, 2010 IP
  8. backlinkneeded

    backlinkneeded Member

    Messages:
    285
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #8
    wow easy coding info. Thanks
     
    backlinkneeded, Dec 22, 2010 IP